bb/bot/commands/weather.go

68 lines
1.2 KiB
Go
Raw Normal View History

2022-09-06 05:00:47 +00:00
package commands
2022-08-24 14:06:00 +00:00
import (
"fmt"
"git.kill0.net/chill9/beepboop/bot"
"git.kill0.net/chill9/beepboop/lib/weather"
2022-08-24 14:06:00 +00:00
log "github.com/sirupsen/logrus"
2022-08-24 14:06:00 +00:00
)
type WeatherHandler struct {
Config bot.Config
Name string
2022-08-24 14:06:00 +00:00
}
2022-08-27 14:00:35 +00:00
func NewWeatherHandler(s string) *WeatherHandler {
2022-09-05 03:36:14 +00:00
return &WeatherHandler{Name: s}
2022-08-24 14:06:00 +00:00
}
func (h *WeatherHandler) SetConfig(config bot.Config) {
h.Config = config
}
2022-09-06 05:00:47 +00:00
func WeatherCommand(cmd *bot.Command, args []string) error {
2022-08-24 14:06:00 +00:00
var (
err error
loc string
w weather.Weather
2022-08-24 14:06:00 +00:00
)
2022-09-06 05:00:47 +00:00
if len(args) != 1 {
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, "help: `!weather <CITY>,<STATE>,<COUNTRY>`")
return nil
2022-08-24 14:06:00 +00:00
}
2022-09-06 05:00:47 +00:00
loc = args[0]
2022-08-24 14:06:00 +00:00
2022-09-06 05:00:47 +00:00
if cmd.Config.OpenWeatherMapToken == "" {
2022-08-24 14:06:00 +00:00
log.Error("OpenWeather token is not set")
2022-09-06 05:00:47 +00:00
return nil
2022-08-24 14:06:00 +00:00
}
2022-09-06 05:00:47 +00:00
wc := weather.NewClient(cmd.Config.OpenWeatherMapToken)
2022-08-24 14:06:00 +00:00
log.Debugf("weather requested for '%s'", loc)
2022-08-24 14:06:00 +00:00
w, err = wc.Get(loc)
2022-08-24 14:06:00 +00:00
if err != nil {
log.Errorf("weather client error: %v", err)
2022-09-06 05:00:47 +00:00
return nil
2022-08-24 14:06:00 +00:00
}
log.Debugf("weather returned for '%s': %+v", loc, w)
2022-08-24 14:06:00 +00:00
2022-09-06 05:00:47 +00:00
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, fmt.Sprintf(
2022-08-24 14:06:00 +00:00
"%s (%.1f, %.1f) — C:%.1f F:%.1f K:%.1f",
loc,
w.Coord.Lat,
w.Coord.Lon,
w.Main.Temp.Celcius(),
w.Main.Temp.Fahrenheit(),
w.Main.Temp.Kelvin(),
))
2022-09-06 05:00:47 +00:00
return nil
2022-08-24 14:06:00 +00:00
}