diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go index 4db34f9..e786afd 100644 --- a/cmd/bot/commands.go +++ b/cmd/bot/commands.go @@ -213,6 +213,14 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { case "xkcd": commands.Xkcd(target, app.TwitchClient) return + case "weather": + if msgLen < 2 { + common.Send(target, "Not enough arguments provided.", app.TwitchClient) + return + } else { + commands.Weather(target, message.Message[10:len(message.Message)], app.TwitchClient) + return + } // Commands with permission level or database from here on diff --git a/go.mod b/go.mod index f23d30a..3443b73 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/lyx0/nourybot go 1.19 require ( + github.com/briandowns/openweathermap v0.18.0 github.com/dustin/go-humanize v1.0.0 github.com/gempir/go-twitch-irc/v3 v3.2.0 github.com/joho/godotenv v1.4.0 diff --git a/go.sum b/go.sum index 87f22f0..43b1a05 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/briandowns/openweathermap v0.18.0 h1:JYTtJ4bKjXZRmDTe7huJ5+dZ7CsjPUw10GUzMASkNV8= +github.com/briandowns/openweathermap v0.18.0/go.mod h1:0GLnknqicWxXnGi1IqoOaZIw+kIe5hkt+YM5WY3j8+0= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/pkg/commands/weather.go b/pkg/commands/weather.go new file mode 100644 index 0000000..dc8de9d --- /dev/null +++ b/pkg/commands/weather.go @@ -0,0 +1,33 @@ +package commands + +import ( + "fmt" + "os" + + owm "github.com/briandowns/openweathermap" + "github.com/gempir/go-twitch-irc/v3" + "github.com/joho/godotenv" + "github.com/lyx0/nourybot/pkg/common" + "go.uber.org/zap" +) + +func Weather(target, location string, tc *twitch.Client) { + sugar := zap.NewExample().Sugar() + defer sugar.Sync() + err := godotenv.Load() + if err != nil { + sugar.Error("Error loading .env file") + } + owmKey := os.Getenv("OWM_KEY") + + w, err := owm.NewCurrent("C", "en", owmKey) + if err != nil { + sugar.Error(err) + } + w.CurrentByName(location) + // fmt.Println(w) + sugar.Infow("Weather", + "location", w) + reply := fmt.Sprintf("Weather for %s, %s: Feels like: %v°C. Currently %v°C with a high of %v°C and a low of %v°C, humidity: %v%%, wind: %vm/s.", w.Name, w.Sys.Country, w.Main.FeelsLike, w.Main.Temp, w.Main.TempMax, w.Main.TempMin, w.Main.Humidity, w.Wind.Speed) + common.Send(target, reply, tc) +}