add randomxkcd and xkcd command

This commit is contained in:
lyx0 2021-10-18 14:57:44 +02:00
parent ef89fadb9b
commit 1433efa100
5 changed files with 90 additions and 1 deletions

35
pkg/api/randomxkcd.go Normal file
View file

@ -0,0 +1,35 @@
package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/lyx0/nourybot/pkg/utils"
)
type XkcdResponse struct {
Num int `json:"num"`
SafeTitle string `json:"safe_title"`
Img string `json:"img"`
}
func RandomXkcd() string {
comicNum := fmt.Sprint(utils.GenerateRandomNumber(2468))
response, err := http.Get(fmt.Sprint("http://xkcd.com/" + comicNum + "/info.0.json"))
if err != nil {
log.Fatalln(err)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalln(err)
}
var responseObject XkcdResponse
json.Unmarshal(responseData, &responseObject)
reply := fmt.Sprint("Random Xkcd #", responseObject.Num, " Title: ", responseObject.SafeTitle, " ", responseObject.Img)
return reply
}

26
pkg/api/xkcd.go Normal file
View file

@ -0,0 +1,26 @@
package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func Xkcd() string {
response, err := http.Get("https://xkcd.com/info.0.json")
if err != nil {
log.Fatalln(err)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalln(err)
}
var responseObject XkcdResponse
json.Unmarshal(responseData, &responseObject)
reply := fmt.Sprint("Current Xkcd #", responseObject.Num, " Title: ", responseObject.SafeTitle, " ", responseObject.Img)
return reply
}

View file

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

12
pkg/commands/xkcd.go Normal file
View file

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

View file

@ -168,6 +168,9 @@ func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, u
case "randomfox":
commands.RandomFox(message.Channel, twitchClient)
return
case "randomxkcd":
commands.RandomXkcd(message.Channel, twitchClient)
return
case "subage":
if msgLen < 3 {
twitchClient.Say(message.Channel, "Usage: ()subage [user] [streamer]")
@ -212,6 +215,8 @@ func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, u
commands.Xd(message.Channel, twitchClient)
return
case "xkcd":
commands.Xkcd(message.Channel, twitchClient)
return
}
}