mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add firstline command
This commit is contained in:
parent
ada20061a7
commit
35d3e74cef
3 changed files with 75 additions and 0 deletions
47
pkg/api/ivr/firstline.go
Normal file
47
pkg/api/ivr/firstline.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package ivr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type firstLineApiResponse struct {
|
||||
User string `json:"user"`
|
||||
Message string `json:"message"`
|
||||
Time string `json:"time"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
var (
|
||||
firstLineBaseUrl = "https://api.ivr.fi/logs/firstmessage"
|
||||
)
|
||||
|
||||
func FirstLine(streamer string, username string) (string, error) {
|
||||
resp, err := http.Get(fmt.Sprintf("%s/%s/%s", firstLineBaseUrl, streamer, 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.Fatalln(err)
|
||||
}
|
||||
|
||||
var responseObject firstLineApiResponse
|
||||
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
|
||||
}
|
||||
|
||||
}
|
17
pkg/commands/firstline.go
Normal file
17
pkg/commands/firstline.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gempir/go-twitch-irc/v2"
|
||||
"github.com/lyx0/nourybot/pkg/api/ivr"
|
||||
)
|
||||
|
||||
func Firstline(channel string, streamer string, username string, client *twitch.Client) {
|
||||
ivrResponse, err := ivr.FirstLine(streamer, username)
|
||||
if err != nil {
|
||||
client.Say(channel, fmt.Sprint(err))
|
||||
return
|
||||
}
|
||||
client.Say(channel, ivrResponse)
|
||||
}
|
|
@ -83,6 +83,17 @@ func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, u
|
|||
commands.Fill(message.Channel, message.Message[7:len(message.Message)], twitchClient)
|
||||
return
|
||||
}
|
||||
case "firstline":
|
||||
if msgLen == 1 {
|
||||
twitchClient.Say(message.Channel, "Usage: ()firstline [channel] [user]")
|
||||
return
|
||||
} else if msgLen == 2 {
|
||||
commands.Firstline(message.Channel, message.Channel, cmdParams[1], twitchClient)
|
||||
return
|
||||
} else {
|
||||
commands.Firstline(message.Channel, cmdParams[1], cmdParams[2], twitchClient)
|
||||
return
|
||||
}
|
||||
case "game":
|
||||
if msgLen == 1 {
|
||||
twitchClient.Say(message.Channel, "Usage: ()game [channel]")
|
||||
|
|
Loading…
Reference in a new issue