add current weather command

This commit is contained in:
lyx0 2022-08-15 15:04:43 +02:00
parent 4125967c63
commit f3bcd83855
4 changed files with 44 additions and 0 deletions

View file

@ -213,6 +213,14 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
case "xkcd": case "xkcd":
commands.Xkcd(target, app.TwitchClient) commands.Xkcd(target, app.TwitchClient)
return 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 // Commands with permission level or database from here on

1
go.mod
View file

@ -3,6 +3,7 @@ module github.com/lyx0/nourybot
go 1.19 go 1.19
require ( require (
github.com/briandowns/openweathermap v0.18.0
github.com/dustin/go-humanize v1.0.0 github.com/dustin/go-humanize v1.0.0
github.com/gempir/go-twitch-irc/v3 v3.2.0 github.com/gempir/go-twitch-irc/v3 v3.2.0
github.com/joho/godotenv v1.4.0 github.com/joho/godotenv v1.4.0

2
go.sum
View file

@ -1,5 +1,7 @@
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= 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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

33
pkg/commands/weather.go Normal file
View file

@ -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)
}