mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add numbers command
This commit is contained in:
parent
ee25eeede4
commit
cdc57f55c4
3 changed files with 63 additions and 0 deletions
41
pkg/api/numbersapi.go
Normal file
41
pkg/api/numbersapi.go
Normal 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
16
pkg/commands/number.go
Normal 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))
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue