diff --git a/pkg/api/randomxkcd.go b/pkg/api/randomxkcd.go new file mode 100644 index 0000000..eb96831 --- /dev/null +++ b/pkg/api/randomxkcd.go @@ -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 +} diff --git a/pkg/api/xkcd.go b/pkg/api/xkcd.go new file mode 100644 index 0000000..5dd88bc --- /dev/null +++ b/pkg/api/xkcd.go @@ -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 +} diff --git a/pkg/commands/randomxkcd.go b/pkg/commands/randomxkcd.go new file mode 100644 index 0000000..0dffb1a --- /dev/null +++ b/pkg/commands/randomxkcd.go @@ -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) +} diff --git a/pkg/commands/xkcd.go b/pkg/commands/xkcd.go new file mode 100644 index 0000000..646dd91 --- /dev/null +++ b/pkg/commands/xkcd.go @@ -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) + +} diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index 68dcf39..f5e1a84 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -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 } - }