From 9927aa2724d00461879a25abac82b212f7022835 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Thu, 11 Jan 2024 02:33:25 +0100 Subject: [PATCH] add currency/preview/weather command --- cmd/bot/commands.go | 40 ++++++++++++++++++++++++++------ pkg/commands/currency.go | 30 ++++++++++++++++++++++++ pkg/commands/errors.go | 8 +++++++ pkg/commands/preview.go | 15 ++++++++++++ pkg/{owm => commands}/weather.go | 8 +------ pkg/common/random.go | 35 ++++++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 14 deletions(-) create mode 100644 pkg/commands/currency.go create mode 100644 pkg/commands/errors.go create mode 100644 pkg/commands/preview.go rename pkg/{owm => commands}/weather.go (88%) create mode 100644 pkg/common/random.go diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go index bfd002d..89fd5ec 100644 --- a/cmd/bot/commands.go +++ b/cmd/bot/commands.go @@ -3,7 +3,7 @@ package main import ( "strings" - "github.com/lyx0/nourybot-matrix/pkg/owm" + "github.com/lyx0/nourybot-matrix/pkg/commands" "maunium.net/go/mautrix/event" ) @@ -21,7 +21,7 @@ func (app *application) ParseCommand(evt *event.Event) { // msgLen is the amount of words in a message without the prefix. // Useful to check if enough cmdParams are provided. - //msgLen := len(strings.SplitN(evt.Content.AsMessage().Body, " ", -2)) + msgLen := len(strings.SplitN(evt.Content.AsMessage().Body, " ", -1)) app.Log.Info().Msgf("Command: %s", evt.Content.AsMessage().Body) @@ -30,16 +30,42 @@ func (app *application) ParseCommand(evt *event.Event) { case "xd": app.SendText(evt, "xd !") return + + case "currency": + if msgLen <= 4 { + reply = "Not enough arguments provided. Usage: ()currency 10 USD to EUR" + } else { + reply, _ = commands.Currency(cmdParams[1], cmdParams[2], cmdParams[4]) + } case "gofile": - app.NewDownload("gofile", evt, cmdParams[1]) - return + if msgLen < 2 { + reply = "Not enough arguments provided. Usage: !gofile [link]" + } else { + app.NewDownload("gofile", evt, cmdParams[1]) + return + } + + case "preview": + if msgLen < 2 { + reply = "Not enough arguments provided. Usage: !preview [stream]" + } else { + reply = commands.Preview(cmdParams[1]) + } case "weather": - reply, _ = owm.Weather(evt.Content.AsMessage().Body[9:len(evt.Content.AsMessage().Body)]) + if msgLen < 2 { + reply = "Not enough arguments provided. Usage: !weather [location]" + } else { + reply, _ = commands.Weather(evt.Content.AsMessage().Body[9:len(evt.Content.AsMessage().Body)]) + } case "yaf": - app.NewDownload("yaf", evt, cmdParams[1]) - return + if msgLen < 2 { + reply = "Not enough arguments provided. Usage: !gofile [link]" + } else { + app.NewDownload("yaf", evt, cmdParams[1]) + return + } } if reply != "" { diff --git a/pkg/commands/currency.go b/pkg/commands/currency.go new file mode 100644 index 0000000..f6ca31e --- /dev/null +++ b/pkg/commands/currency.go @@ -0,0 +1,30 @@ +package commands + +import ( + "fmt" + "io" + "net/http" +) + +func Currency(currAmount, currFrom, currTo string) (string, error) { + basePath := "https://decapi.me/misc/currency/" + from := fmt.Sprintf("?from=%s", currFrom) + to := fmt.Sprintf("&to=%s", currTo) + value := fmt.Sprintf("&value=%s", currAmount) + + // https://decapi.me/misc/currency/?from=usd&to=usd&value=10 + resp, err := http.Get(fmt.Sprint(basePath + from + to + value)) + if err != nil { + return "", ErrInternalServerError + } + + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", ErrInternalServerError + } + + reply := string(body) + return reply, nil +} diff --git a/pkg/commands/errors.go b/pkg/commands/errors.go new file mode 100644 index 0000000..0483496 --- /dev/null +++ b/pkg/commands/errors.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/pkg/commands/preview.go b/pkg/commands/preview.go new file mode 100644 index 0000000..c5699eb --- /dev/null +++ b/pkg/commands/preview.go @@ -0,0 +1,15 @@ +package commands + +import ( + "fmt" + + "github.com/lyx0/nourybot-matrix/pkg/common" +) + +func Preview(channel string) string { + imageHeight := common.GenerateRandomNumberRange(1040, 1080) + imageWidth := common.GenerateRandomNumberRange(1890, 1920) + + reply := fmt.Sprintf("https://static-cdn.jtvnw.net/previews-ttv/live_user_%s-%vx%v.jpg", channel, imageWidth, imageHeight) + return reply +} diff --git a/pkg/owm/weather.go b/pkg/commands/weather.go similarity index 88% rename from pkg/owm/weather.go rename to pkg/commands/weather.go index f241936..6f20017 100644 --- a/pkg/owm/weather.go +++ b/pkg/commands/weather.go @@ -1,7 +1,6 @@ -package owm +package commands import ( - "errors" "fmt" "os" @@ -9,11 +8,6 @@ import ( "github.com/joho/godotenv" ) -var ( - ErrInternalServerError = errors.New("internal server error") - ErrWeatherLocationNotFound = errors.New("location not found") -) - // 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) { diff --git a/pkg/common/random.go b/pkg/common/random.go new file mode 100644 index 0000000..c56b1ca --- /dev/null +++ b/pkg/common/random.go @@ -0,0 +1,35 @@ +package common + +import ( + "fmt" + "math/rand" + "strconv" +) + +// 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 { + return rand.Intn(num) + } +} + +// GenerateRandomNumber returns a random number from +// a given max value as a int +func GenerateRandomNumber(max int) int { + 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) +}