diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index ccaa36f..9c2d298 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -58,6 +58,21 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { reply = "xd" } + case "bttv": + if msgLen < 2 { + reply = "Not enough arguments provided. Usage: ()bttv " + } else { + reply = commands.Bttv(cmdParams[1]) + } + + // Coinflip + case "coin": + reply = commands.Coinflip() + case "coinflip": + reply = commands.Coinflip() + case "cf": + reply = commands.Coinflip() + case "nourybot": reply = "Lidl Twitch bot made by @nourylul. Prefix: ()" @@ -65,6 +80,16 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { reply = commands.Ping() // ()bttv + // ()weather + case "weather": + if msgLen == 1 { + reply = "Not enough arguments provided." + } else if msgLen < 2 { + reply = "Not enough arguments provided." + } else { + reply, _ = commands.Weather(message.Message[10:len(message.Message)]) + } + // ################## // Check if the commandName exists as the "name" of a command in the database. // if it doesnt then ignore it. diff --git a/go.mod b/go.mod index 59736be..4ebf379 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( ) require ( + github.com/briandowns/openweathermap v0.19.0 github.com/dustin/go-humanize v1.0.1 github.com/golang-jwt/jwt v3.2.1+incompatible // indirect github.com/joho/godotenv v1.5.1 diff --git a/go.sum b/go.sum index 542a27e..6720907 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= +github.com/briandowns/openweathermap v0.19.0 h1:nkopLMEtZLxbZI1th6dOG6xkajpszofqf53r5K8mT9k= +github.com/briandowns/openweathermap v0.19.0/go.mod h1:0GLnknqicWxXnGi1IqoOaZIw+kIe5hkt+YM5WY3j8+0= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= 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= diff --git a/internal/commands/bttv.go b/internal/commands/bttv.go new file mode 100644 index 0000000..813faec --- /dev/null +++ b/internal/commands/bttv.go @@ -0,0 +1,11 @@ +package commands + +import ( + "fmt" +) + +func Bttv(query string) string { + reply := fmt.Sprintf("https://betterttv.com/emotes/shared/search?query=%s", query) + + return reply +} diff --git a/internal/commands/coinflip.go b/internal/commands/coinflip.go new file mode 100644 index 0000000..2765120 --- /dev/null +++ b/internal/commands/coinflip.go @@ -0,0 +1,21 @@ +package commands + +import ( + "github.com/lyx0/nourybot/internal/common" +) + +func Coinflip() string { + flip := common.GenerateRandomNumber(2) + var reply string + + switch flip { + case 0: + reply = "Heads!" + case 1: + reply = "Tails!" + default: + reply = "Heads!" + } + + return reply +} diff --git a/internal/commands/commands.go b/internal/commands/commands.go new file mode 100644 index 0000000..0483496 --- /dev/null +++ b/internal/commands/commands.go @@ -0,0 +1,8 @@ +package commands + +import "errors" + +var ( + ErrInternalServerError = errors.New("internal server error") + ErrWeatherLocationNotFound = errors.New("location not found") +) diff --git a/internal/commands/weather.go b/internal/commands/weather.go new file mode 100644 index 0000000..6f20017 --- /dev/null +++ b/internal/commands/weather.go @@ -0,0 +1,47 @@ +package commands + +import ( + "fmt" + "os" + + owm "github.com/briandowns/openweathermap" + "github.com/joho/godotenv" +) + +// Weather queries the OpenWeatherMap Api for the given location and sends the +// current weather response to the target twitch chat. +func Weather(location string) (string, error) { + err := godotenv.Load() + if err != nil { + return "", ErrInternalServerError + } + owmKey := os.Getenv("OWM_KEY") + + w, err := owm.NewCurrent("C", "en", owmKey) + if err != nil { + return "", ErrInternalServerError + } + + if err := w.CurrentByName(location); err != nil { + return "", ErrInternalServerError + } + + // Longitude and Latitude are returned as 0 when the supplied location couldn't be + // assigned to a OpenWeatherMap location. + if w.GeoPos.Longitude == 0 && w.GeoPos.Latitude == 0 { + return "", ErrWeatherLocationNotFound + } else { + // Weather for Vilnius, LT: Feels like: 29.67°C. Currently 29.49°C with a high of 29.84°C and a low of 29.49°C, humidity: 45%, wind: 6.17m/s. + 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, + ) + return reply, nil + } +} diff --git a/internal/common/random.go b/internal/common/random.go new file mode 100644 index 0000000..5228bef --- /dev/null +++ b/internal/common/random.go @@ -0,0 +1,38 @@ +package common + +import ( + "fmt" + "math/rand" + "strconv" + "time" +) + +// StrGenerateRandomNumber generates a random number from +// a given max value as a string +func StrGenerateRandomNumber(max string) int { + num, err := strconv.Atoi(max) + if num < 1 { + return 0 + } + + if err != nil { + fmt.Printf("Supplied value %v is not a number", num) + return 0 + } else { + rand.Seed(time.Now().UnixNano()) + return rand.Intn(num) + } +} + +// GenerateRandomNumber returns a random number from +// a given max value as a int +func GenerateRandomNumber(max int) int { + rand.Seed(time.Now().UnixNano()) + return rand.Intn(max) +} + +// GenerateRandomNumberRange returns a random number +// over a given minimum and maximum range. +func GenerateRandomNumberRange(min int, max int) int { + return (rand.Intn(max-min) + min) +}