add a bunch of commands

This commit is contained in:
lyx0 2021-10-19 22:58:09 +02:00
parent 8c0b51894f
commit 88aa450b47
7 changed files with 152 additions and 0 deletions

30
pkg/api/aiden/aiden.go Normal file
View file

@ -0,0 +1,30 @@
package aiden
import (
"fmt"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
)
var (
basePath = "https://customapi.aidenwallis.co.uk/"
)
func ApiCall(uri string) (string, error) {
resp, err := http.Get(fmt.Sprint(basePath + uri))
if err != nil {
log.Error(err)
return "Something went wrong FeelsBadMan", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error(err)
return "Something went wrong FeelsBadMan", err
}
return string(body), nil
}

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

@ -0,0 +1,35 @@
package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/lyx0/nourybot/pkg/utils"
log "github.com/sirupsen/logrus"
)
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.Error(err)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Error(err)
}
var responseObject XkcdResponse
json.Unmarshal(responseData, &responseObject)
reply := fmt.Sprint("Random Xkcd #", responseObject.Num, " Title: ", responseObject.SafeTitle, " ", responseObject.Img)
return reply
}

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

@ -0,0 +1,27 @@
package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
)
func Xkcd() string {
response, err := http.Get("https://xkcd.com/info.0.json")
if err != nil {
log.Error(err)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Error(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/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api"
)
func RandomXkcd(channel string, nb *bot.Bot) {
reply := api.RandomXkcd()
nb.Send(channel, reply)
}

20
pkg/commands/weather.go Normal file
View file

@ -0,0 +1,20 @@
package commands
import (
"fmt"
"github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/aiden"
log "github.com/sirupsen/logrus"
)
func Weather(channel string, location string, nb *bot.Bot) {
reply, err := aiden.ApiCall(fmt.Sprintf("api/v1/misc/weather/%s", location))
if err != nil {
nb.Send(channel, "Something went wrong FeelsBadMan")
log.Error(err)
}
nb.Send(channel, reply)
}

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

@ -0,0 +1,13 @@
package commands
import (
"github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api"
)
func Xkcd(channel string, nb *bot.Bot) {
reply := api.Xkcd()
nb.Send(channel, reply)
}

View file

@ -36,8 +36,24 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) {
nb.Send(message.Channel, cmdParams[1]) nb.Send(message.Channel, cmdParams[1])
return return
} }
case "randomxkcd":
commands.RandomXkcd(message.Channel, nb)
return
case "ping": case "ping":
commands.Ping(message.Channel, nb) commands.Ping(message.Channel, nb)
return
case "weather":
if msgLen == 1 {
nb.Send(message.Channel, "Usage: ()weather [location]")
return
} else {
commands.Weather(message.Channel, message.Message[9:len(message.Message)], nb)
return
}
case "xkcd":
commands.Xkcd(message.Channel, nb)
return
} }
} }