From 7bc69ff5e065e83b7e346e60c310cb9c36291eff Mon Sep 17 00:00:00 2001 From: lyx0 Date: Tue, 19 Oct 2021 23:27:55 +0200 Subject: [PATCH] add more commands --- cmd/main.go | 3 +- pkg/api/ivr/firstline.go | 47 ++++++++++++++++++ pkg/api/ivr/followage.go | 47 ++++++++++++++++++ pkg/commands/botstatus.go | 19 ++++++++ pkg/commands/bttvemotes.go | 19 ++++++++ pkg/commands/coinflip.go | 16 +++++++ pkg/commands/color.go | 15 ++++++ pkg/commands/echo.go | 7 +++ pkg/commands/eightball.go | 17 +++++++ pkg/commands/ffzemotes.go | 20 ++++++++ pkg/commands/fill.go | 25 ++++++++++ pkg/commands/firstline.go | 17 +++++++ pkg/commands/followage.go | 16 +++++++ pkg/commands/game.go | 20 ++++++++ pkg/handlers/command.go | 98 ++++++++++++++++++++++++++++++++++---- 15 files changed, 376 insertions(+), 10 deletions(-) create mode 100644 pkg/api/ivr/firstline.go create mode 100644 pkg/api/ivr/followage.go create mode 100644 pkg/commands/botstatus.go create mode 100644 pkg/commands/bttvemotes.go create mode 100644 pkg/commands/coinflip.go create mode 100644 pkg/commands/color.go create mode 100644 pkg/commands/echo.go create mode 100644 pkg/commands/eightball.go create mode 100644 pkg/commands/ffzemotes.go create mode 100644 pkg/commands/fill.go create mode 100644 pkg/commands/firstline.go create mode 100644 pkg/commands/followage.go create mode 100644 pkg/commands/game.go diff --git a/cmd/main.go b/cmd/main.go index 47cabbc..4839b99 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -36,6 +36,7 @@ func main() { handlers.PrivateMessage(message, nb) }) - nb.TwitchClient.Join("nourybot") + nb.TwitchClient.Join("nouryqt", "nourybot") + nb.Send("nourybot", "HeyGuys") nb.TwitchClient.Connect() } diff --git a/pkg/api/ivr/firstline.go b/pkg/api/ivr/firstline.go new file mode 100644 index 0000000..3f2ae15 --- /dev/null +++ b/pkg/api/ivr/firstline.go @@ -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.Error(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 + } + +} diff --git a/pkg/api/ivr/followage.go b/pkg/api/ivr/followage.go new file mode 100644 index 0000000..2ab1ea9 --- /dev/null +++ b/pkg/api/ivr/followage.go @@ -0,0 +1,47 @@ +package ivr + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + + log "github.com/sirupsen/logrus" +) + +// https://api.ivr.fi +type followageApiResponse struct { + User string `json:"user"` + UserID string `json:"userid"` + Channel string `json:"channel"` + ChannelId string `json:"channelid"` + FollowedAt string `json:"followedAt"` + Error string `json:"error"` +} + +func Followage(streamer string, username string) (string, error) { + resp, err := http.Get(fmt.Sprintf("https://api.ivr.fi/twitch/subage/%s/%s", username, streamer)) + if err != nil { + log.Error(err) + } + + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Error(err) + } + + var responseObject followageApiResponse + json.Unmarshal(body, &responseObject) + + // User or channel was not found + if responseObject.Error != "" { + return fmt.Sprintf(responseObject.Error + " FeelsBadMan"), nil + } else if responseObject.FollowedAt == "" { + return fmt.Sprintf(username + " is not following " + streamer), nil + } else { + d := responseObject.FollowedAt[:10] + return fmt.Sprintf(username + " has been following " + streamer + " since " + d + "."), nil + } +} diff --git a/pkg/commands/botstatus.go b/pkg/commands/botstatus.go new file mode 100644 index 0000000..2660eec --- /dev/null +++ b/pkg/commands/botstatus.go @@ -0,0 +1,19 @@ +package commands + +import ( + "fmt" + + "github.com/lyx0/nourybot/cmd/bot" + "github.com/lyx0/nourybot/pkg/api/aiden" + log "github.com/sirupsen/logrus" +) + +func BotStatus(channel string, name string, nb *bot.Bot) { + resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/botStatus/%s?includeLimits=1", name)) + if err != nil { + nb.Send(channel, "Something went wrong FeelsBadMan") + log.Error(err) + } + + nb.Send(channel, resp) +} diff --git a/pkg/commands/bttvemotes.go b/pkg/commands/bttvemotes.go new file mode 100644 index 0000000..94b1d93 --- /dev/null +++ b/pkg/commands/bttvemotes.go @@ -0,0 +1,19 @@ +package commands + +import ( + "fmt" + + "github.com/lyx0/nourybot/cmd/bot" + "github.com/lyx0/nourybot/pkg/api/aiden" + log "github.com/sirupsen/logrus" +) + +func BttvEmotes(channel string, nb *bot.Bot) { + resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/emotes/%s/bttv", channel)) + if err != nil { + log.Error(err) + nb.Send(channel, "Something went wrong FeelsBadMan") + } + + nb.Send(channel, string(resp)) +} diff --git a/pkg/commands/coinflip.go b/pkg/commands/coinflip.go new file mode 100644 index 0000000..63c8aca --- /dev/null +++ b/pkg/commands/coinflip.go @@ -0,0 +1,16 @@ +package commands + +import ( + "github.com/lyx0/nourybot/cmd/bot" + "github.com/lyx0/nourybot/pkg/utils" +) + +func Coinflip(channel string, nb *bot.Bot) { + result := utils.GenerateRandomNumber(2) + + if result == 1 { + nb.Send(channel, "Heads") + } else { + nb.Send(channel, "Tails") + } +} diff --git a/pkg/commands/color.go b/pkg/commands/color.go new file mode 100644 index 0000000..155acd6 --- /dev/null +++ b/pkg/commands/color.go @@ -0,0 +1,15 @@ +package commands + +import ( + "fmt" + + "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" +) + +func Color(message twitch.PrivateMessage, nb *bot.Bot) { + reply := fmt.Sprintf("@%v, your color is %v", message.User.DisplayName, message.User.Color) + + nb.Send(message.Channel, reply) + +} diff --git a/pkg/commands/echo.go b/pkg/commands/echo.go new file mode 100644 index 0000000..ddd9917 --- /dev/null +++ b/pkg/commands/echo.go @@ -0,0 +1,7 @@ +package commands + +import "github.com/lyx0/nourybot/cmd/bot" + +func Echo(channel string, message string, nb *bot.Bot) { + nb.Send(channel, message) +} diff --git a/pkg/commands/eightball.go b/pkg/commands/eightball.go new file mode 100644 index 0000000..751d979 --- /dev/null +++ b/pkg/commands/eightball.go @@ -0,0 +1,17 @@ +package commands + +import ( + "github.com/lyx0/nourybot/cmd/bot" + "github.com/lyx0/nourybot/pkg/api/aiden" + log "github.com/sirupsen/logrus" +) + +func EightBall(channel string, nb *bot.Bot) { + resp, err := aiden.ApiCall("api/v1/misc/8ball") + if err != nil { + log.Error(err) + nb.Send(channel, "Something went wrong FeelsBadMan") + } + + nb.Send(channel, string(resp)) +} diff --git a/pkg/commands/ffzemotes.go b/pkg/commands/ffzemotes.go new file mode 100644 index 0000000..14f1e7d --- /dev/null +++ b/pkg/commands/ffzemotes.go @@ -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 FfzEmotes(channel string, nb *bot.Bot) { + + resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/emotes/%s/ffz", channel)) + if err != nil { + log.Error(err) + nb.Send(channel, "Something went wrong FeelsBadMan") + } + + nb.Send(channel, string(resp)) +} diff --git a/pkg/commands/fill.go b/pkg/commands/fill.go new file mode 100644 index 0000000..36b2663 --- /dev/null +++ b/pkg/commands/fill.go @@ -0,0 +1,25 @@ +package commands + +import ( + "fmt" + "strings" + + "github.com/lyx0/nourybot/cmd/bot" +) + +func Fill(channel string, emote string, nb *bot.Bot) { + if emote[0] == '.' || emote[0] == '/' { + nb.Send(channel, ":tf:") + return + } + + // Get the length of the emote + emoteLength := (len(emote) + 1) + + // Check how often the emote fits into a single message + repeatCount := (499 / emoteLength) + + reply := strings.Repeat(fmt.Sprintf(emote+" "), repeatCount) + nb.Send(channel, reply) + +} diff --git a/pkg/commands/firstline.go b/pkg/commands/firstline.go new file mode 100644 index 0000000..eed2fe7 --- /dev/null +++ b/pkg/commands/firstline.go @@ -0,0 +1,17 @@ +package commands + +import ( + "fmt" + + "github.com/lyx0/nourybot/cmd/bot" + "github.com/lyx0/nourybot/pkg/api/ivr" +) + +func Firstline(channel string, streamer string, username string, nb *bot.Bot) { + ivrResponse, err := ivr.FirstLine(streamer, username) + if err != nil { + nb.Send(channel, fmt.Sprint(err)) + return + } + nb.Send(channel, ivrResponse) +} diff --git a/pkg/commands/followage.go b/pkg/commands/followage.go new file mode 100644 index 0000000..b7eb00b --- /dev/null +++ b/pkg/commands/followage.go @@ -0,0 +1,16 @@ +package commands + +import ( + "fmt" + + "github.com/lyx0/nourybot/cmd/bot" + "github.com/lyx0/nourybot/pkg/api/ivr" +) + +func Followage(channel string, streamer string, username string, nb *bot.Bot) { + ivrResponse, err := ivr.Followage(streamer, username) + if err != nil { + nb.Send(channel, fmt.Sprint(err)) + } + nb.Send(channel, ivrResponse) +} diff --git a/pkg/commands/game.go b/pkg/commands/game.go new file mode 100644 index 0000000..cfbd60d --- /dev/null +++ b/pkg/commands/game.go @@ -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 Game(channel string, name string, nb *bot.Bot) { + + game, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/channel/%s/game", name)) + if err != nil { + nb.Send(channel, "Something went wrong FeelsBadMan") + log.Error(err) + } + reply := fmt.Sprintf("@%s was last seen playing: %s", name, game) + nb.Send(channel, reply) +} diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index c7c39a6..fcb2ab4 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -26,33 +26,113 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) { // Useful for checking if enough cmdParams are given. msgLen := len(strings.SplitN(message.Message, " ", -2)) + // target channel + target := message.Channel + switch commandName { case "": if msgLen == 1 { - nb.Send(message.Channel, "xd") + nb.Send(target, "xd") } - case "echo": - if msgLen != 1 { - nb.Send(message.Channel, cmdParams[1]) + case "botstatus": + if msgLen == 1 { + nb.Send(target, "Usage: ()botstatus [username]") + return + } else { + commands.BotStatus(target, cmdParams[1], nb) return } + case "bttvemotes": + commands.BttvEmotes(target, nb) + return + case "cf": + commands.Coinflip(target, nb) + return + case "coin": + commands.Coinflip(target, nb) + return + case "coinflip": + commands.Coinflip(target, nb) + return + case "color": + commands.Color(message, nb) + return + case "mycolor": + commands.Color(message, nb) + return + case "echo": + commands.Echo(target, message.Message[7:len(message.Message)], nb) + return + case "8ball": + commands.EightBall(target, nb) + return + case "ffzemotes": + commands.FfzEmotes(target, nb) + return + + case "fill": + if msgLen == 1 { + nb.Send(target, "Usage: ()fill [emote]") + return + } else { + commands.Fill(target, message.Message[7:len(message.Message)], nb) + return + } + case "firstline": + if msgLen == 1 { + nb.Send(target, "Usage: ()firstline [channel] [user]") + return + } else if msgLen == 2 { + commands.Firstline(target, message.Channel, cmdParams[1], nb) + return + } else { + commands.Firstline(target, cmdParams[1], cmdParams[2], nb) + return + } + case "fl": + if msgLen == 1 { + nb.Send(target, "Usage: ()firstline [channel] [user]") + return + } else if msgLen == 2 { + commands.Firstline(target, message.Channel, cmdParams[1], nb) + return + } else { + commands.Firstline(target, cmdParams[1], cmdParams[2], nb) + return + } + case "followage": + if msgLen <= 2 { + nb.Send(target, "Usage: ()followage [channel] [user]") + return + } else { + commands.Followage(target, cmdParams[1], cmdParams[2], nb) + return + } + case "game": + if msgLen == 1 { + nb.Send(target, "Usage: ()game [channel]") + return + } else { + commands.Game(target, cmdParams[1], nb) + } + case "randomxkcd": - commands.RandomXkcd(message.Channel, nb) + commands.RandomXkcd(target, nb) return case "ping": - commands.Ping(message.Channel, nb) + commands.Ping(target, nb) return case "weather": if msgLen == 1 { - nb.Send(message.Channel, "Usage: ()weather [location]") + nb.Send(target, "Usage: ()weather [location]") return } else { - commands.Weather(message.Channel, message.Message[9:len(message.Message)], nb) + commands.Weather(target, message.Message[9:len(message.Message)], nb) return } case "xkcd": - commands.Xkcd(message.Channel, nb) + commands.Xkcd(target, nb) return }