mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add randomquote command
This commit is contained in:
parent
8e9d634df8
commit
257c61b650
49
pkg/api/ivr/randomquote.go
Normal file
49
pkg/api/ivr/randomquote.go
Normal 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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
21
pkg/commands/randomquote.go
Normal file
21
pkg/commands/randomquote.go
Normal 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)
|
||||||
|
}
|
|
@ -241,6 +241,29 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) {
|
||||||
commands.RandomFox(target, nb)
|
commands.RandomFox(target, nb)
|
||||||
return
|
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":
|
case "randomxkcd":
|
||||||
commands.RandomXkcd(target, nb)
|
commands.RandomXkcd(target, nb)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue