diff --git a/cmd/bot/bot.go b/cmd/bot/bot.go index 5845575..1480910 100644 --- a/cmd/bot/bot.go +++ b/cmd/bot/bot.go @@ -4,63 +4,33 @@ import ( "time" twitch "github.com/gempir/go-twitch-irc/v2" - cfg "github.com/lyx0/nourybot/pkg/config" - "github.com/lyx0/nourybot/pkg/handlers" - log "github.com/sirupsen/logrus" ) type Bot struct { - twitchClient *twitch.Client - cfg *cfg.Config + TwitchClient *twitch.Client Uptime time.Time } -func NewBot(cfg *cfg.Config) *Bot { - - log.Info("fn Newbot") - twitchClient := twitch.NewClient(cfg.Username, cfg.Oauth) - - return &Bot{ - cfg: cfg, - twitchClient: twitchClient, - } +type Channel struct { + Name string } -func (b *Bot) Connect() error { - log.Info("fn Connect") - - b.Uptime = time.Now() - - b.twitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) { - handlers.HandlePrivateMessage(message, b.twitchClient, b.cfg, b.Uptime) - }) - - b.twitchClient.OnWhisperMessage(func(message twitch.WhisperMessage) { - handlers.HandleWhisperMessage(message, b.twitchClient) - }) - - err := b.twitchClient.Connect() - if err != nil { - log.Error("Error Connecting from Twitch: ", err) +func (b *Bot) Send(target, text string) { + if len(text) == 0 { + return } - return err -} + // if message[0] == '.' || message[0] == '/' { + // message = ". " + message + // } -func (b *Bot) Disconnect() error { - err := b.twitchClient.Disconnect() - if err != nil { - log.Error("Error Disconnecting from Twitch: ", err) + if len(text) > 500 { + firstMessage := text[0:499] + secondMessage := text[499:] + b.TwitchClient.Say(target, firstMessage) + b.TwitchClient.Say(target, secondMessage) + return } - return err -} - -func (b *Bot) Say(channel string, message string) { - b.twitchClient.Say(channel, message) -} - -func (b *Bot) Join(channel string) { - log.Info("fn Join") - b.twitchClient.Join(channel) + b.TwitchClient.Say(target, text) } diff --git a/cmd/main.go b/cmd/main.go index 990c7b8..4839b99 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,17 +1,42 @@ package main import ( + "fmt" + "time" + + "github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/config" + "github.com/lyx0/nourybot/pkg/handlers" ) +var nb *bot.Bot + func main() { - cfg := config.LoadConfig() - nb := bot.NewBot(cfg) + conf := config.LoadConfig() - nb.Join("nourybot") + nb = &bot.Bot{ + TwitchClient: twitch.NewClient(conf.Username, conf.Oauth), + Uptime: time.Now(), + } - nb.Say("nourybot", "HeyGuys") + nb.TwitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) { + // If channelID is missing something must have gone wrong. + channelID := message.Tags["room-id"] + if channelID == "" { + fmt.Printf("Missing room-id tag in message") + return + } - nb.Connect() + // So that the bot doesn't repeat itself. + if message.Tags["user-id"] == "596581605" { + return + } + + handlers.PrivateMessage(message, nb) + }) + + nb.TwitchClient.Join("nouryqt", "nourybot") + nb.Send("nourybot", "HeyGuys") + nb.TwitchClient.Connect() } diff --git a/pkg/commands/botstatus.go b/pkg/commands/botstatus.go index 1632da4..2660eec 100644 --- a/pkg/commands/botstatus.go +++ b/pkg/commands/botstatus.go @@ -3,18 +3,17 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api/aiden" log "github.com/sirupsen/logrus" ) -func BotStatus(channel string, name string, client *twitch.Client) { +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 { - client.Say(channel, "Something went wrong FeelsBadMan") + nb.Send(channel, "Something went wrong FeelsBadMan") log.Error(err) } - client.Say(channel, resp) - + nb.Send(channel, resp) } diff --git a/pkg/commands/bttvemotes.go b/pkg/commands/bttvemotes.go index db389c8..94b1d93 100644 --- a/pkg/commands/bttvemotes.go +++ b/pkg/commands/bttvemotes.go @@ -3,17 +3,17 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api/aiden" log "github.com/sirupsen/logrus" ) -func BttvEmotes(channel string, client *twitch.Client) { +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) - client.Say(channel, "Something went wrong FeelsBadMan") + nb.Send(channel, "Something went wrong FeelsBadMan") } - client.Say(channel, string(resp)) + nb.Send(channel, string(resp)) } diff --git a/pkg/commands/coinflip.go b/pkg/commands/coinflip.go index c713041..63c8aca 100644 --- a/pkg/commands/coinflip.go +++ b/pkg/commands/coinflip.go @@ -1,16 +1,16 @@ package commands import ( - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/utils" ) -func Coinflip(channel string, client *twitch.Client) { +func Coinflip(channel string, nb *bot.Bot) { result := utils.GenerateRandomNumber(2) if result == 1 { - client.Say(channel, "Heads") + nb.Send(channel, "Heads") } else { - client.Say(channel, "Tails") + nb.Send(channel, "Tails") } } diff --git a/pkg/commands/color.go b/pkg/commands/color.go index 1740603..155acd6 100644 --- a/pkg/commands/color.go +++ b/pkg/commands/color.go @@ -4,11 +4,12 @@ import ( "fmt" "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" ) -func Color(message twitch.PrivateMessage, client *twitch.Client) { +func Color(message twitch.PrivateMessage, nb *bot.Bot) { reply := fmt.Sprintf("@%v, your color is %v", message.User.DisplayName, message.User.Color) - client.Say(message.Channel, reply) + nb.Send(message.Channel, reply) } diff --git a/pkg/commands/echo.go b/pkg/commands/echo.go index cb615bb..ddd9917 100644 --- a/pkg/commands/echo.go +++ b/pkg/commands/echo.go @@ -1,7 +1,7 @@ package commands -import "github.com/gempir/go-twitch-irc/v2" +import "github.com/lyx0/nourybot/cmd/bot" -func Echo(channel string, message string, client *twitch.Client) { - client.Say(channel, message) +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 index 47aa4ea..751d979 100644 --- a/pkg/commands/eightball.go +++ b/pkg/commands/eightball.go @@ -1,17 +1,17 @@ package commands import ( - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api/aiden" log "github.com/sirupsen/logrus" ) -func EightBall(channel string, client *twitch.Client) { +func EightBall(channel string, nb *bot.Bot) { resp, err := aiden.ApiCall("api/v1/misc/8ball") if err != nil { log.Error(err) - client.Say(channel, "Something went wrong FeelsBadMan") + nb.Send(channel, "Something went wrong FeelsBadMan") } - client.Say(channel, string(resp)) + nb.Send(channel, string(resp)) } diff --git a/pkg/commands/ffzemotes.go b/pkg/commands/ffzemotes.go index 7ab9fdd..14f1e7d 100644 --- a/pkg/commands/ffzemotes.go +++ b/pkg/commands/ffzemotes.go @@ -3,18 +3,18 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api/aiden" log "github.com/sirupsen/logrus" ) -func FfzEmotes(channel string, client *twitch.Client) { +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) - client.Say(channel, "Something went wrong FeelsBadMan") + nb.Send(channel, "Something went wrong FeelsBadMan") } - client.Say(channel, string(resp)) + nb.Send(channel, string(resp)) } diff --git a/pkg/commands/fill.go b/pkg/commands/fill.go index c5f6379..36b2663 100644 --- a/pkg/commands/fill.go +++ b/pkg/commands/fill.go @@ -4,12 +4,12 @@ import ( "fmt" "strings" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" ) -func Fill(channel string, emote string, client *twitch.Client) { +func Fill(channel string, emote string, nb *bot.Bot) { if emote[0] == '.' || emote[0] == '/' { - client.Say(channel, ":tf:") + nb.Send(channel, ":tf:") return } @@ -20,6 +20,6 @@ func Fill(channel string, emote string, client *twitch.Client) { repeatCount := (499 / emoteLength) reply := strings.Repeat(fmt.Sprintf(emote+" "), repeatCount) - client.Say(channel, reply) + nb.Send(channel, reply) } diff --git a/pkg/commands/firstline.go b/pkg/commands/firstline.go index f387e24..eed2fe7 100644 --- a/pkg/commands/firstline.go +++ b/pkg/commands/firstline.go @@ -3,15 +3,15 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api/ivr" ) -func Firstline(channel string, streamer string, username string, client *twitch.Client) { +func Firstline(channel string, streamer string, username string, nb *bot.Bot) { ivrResponse, err := ivr.FirstLine(streamer, username) if err != nil { - client.Say(channel, fmt.Sprint(err)) + nb.Send(channel, fmt.Sprint(err)) return } - client.Say(channel, ivrResponse) + nb.Send(channel, ivrResponse) } diff --git a/pkg/commands/followage.go b/pkg/commands/followage.go index 0825050..b7eb00b 100644 --- a/pkg/commands/followage.go +++ b/pkg/commands/followage.go @@ -3,14 +3,14 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api/ivr" ) -func Followage(channel string, streamer string, username string, client *twitch.Client) { +func Followage(channel string, streamer string, username string, nb *bot.Bot) { ivrResponse, err := ivr.Followage(streamer, username) if err != nil { - client.Say(channel, fmt.Sprint(err)) + nb.Send(channel, fmt.Sprint(err)) } - client.Say(channel, ivrResponse) + nb.Send(channel, ivrResponse) } diff --git a/pkg/commands/game.go b/pkg/commands/game.go index 797e11c..cfbd60d 100644 --- a/pkg/commands/game.go +++ b/pkg/commands/game.go @@ -3,18 +3,18 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api/aiden" log "github.com/sirupsen/logrus" ) -func Game(channel string, name string, client *twitch.Client) { +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 { - client.Say(channel, "Something went wrong FeelsBadMan") + nb.Send(channel, "Something went wrong FeelsBadMan") log.Error(err) } reply := fmt.Sprintf("@%s was last seen playing: %s", name, game) - client.Say(channel, reply) + nb.Send(channel, reply) } diff --git a/pkg/commands/godocs.go b/pkg/commands/godocs.go index 65da7e2..9a92097 100644 --- a/pkg/commands/godocs.go +++ b/pkg/commands/godocs.go @@ -3,11 +3,11 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" ) -func Godocs(channel string, searchTerm string, client *twitch.Client) { +func Godocs(channel string, searchTerm string, nb *bot.Bot) { resp := fmt.Sprintf("https://godocs.io/?q=%s", searchTerm) - client.Say(channel, resp) + nb.Send(channel, resp) } diff --git a/pkg/commands/number.go b/pkg/commands/number.go index 11942b9..bd3c0cf 100644 --- a/pkg/commands/number.go +++ b/pkg/commands/number.go @@ -1,16 +1,16 @@ package commands import ( - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api" ) -func RandomNumber(channel string, client *twitch.Client) { +func RandomNumber(channel string, nb *bot.Bot) { reply := api.RandomNumber() - client.Say(channel, string(reply)) + nb.Send(channel, string(reply)) } -func Number(channel string, number string, client *twitch.Client) { +func Number(channel string, number string, nb *bot.Bot) { reply := api.Number(number) - client.Say(channel, string(reply)) + nb.Send(channel, string(reply)) } diff --git a/pkg/commands/ping.go b/pkg/commands/ping.go index 78a0ced..7f3845d 100644 --- a/pkg/commands/ping.go +++ b/pkg/commands/ping.go @@ -2,17 +2,17 @@ package commands import ( "fmt" - "time" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/humanize" "github.com/lyx0/nourybot/pkg/utils" ) -func Ping(channel string, client *twitch.Client, uptime time.Time) { +func Ping(target string, nb *bot.Bot) { commandCount := fmt.Sprint(utils.GetCommandsUsed()) - botUptime := humanize.Time(uptime) + botUptime := humanize.Time(nb.Uptime) - reply := fmt.Sprintf("Pong! :) Commands used: %v, Last restart: %v", commandCount, botUptime) - client.Say(channel, reply) + reply := fmt.Sprintf("Pong! :) Commands Used: %v, Last restart: %v", commandCount, botUptime) + + nb.Send(target, reply) } diff --git a/pkg/commands/pingme.go b/pkg/commands/pingme.go index 26cc98c..d118c93 100644 --- a/pkg/commands/pingme.go +++ b/pkg/commands/pingme.go @@ -3,12 +3,11 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" ) -func Pingme(channel string, user string, client *twitch.Client) { +func Pingme(channel string, user string, nb *bot.Bot) { response := fmt.Sprintf("@%s", user) - client.Say(channel, response) - + nb.Send(channel, response) } diff --git a/pkg/commands/profilepicture.go b/pkg/commands/profilepicture.go index e52f412..8146b7c 100644 --- a/pkg/commands/profilepicture.go +++ b/pkg/commands/profilepicture.go @@ -3,16 +3,16 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api/ivr" ) -func ProfilePicture(channel string, target string, client *twitch.Client) { +func ProfilePicture(channel string, target string, nb *bot.Bot) { reply, err := ivr.ProfilePicture(target) if err != nil { - client.Say(channel, fmt.Sprint(err)) + nb.Send(channel, fmt.Sprint(err)) return } - client.Say(channel, reply) + nb.Send(channel, reply) } diff --git a/pkg/commands/pyramid.go b/pkg/commands/pyramid.go index aa2e7e7..3291f13 100644 --- a/pkg/commands/pyramid.go +++ b/pkg/commands/pyramid.go @@ -5,17 +5,17 @@ import ( "strconv" "strings" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" ) -func Pyramid(channel string, size string, emote string, client *twitch.Client) { +func Pyramid(channel string, size string, emote string, nb *bot.Bot) { if size[0] == '.' || size[0] == '/' { - client.Say(channel, ":tf:") + nb.Send(channel, ":tf:") return } if emote[0] == '.' || emote[0] == '/' { - client.Say(channel, ":tf:") + nb.Send(channel, ":tf:") return } @@ -23,27 +23,27 @@ func Pyramid(channel string, size string, emote string, client *twitch.Client) { pyramidEmote := fmt.Sprint(emote + " ") if err != nil { - client.Say(channel, "Something went wrong") + nb.Send(channel, "Something went wrong") } if pyramidSize == 1 { - client.Say(channel, fmt.Sprint(pyramidEmote+"...")) + nb.Send(channel, fmt.Sprint(pyramidEmote+"...")) return } if pyramidSize > 20 { - client.Say(channel, "Max pyramid size is 20") + nb.Send(channel, "Max pyramid size is 20") return } for i := 0; i <= pyramidSize; i++ { pyramidMessageAsc := strings.Repeat(pyramidEmote, i) // fmt.Println(pyramidMessageAsc) - client.Say(channel, pyramidMessageAsc) + nb.Send(channel, pyramidMessageAsc) } for j := pyramidSize - 1; j >= 0; j-- { pyramidMessageDesc := strings.Repeat(pyramidEmote, j) // fmt.Println(pyramidMessageDesc) - client.Say(channel, pyramidMessageDesc) + nb.Send(channel, pyramidMessageDesc) } } diff --git a/pkg/commands/randomcat.go b/pkg/commands/randomcat.go index 2987d56..34dae05 100644 --- a/pkg/commands/randomcat.go +++ b/pkg/commands/randomcat.go @@ -1,12 +1,12 @@ package commands import ( - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api" ) -func RandomCat(channel string, client *twitch.Client) { +func RandomCat(channel string, nb *bot.Bot) { reply := api.RandomCat() - client.Say(channel, reply) + nb.Send(channel, reply) } diff --git a/pkg/commands/randomdog.go b/pkg/commands/randomdog.go index ece7677..5555dba 100644 --- a/pkg/commands/randomdog.go +++ b/pkg/commands/randomdog.go @@ -1,12 +1,12 @@ package commands import ( - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api" ) -func RandomDog(channel string, client *twitch.Client) { +func RandomDog(channel string, nb *bot.Bot) { reply := api.RandomDog() - client.Say(channel, reply) + nb.Send(channel, reply) } diff --git a/pkg/commands/randomfox.go b/pkg/commands/randomfox.go index 97c7e5e..d9b3744 100644 --- a/pkg/commands/randomfox.go +++ b/pkg/commands/randomfox.go @@ -1,12 +1,12 @@ package commands import ( - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api" ) -func RandomFox(channel string, client *twitch.Client) { +func RandomFox(channel string, nb *bot.Bot) { reply := api.RandomFox() - client.Say(channel, reply) + nb.Send(channel, reply) } diff --git a/pkg/commands/randomxkcd.go b/pkg/commands/randomxkcd.go index 0dffb1a..23573dc 100644 --- a/pkg/commands/randomxkcd.go +++ b/pkg/commands/randomxkcd.go @@ -1,11 +1,11 @@ package commands import ( - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api" ) -func RandomXkcd(channel string, client *twitch.Client) { +func RandomXkcd(channel string, nb *bot.Bot) { reply := api.RandomXkcd() - client.Say(channel, reply) + nb.Send(channel, reply) } diff --git a/pkg/commands/subage.go b/pkg/commands/subage.go index c101818..70a0988 100644 --- a/pkg/commands/subage.go +++ b/pkg/commands/subage.go @@ -3,16 +3,16 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api/ivr" ) -func Subage(channel string, username string, streamer string, client *twitch.Client) { +func Subage(channel string, username string, streamer string, nb *bot.Bot) { ivrResponse, err := ivr.Subage(username, streamer) if err != nil { - client.Say(channel, fmt.Sprint(err)) + nb.Send(channel, fmt.Sprint(err)) return } - client.Say(channel, ivrResponse) + nb.Send(channel, ivrResponse) } diff --git a/pkg/commands/thumbnail.go b/pkg/commands/thumbnail.go index 311257c..ab19f95 100644 --- a/pkg/commands/thumbnail.go +++ b/pkg/commands/thumbnail.go @@ -3,14 +3,14 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/utils" ) -func Thumbnail(channel string, target string, client *twitch.Client) { +func Thumbnail(channel string, target string, nb *bot.Bot) { imageHeight := utils.GenerateRandomNumberRange(1040, 1080) imageWidth := utils.GenerateRandomNumberRange(1890, 1920) response := fmt.Sprintf("https://static-cdn.jtvnw.net/previews-ttv/live_user_%v-%vx%v.jpg", target, imageWidth, imageHeight) - client.Say(channel, response) + nb.Send(channel, response) } diff --git a/pkg/commands/title.go b/pkg/commands/title.go index 0a69bea..076629b 100644 --- a/pkg/commands/title.go +++ b/pkg/commands/title.go @@ -3,17 +3,17 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api/aiden" log "github.com/sirupsen/logrus" ) -func Title(channel string, target string, client *twitch.Client) { +func Title(channel string, target string, nb *bot.Bot) { title, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/channel/%s/title", target)) if err != nil { - client.Say(channel, "Something went wrong FeelsBadMan") + nb.Send(channel, "Something went wrong FeelsBadMan") log.Error(err) } reply := fmt.Sprintf("%s title is: %s", target, title) - client.Say(channel, reply) + nb.Send(channel, reply) } diff --git a/pkg/commands/uptime.go b/pkg/commands/uptime.go index 12bcbbf..af97c0c 100644 --- a/pkg/commands/uptime.go +++ b/pkg/commands/uptime.go @@ -3,19 +3,19 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api/aiden" log "github.com/sirupsen/logrus" ) -func Uptime(channel string, name string, client *twitch.Client) { +func Uptime(channel string, name string, nb *bot.Bot) { resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/channel/%s/uptime", name)) if err != nil { - client.Say(channel, "Something went wrong FeelsBadMan") + nb.Send(channel, "Something went wrong FeelsBadMan") log.Error(err) } - client.Say(channel, resp) + nb.Send(channel, resp) } diff --git a/pkg/commands/userid.go b/pkg/commands/userid.go index 2cdaf0e..bdc9897 100644 --- a/pkg/commands/userid.go +++ b/pkg/commands/userid.go @@ -1,11 +1,11 @@ package commands import ( - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api/ivr" ) -func Userid(channel string, target string, client *twitch.Client) { +func Userid(channel string, target string, nb *bot.Bot) { reply := ivr.Userid(target) - client.Say(channel, reply) + nb.Send(channel, reply) } diff --git a/pkg/commands/weather.go b/pkg/commands/weather.go index 7dffced..1f0ad1f 100644 --- a/pkg/commands/weather.go +++ b/pkg/commands/weather.go @@ -3,18 +3,18 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api/aiden" log "github.com/sirupsen/logrus" ) -func Weather(channel string, location string, client *twitch.Client) { +func Weather(channel string, location string, nb *bot.Bot) { reply, err := aiden.ApiCall(fmt.Sprintf("api/v1/misc/weather/%s", location)) if err != nil { - client.Say(channel, "Something went wrong FeelsBadMan") + nb.Send(channel, "Something went wrong FeelsBadMan") log.Error(err) } - client.Say(channel, reply) + nb.Send(channel, reply) } diff --git a/pkg/commands/xd.go b/pkg/commands/xd.go index 1afda65..d5c7c49 100644 --- a/pkg/commands/xd.go +++ b/pkg/commands/xd.go @@ -1,7 +1,7 @@ package commands -import "github.com/gempir/go-twitch-irc/v2" +import "github.com/lyx0/nourybot/cmd/bot" -func Xd(channel string, client *twitch.Client) { - client.Say(channel, "xd") +func Xd(channel string, nb *bot.Bot) { + nb.Send(channel, "xd") } diff --git a/pkg/commands/xkcd.go b/pkg/commands/xkcd.go index 646dd91..1768851 100644 --- a/pkg/commands/xkcd.go +++ b/pkg/commands/xkcd.go @@ -1,12 +1,13 @@ package commands import ( - "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/api" ) -func Xkcd(channel string, client *twitch.Client) { +func Xkcd(channel string, nb *bot.Bot) { reply := api.Xkcd() - client.Say(channel, reply) + + nb.Send(channel, reply) } diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index 9da2edf..7b9754a 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -2,23 +2,16 @@ package handlers import ( "strings" - "time" "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/commands" "github.com/lyx0/nourybot/pkg/utils" - log "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus" ) -// HandleCommand receives a twitch.PrivateMessage from -// HandlePrivateMessage where it found a command in it. -// HandleCommand passes on the message to the specific -// command handler for further action. -func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, uptime time.Time) { - log.Info("fn HandleCommand") - - // Counter that increments on every command call. - utils.CommandUsed() +func Command(message twitch.PrivateMessage, nb *bot.Bot) { + logrus.Info("fn Command") // commandName is the actual command name without the prefix. commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:]) @@ -34,238 +27,242 @@ func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, u // 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 { - twitchClient.Say(message.Channel, "Why yes, that's my prefix :)") - return + nb.Send(target, "xd") } case "botstatus": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()botstatus [username]") + nb.Send(target, "Usage: ()botstatus [username]") return } else { - commands.BotStatus(message.Channel, cmdParams[1], twitchClient) + commands.BotStatus(target, cmdParams[1], nb) return } case "bttvemotes": - commands.BttvEmotes(message.Channel, twitchClient) + commands.BttvEmotes(target, nb) return case "cf": - commands.Coinflip(message.Channel, twitchClient) + commands.Coinflip(target, nb) return case "coin": - commands.Coinflip(message.Channel, twitchClient) + commands.Coinflip(target, nb) return case "coinflip": - commands.Coinflip(message.Channel, twitchClient) + commands.Coinflip(target, nb) return case "color": - commands.Color(message, twitchClient) + commands.Color(message, nb) return case "mycolor": - commands.Color(message, twitchClient) + commands.Color(message, nb) return case "echo": - commands.Echo(message.Channel, message.Message[7:len(message.Message)], twitchClient) + commands.Echo(target, message.Message[7:len(message.Message)], nb) return case "8ball": - commands.EightBall(message.Channel, twitchClient) + commands.EightBall(target, nb) return case "ffzemotes": - commands.FfzEmotes(message.Channel, twitchClient) + commands.FfzEmotes(target, nb) return + case "fill": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()fill [emote]") + nb.Send(target, "Usage: ()fill [emote]") return } else { - commands.Fill(message.Channel, message.Message[7:len(message.Message)], twitchClient) + commands.Fill(target, message.Message[7:len(message.Message)], nb) return } case "firstline": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()firstline [channel] [user]") + nb.Send(target, "Usage: ()firstline [channel] [user]") return } else if msgLen == 2 { - commands.Firstline(message.Channel, message.Channel, cmdParams[1], twitchClient) + commands.Firstline(target, target, cmdParams[1], nb) return } else { - commands.Firstline(message.Channel, cmdParams[1], cmdParams[2], twitchClient) + commands.Firstline(target, cmdParams[1], cmdParams[2], nb) return } case "fl": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()firstline [channel] [user]") + nb.Send(target, "Usage: ()firstline [channel] [user]") return } else if msgLen == 2 { - commands.Firstline(message.Channel, message.Channel, cmdParams[1], twitchClient) + commands.Firstline(target, target, cmdParams[1], nb) return } else { - commands.Firstline(message.Channel, cmdParams[1], cmdParams[2], twitchClient) + commands.Firstline(target, cmdParams[1], cmdParams[2], nb) return } case "followage": if msgLen <= 2 { - twitchClient.Say(message.Channel, "Usage: ()followage [channel] [user]") + nb.Send(target, "Usage: ()followage [channel] [user]") return } else { - commands.Followage(message.Channel, cmdParams[1], cmdParams[2], twitchClient) + commands.Followage(target, cmdParams[1], cmdParams[2], nb) return } case "game": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()game [channel]") + nb.Send(target, "Usage: ()game [channel]") return } else { - commands.Game(message.Channel, cmdParams[1], twitchClient) + commands.Game(target, cmdParams[1], nb) } case "godoc": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()godoc [term]") + nb.Send(target, "Usage: ()godoc [term]") return } else { - commands.Godocs(message.Channel, message.Message[8:len(message.Message)], twitchClient) + commands.Godocs(target, message.Message[8:len(message.Message)], nb) return } case "godocs": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()godoc [term]") + nb.Send(target, "Usage: ()godoc [term]") return } else { - commands.Godocs(message.Channel, message.Message[9:len(message.Message)], twitchClient) + commands.Godocs(target, message.Message[9:len(message.Message)], nb) return } case "num": if msgLen == 1 { - commands.RandomNumber(message.Channel, twitchClient) + commands.RandomNumber(target, nb) } else { - commands.Number(message.Channel, cmdParams[1], twitchClient) + commands.Number(target, cmdParams[1], nb) } case "number": if msgLen == 1 { - commands.RandomNumber(message.Channel, twitchClient) + commands.RandomNumber(target, nb) } else { - commands.Number(message.Channel, cmdParams[1], twitchClient) + commands.Number(target, cmdParams[1], nb) } case "ping": - commands.Ping(message.Channel, twitchClient, uptime) + commands.Ping(target, nb) return case "pingme": - commands.Pingme(message.Channel, message.User.DisplayName, twitchClient) + commands.Pingme(target, message.User.DisplayName, nb) return case "preview": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()preview [channel]") + nb.Send(target, "Usage: ()preview [channel]") return } else { - commands.Thumbnail(message.Channel, cmdParams[1], twitchClient) + commands.Thumbnail(target, cmdParams[1], nb) return } case "profilepicture": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()profilepicture [user]") + nb.Send(target, "Usage: ()profilepicture [user]") return } - commands.ProfilePicture(message.Channel, cmdParams[1], twitchClient) + commands.ProfilePicture(target, cmdParams[1], nb) return case "pfp": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()pfp [user]") + nb.Send(target, "Usage: ()pfp [user]") return } - commands.ProfilePicture(message.Channel, cmdParams[1], twitchClient) + commands.ProfilePicture(target, cmdParams[1], nb) return + case "pyramid": if msgLen != 3 { - twitchClient.Say(message.Channel, "Usage: ()pyramid [size] [emote]") + nb.Send(target, "Usage: ()pyramid [size] [emote]") } else if utils.ElevatedPrivsMessage(message) { - commands.Pyramid(message.Channel, cmdParams[1], cmdParams[2], twitchClient) + commands.Pyramid(target, cmdParams[1], cmdParams[2], nb) } else { - twitchClient.Say(message.Channel, "Pleb's can't pyramid FeelsBadMan") + nb.Send(target, "Pleb's can't pyramid FeelsBadMan") } case "randomcat": - commands.RandomCat(message.Channel, twitchClient) + commands.RandomCat(target, nb) return case "randomdog": - commands.RandomDog(message.Channel, twitchClient) + commands.RandomDog(target, nb) return case "randomfox": - commands.RandomFox(message.Channel, twitchClient) + commands.RandomFox(target, nb) return case "randomxkcd": - commands.RandomXkcd(message.Channel, twitchClient) + commands.RandomXkcd(target, nb) return case "subage": if msgLen < 3 { - twitchClient.Say(message.Channel, "Usage: ()subage [user] [streamer]") + nb.Send(target, "Usage: ()subage [user] [streamer]") return } else { - commands.Subage(message.Channel, cmdParams[1], cmdParams[2], twitchClient) + commands.Subage(target, cmdParams[1], cmdParams[2], nb) return } case "thumb": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()thumbnail [channel]") + nb.Send(target, "Usage: ()thumbnail [channel]") return } else { - commands.Thumbnail(message.Channel, cmdParams[1], twitchClient) + commands.Thumbnail(target, cmdParams[1], nb) return } case "thumbnail": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()thumbnail [channel]") + nb.Send(target, "Usage: ()thumbnail [channel]") return } else { - commands.Thumbnail(message.Channel, cmdParams[1], twitchClient) + commands.Thumbnail(target, cmdParams[1], nb) return } case "title": if msgLen == 1 { - commands.Title(message.Channel, message.Channel, twitchClient) + commands.Title(target, target, nb) return } else { - commands.Title(message.Channel, cmdParams[1], twitchClient) + commands.Title(target, cmdParams[1], nb) return } case "uptime": if msgLen == 1 { - commands.Uptime(message.Channel, message.Channel, twitchClient) + commands.Uptime(target, target, nb) return } else { - commands.Uptime(message.Channel, cmdParams[1], twitchClient) + commands.Uptime(target, cmdParams[1], nb) return } case "uid": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()uid [username]") + nb.Send(target, "Usage: ()uid [username]") return } else { - commands.Userid(message.Channel, cmdParams[1], twitchClient) + commands.Userid(target, cmdParams[1], nb) return } case "userid": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()userid [username]") + nb.Send(target, "Usage: ()userid [username]") return } else { - commands.Userid(message.Channel, cmdParams[1], twitchClient) + commands.Userid(target, cmdParams[1], nb) return } + case "weather": if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()weather [location]") + nb.Send(target, "Usage: ()weather [location]") return } else { - commands.Weather(message.Channel, message.Message[9:len(message.Message)], twitchClient) + commands.Weather(target, message.Message[9:len(message.Message)], nb) return } case "xd": - commands.Xd(message.Channel, twitchClient) - return - + commands.Xd(target, nb) case "xkcd": - commands.Xkcd(message.Channel, twitchClient) + commands.Xkcd(target, nb) return } + } diff --git a/pkg/handlers/privatemessage.go b/pkg/handlers/privatemessage.go index fabb72a..81670c5 100644 --- a/pkg/handlers/privatemessage.go +++ b/pkg/handlers/privatemessage.go @@ -1,10 +1,8 @@ package handlers import ( - "time" - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/config" + "github.com/lyx0/nourybot/cmd/bot" log "github.com/sirupsen/logrus" ) @@ -12,8 +10,8 @@ import ( // *twitch.Client and *config.Config and has the logic to decide if the provided // PrivateMessage is a command or not and passes it on accordingly. // Typical twitch message tags https://paste.ivr.fi/nopiradodo.lua -func HandlePrivateMessage(message twitch.PrivateMessage, client *twitch.Client, cfg *config.Config, uptime time.Time) { - log.Info("fn HandlePrivateMessage") +func PrivateMessage(message twitch.PrivateMessage, nb *bot.Bot) { + log.Info("fn PrivateMessage") // log.Info(message) // roomId is the Twitch UserID of the channel the message @@ -26,13 +24,6 @@ func HandlePrivateMessage(message twitch.PrivateMessage, client *twitch.Client, return } - // Message was sent from the Bot. Don't act on it - // so that we don't repeat ourself. - botUserId := cfg.BotUserId - if message.Tags["user-id"] == botUserId { - return - } - // Since our command prefix is () ignore every message // that is less than 2 if len(message.Message) >= 2 { @@ -40,7 +31,7 @@ func HandlePrivateMessage(message twitch.PrivateMessage, client *twitch.Client, // Message starts with (), pass it on to // the command handler. if message.Message[:2] == "()" { - HandleCommand(message, client, uptime) + Command(message, nb) return } } diff --git a/pkg/handlers/whisper.go b/pkg/handlers/whisper.go deleted file mode 100644 index d8e10a9..0000000 --- a/pkg/handlers/whisper.go +++ /dev/null @@ -1,14 +0,0 @@ -package handlers - -import ( - "github.com/gempir/go-twitch-irc/v2" - log "github.com/sirupsen/logrus" -) - -func HandleWhisperMessage(whisper twitch.WhisperMessage, client *twitch.Client) { - log.Info("fn HandleWhisperMessage") - log.Info(whisper) - if whisper.Message == "xd" { - client.Whisper(whisper.User.Name, "xd") - } -}