add randomquote command

This commit is contained in:
lyx0 2021-10-22 17:01:36 +02:00
parent 8e9d634df8
commit 257c61b650
3 changed files with 93 additions and 0 deletions

View file

@ -0,0 +1,49 @@
package ivr
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
)
type randomQuoteApiResponse struct {
User string `json:"user"`
Message string `json:"message"`
Time string `json:"time"`
Error string `json:"Error"`
}
var (
randomQuoteBaseUrl = "https://api.ivr.fi/logs/rq"
)
// FirstLine returns the first line a given user has sent in a
// given channel.
func RandomQuote(channel string, username string) (string, error) {
resp, err := http.Get(fmt.Sprintf("%s/%s/%s", randomQuoteBaseUrl, channel, username))
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)
}
var responseObject randomQuoteApiResponse
json.Unmarshal(body, &responseObject)
// User or channel was not found
if responseObject.Error != "" {
return fmt.Sprintf(responseObject.Error + " FeelsBadMan"), nil
} else {
return fmt.Sprintf(username + ": " + responseObject.Message + " (" + responseObject.Time + " ago)."), nil
}
}

View file

@ -0,0 +1,21 @@
package commands
import (
"fmt"
"github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/ivr"
"github.com/sirupsen/logrus"
)
func RandomQuote(channel string, target string, username string, nb *bot.Bot) {
ivrResponse, err := ivr.RandomQuote(target, username)
if err != nil {
nb.Send(channel, fmt.Sprint(err))
return
}
logrus.Info(ivrResponse)
nb.Send(channel, ivrResponse)
}

View file

@ -241,6 +241,29 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) {
commands.RandomFox(target, nb)
return
case "rq":
if msgLen == 1 {
nb.Send(target, "Usage: ()rq [channel] [user]")
return
} else if msgLen == 2 {
commands.RandomQuote(target, target, cmdParams[1], nb)
return
} else {
commands.RandomQuote(target, cmdParams[1], cmdParams[2], nb)
return
}
case "randomquote":
if msgLen == 1 {
nb.Send(target, "Usage: ()randomquote [channel] [user]")
return
} else if msgLen == 2 {
commands.RandomQuote(target, target, cmdParams[1], nb)
return
} else {
commands.RandomQuote(target, cmdParams[1], cmdParams[2], nb)
return
}
case "randomxkcd":
commands.RandomXkcd(target, nb)
return