clean up currency command

This commit is contained in:
lyx0 2021-10-23 20:40:40 +02:00
parent 5c0545e732
commit 9a4a3d2874
2 changed files with 12 additions and 6 deletions

View file

@ -10,14 +10,13 @@ import (
log "github.com/sirupsen/logrus"
)
// reply, err := api.Currency(currAmount, currFrom, currTo)
type currencyResponse struct {
Amount int `json:"amount"`
Base string `json:"base"`
Rates map[string]float32 `json:"rates"`
}
// Idk if i ever need it again so it stays for now
// type rates map[string]interface{}
// type rates struct {
// AUD float32 `json:"AUD"`
@ -40,10 +39,12 @@ type currencyResponse struct {
// https://api.frankfurter.app/latest?amount=10&from=gbp&to=usd
// Really messy code but works right now
func Currency(currAmount string, currFrom string, currTo string) (string, error) {
baseUrl := "https://api.frankfurter.app"
response, err := http.Get(fmt.Sprintf("%s/latest?amount=%s&from=%s&to=%s", baseUrl, currAmount, strings.ToUpper(currFrom), strings.ToUpper(currTo)))
currFromUpper := strings.ToUpper(currFrom)
currToUpper := strings.ToUpper(currTo)
response, err := http.Get(fmt.Sprintf("%s/latest?amount=%s&from=%s&to=%s", baseUrl, currAmount, currFromUpper, currToUpper))
if err != nil {
log.Error(err)
}
@ -59,8 +60,9 @@ func Currency(currAmount string, currFrom string, currTo string) (string, error)
// log.Info(responseObject.Amount)
// log.Info(responseObject.Base)
// log.Info(responseObject.Rates[strings.ToUpper(currTo)])
if responseObject.Rates[strings.ToUpper(currTo)] != 0 {
return fmt.Sprintf("%s%s are %v%s", currAmount, currFrom, responseObject.Rates[strings.ToUpper(currTo)], currTo), nil
if responseObject.Rates[currToUpper] != 0 {
return fmt.Sprintf("%s %s = %.2f %s", currAmount, currFromUpper, responseObject.Rates[currToUpper], currToUpper), nil
}
return "Something went wrong FeelsBadMan", nil
}

View file

@ -72,6 +72,10 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) {
// ()currency 10 gbp to usd
case "currency":
if msgLen <= 4 {
nb.Send(target, "Usage: ()currency 10 usd to eur, only 3 letter codes work.")
return
}
commands.Currency(target, cmdParams[1], cmdParams[2], cmdParams[4], nb)
case "echo":
commands.Echo(target, message.Message[7:len(message.Message)], nb)