mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add currency command
This commit is contained in:
parent
f33a7382ad
commit
b31468ae3c
|
@ -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 <username>", tc)
|
||||
common.Send(target, "Not enough arguments provided. Usage: ()tweet forsen", tc)
|
||||
return
|
||||
} else {
|
||||
commands.Tweet(target, cmdParams[1], tc)
|
||||
|
|
21
pkg/commands/currency.go
Normal file
21
pkg/commands/currency.go
Normal file
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
16
pkg/common/counter.go
Normal file
16
pkg/common/counter.go
Normal file
|
@ -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
|
||||
}
|
38
pkg/decapi/currency.go
Normal file
38
pkg/decapi/currency.go
Normal file
|
@ -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
|
||||
}
|
5
pkg/decapi/types.go
Normal file
5
pkg/decapi/types.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package decapi
|
||||
|
||||
var (
|
||||
twitterUserNotFoundError = "[Error] - [34] Sorry, that page does not exist."
|
||||
)
|
Loading…
Reference in a new issue