From b31468ae3c630f92c86c6f6a5ecb9ff0733fb79b Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sun, 7 Aug 2022 17:24:47 +0200 Subject: [PATCH] add currency command --- cmd/bot/command.go | 11 ++++++++++- pkg/commands/currency.go | 21 +++++++++++++++++++++ pkg/commands/ping.go | 3 ++- pkg/common/counter.go | 16 ++++++++++++++++ pkg/decapi/currency.go | 38 ++++++++++++++++++++++++++++++++++++++ pkg/decapi/types.go | 5 +++++ 6 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 pkg/commands/currency.go create mode 100644 pkg/common/counter.go create mode 100644 pkg/decapi/currency.go create mode 100644 pkg/decapi/types.go diff --git a/cmd/bot/command.go b/cmd/bot/command.go index 8404393..f9e752b 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -13,6 +13,9 @@ func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) { sugar := zap.NewExample().Sugar() defer sugar.Sync() + // Increments the counter how many commands were used. + common.CommandUsed() + // commandName is the actual name of the command without the prefix. // e.g. `()ping` would be `ping`. commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:]) @@ -50,6 +53,12 @@ func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) { common.Send(target, "xd", tc) return } + case "currency": + if msgLen < 4 { + common.Send(target, "Not enough arguments provided. Usage: ()currency 10 USD to EUR", tc) + return + } + commands.Currency(target, cmdParams[1], cmdParams[2], cmdParams[4], tc) case "echo": if msgLen < 2 { common.Send(target, "Not enough arguments provided.", tc) @@ -60,7 +69,7 @@ func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) { } case "tweet": if msgLen < 2 { - common.Send(target, "Not enough arguments provided. Usage: ()tweet ", tc) + common.Send(target, "Not enough arguments provided. Usage: ()tweet forsen", tc) return } else { commands.Tweet(target, cmdParams[1], tc) diff --git a/pkg/commands/currency.go b/pkg/commands/currency.go new file mode 100644 index 0000000..9142f22 --- /dev/null +++ b/pkg/commands/currency.go @@ -0,0 +1,21 @@ +package commands + +import ( + "github.com/gempir/go-twitch-irc/v3" + "github.com/lyx0/nourybot/pkg/common" + "github.com/lyx0/nourybot/pkg/decapi" + "go.uber.org/zap" +) + +// ()currency 10 USD to EUR +func Currency(target, currAmount, currFrom, currTo string, tc *twitch.Client) { + sugar := zap.NewExample().Sugar() + defer sugar.Sync() + + resp, err := decapi.Currency(currAmount, currFrom, currTo) + if err != nil { + sugar.Error(err) + } + + common.Send(target, resp, tc) +} diff --git a/pkg/commands/ping.go b/pkg/commands/ping.go index 5092459..6df60d8 100644 --- a/pkg/commands/ping.go +++ b/pkg/commands/ping.go @@ -10,7 +10,8 @@ import ( func Ping(target string, tc *twitch.Client) { botUptime := humanize.Time(common.GetUptime()) + commandsUsed := common.GetCommandsUsed() - reply := fmt.Sprintf("Pong! :) Last restart: %v", botUptime) + reply := fmt.Sprintf("Pong! :) Commands used: %v, Last restart: %v", commandsUsed, botUptime) common.Send(target, reply, tc) } diff --git a/pkg/common/counter.go b/pkg/common/counter.go new file mode 100644 index 0000000..28451bc --- /dev/null +++ b/pkg/common/counter.go @@ -0,0 +1,16 @@ +package common + +var ( + tempCommands = 0 +) + +// CommandUsed is called on every command incremenenting tempCommands. +func CommandUsed() { + tempCommands++ +} + +// GetCommandsUsed returns the amount of commands that have been used +// since the last restart. +func GetCommandsUsed() int { + return tempCommands +} diff --git a/pkg/decapi/currency.go b/pkg/decapi/currency.go new file mode 100644 index 0000000..3308bf6 --- /dev/null +++ b/pkg/decapi/currency.go @@ -0,0 +1,38 @@ +package decapi + +import ( + "fmt" + "io" + "net/http" + + "go.uber.org/zap" +) + +// ()currency 10 USD to EUR +func Currency(currAmount, currFrom, currTo string) (string, error) { + sugar := zap.NewExample().Sugar() + defer sugar.Sync() + + 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 { + sugar.Error(err) + return "Something went wrong FeelsBadMan", err + } + + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + sugar.Error(err) + return "Something went wrong FeelsBadMan", err + } + + reply := string(body) + return reply, nil +} diff --git a/pkg/decapi/types.go b/pkg/decapi/types.go new file mode 100644 index 0000000..74752c7 --- /dev/null +++ b/pkg/decapi/types.go @@ -0,0 +1,5 @@ +package decapi + +var ( + twitterUserNotFoundError = "[Error] - [34] Sorry, that page does not exist." +)