implement a few commands

This commit is contained in:
lyx0 2023-09-07 20:43:16 +02:00
parent b2d7860cc2
commit 56d7a1bc1d
8 changed files with 153 additions and 0 deletions

View file

@ -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 <emote name>"
} 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 <emote name>
// ()weather <location>
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.

1
go.mod
View file

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

2
go.sum
View file

@ -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=

11
internal/commands/bttv.go Normal file
View file

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

View file

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

View file

@ -0,0 +1,8 @@
package commands
import "errors"
var (
ErrInternalServerError = errors.New("internal server error")
ErrWeatherLocationNotFound = errors.New("location not found")
)

View file

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

38
internal/common/random.go Normal file
View file

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