add more commands

This commit is contained in:
lyx0 2023-09-07 21:09:33 +02:00
parent 56d7a1bc1d
commit ec86f396bd
4 changed files with 199 additions and 0 deletions

View file

@ -73,9 +73,23 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
case "cf": case "cf":
reply = commands.Coinflip() reply = commands.Coinflip()
// ()currency <amount> <input currency> to <output currency>
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 "nourybot": case "nourybot":
reply = "Lidl Twitch bot made by @nourylul. Prefix: ()" reply = "Lidl Twitch bot made by @nourylul. Prefix: ()"
case "phonetic":
if msgLen == 1 {
reply = "Not enough arguments provided. Usage: ()phonetic <text to translate>"
} else {
reply, _ = commands.Phonetic(message.Message[11:len(message.Message)])
}
case "ping": case "ping":
reply = commands.Ping() reply = commands.Ping()
// ()bttv <emote name> // ()bttv <emote name>
@ -90,6 +104,16 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
reply, _ = commands.Weather(message.Message[10:len(message.Message)]) reply, _ = commands.Weather(message.Message[10:len(message.Message)])
} }
// Xkcd
// Random Xkcd
case "rxkcd":
reply, _ = commands.RandomXkcd()
case "randomxkcd":
reply, _ = commands.RandomXkcd()
// Latest Xkcd
case "xkcd":
reply, _ = commands.Xkcd()
// ################## // ##################
// Check if the commandName exists as the "name" of a command in the database. // Check if the commandName exists as the "name" of a command in the database.
// if it doesnt then ignore it. // if it doesnt then ignore it.

View file

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

View file

@ -0,0 +1,89 @@
package commands
import "fmt"
var cm = map[string]string{
"`": "ё",
"~": "Ё",
"=": "ъ",
"+": "Ъ",
"[": "ю",
"]": "щ",
`\`: "э",
"{": "Ю",
"}": "Щ",
"|": "Э",
";": "ь",
":": "Ь",
"'": "ж",
`"`: "Ж",
"q": "я",
"w": "ш",
"e": "е",
"r": "р",
"t": "т",
"y": "ы",
"u": "у",
"i": "и",
"o": "о",
"p": "п",
"a": "а",
"s": "с",
"d": "д",
"f": "ф",
"g": "г",
"h": "ч",
"j": "й",
"k": "к",
"l": "л",
"z": "з",
"x": "х",
"c": "ц",
"v": "в",
"b": "б",
"n": "н",
"m": "м",
"Q": "Я",
"W": "Ш",
"E": "Е",
"R": "Р",
"T": "Т",
"Y": "Ы",
"U": "У",
"I": "И",
"O": "О",
"P": "П",
"A": "А",
"S": "С",
"D": "Д",
"F": "Ф",
"G": "Г",
"H": "Ч",
"J": "Й",
"K": "К",
"L": "Л",
"Z": "З",
"X": "Х",
"C": "Ц",
"V": "В",
"B": "Б",
"N": "Н",
"M": "М",
}
func Phonetic(message string) (string, error) {
var ts string
for _, c := range message {
if _, ok := cm[string(c)]; ok {
ts = ts + cm[string(c)]
} else {
ts = ts + string(c)
}
}
return fmt.Sprint(ts), nil
}

56
internal/commands/xkcd.go Normal file
View file

@ -0,0 +1,56 @@
package commands
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/lyx0/nourybot/internal/common"
)
type xkcdResponse struct {
Num int `json:"num"`
SafeTitle string `json:"safe_title"`
Img string `json:"img"`
}
func Xkcd() (string, error) {
response, err := http.Get("https://xkcd.com/info.0.json")
if err != nil {
return "", ErrInternalServerError
}
responseData, err := io.ReadAll(response.Body)
if err != nil {
return "", ErrInternalServerError
}
var responseObject xkcdResponse
if err = json.Unmarshal(responseData, &responseObject); err != nil {
return "", ErrInternalServerError
}
reply := fmt.Sprint("Current Xkcd #", responseObject.Num, " Title: ", responseObject.SafeTitle, " ", responseObject.Img)
return reply, nil
}
func RandomXkcd() (string, error) {
comicNum := fmt.Sprint(common.GenerateRandomNumber(2772))
response, err := http.Get(fmt.Sprint("http://xkcd.com/" + comicNum + "/info.0.json"))
if err != nil {
return "", ErrInternalServerError
}
responseData, err := io.ReadAll(response.Body)
if err != nil {
return "", ErrInternalServerError
}
var responseObject xkcdResponse
if err = json.Unmarshal(responseData, &responseObject); err != nil {
return "", ErrInternalServerError
}
reply := fmt.Sprint("Random Xkcd #", responseObject.Num, " Title: ", responseObject.SafeTitle, " ", responseObject.Img)
return reply, nil
}