add numbers command

This commit is contained in:
lyx0 2021-10-17 21:37:45 +02:00
parent ee25eeede4
commit cdc57f55c4
3 changed files with 63 additions and 0 deletions

41
pkg/api/numbersapi.go Normal file
View file

@ -0,0 +1,41 @@
package api
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
log "github.com/sirupsen/logrus"
)
func RandomNumber() string {
response, err := http.Get("http://numbersapi.com/random/trivia")
if err != nil {
log.Fatalln(err)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalln(err)
}
return string(responseData)
}
func Number(num string) string {
number, err := strconv.Atoi(num)
if err != nil {
return "Given value is not a number."
} else {
response, err := http.Get(fmt.Sprint("http://numbersapi.com/" + string(number)))
if err != nil {
log.Fatalln(err)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalln(err)
}
return string(responseData)
}
}

16
pkg/commands/number.go Normal file
View file

@ -0,0 +1,16 @@
package commands
import (
"github.com/gempir/go-twitch-irc/v2"
"github.com/lyx0/nourybot/pkg/api"
)
func RandomNumber(channel string, client *twitch.Client) {
reply := api.RandomNumber()
client.Say(channel, string(reply))
}
func Number(channel string, number string, client *twitch.Client) {
reply := api.Number(number)
client.Say(channel, string(reply))
}

View file

@ -136,6 +136,12 @@ func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, u
commands.Godocs(message.Channel, message.Message[9:len(message.Message)], twitchClient)
return
}
case "number":
if msgLen == 1 {
commands.RandomNumber(message.Channel, twitchClient)
} else {
commands.Number(message.Channel, cmdParams[1], twitchClient)
}
case "ping":
commands.Ping(message.Channel, twitchClient, uptime)
return