diff --git a/cmd/bot/bot.go b/cmd/bot/bot.go index a9550c2..9bc6ef6 100644 --- a/cmd/bot/bot.go +++ b/cmd/bot/bot.go @@ -4,6 +4,7 @@ import ( twitch "github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/pkg/commands" cfg "github.com/lyx0/nourybot/pkg/config" + "github.com/lyx0/nourybot/pkg/handlers" log "github.com/sirupsen/logrus" ) @@ -26,7 +27,7 @@ func NewBot(cfg *cfg.Config) *Bot { func (b *Bot) Connect() error { log.Info("fn Connect") b.twitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) { - commands.HandleCommand(message, b.twitchClient) + handlers.HandleTwitchMessage(message, b.twitchClient) }) err := b.twitchClient.Connect() diff --git a/pkg/config/config.go b/pkg/config/config.go index 325df81..a1bd729 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -8,8 +8,9 @@ import ( ) type Config struct { - Username string - Oauth string + Username string + Oauth string + BotUserId string } func LoadConfig() *Config { @@ -19,8 +20,9 @@ func LoadConfig() *Config { } cfg := &Config{ - Username: os.Getenv("TWITCH_USER"), - Oauth: os.Getenv("TWITCH_PASS"), + Username: os.Getenv("TWITCH_USER"), + Oauth: os.Getenv("TWITCH_PASS"), + BotUserId: os.Getenv("BOT_USER_ID"), } log.Info("Config loaded succesfully") diff --git a/pkg/handlers/twitchmessage.go b/pkg/handlers/twitchmessage.go new file mode 100644 index 0000000..48846a5 --- /dev/null +++ b/pkg/handlers/twitchmessage.go @@ -0,0 +1,40 @@ +package handlers + +import ( + "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/pkg/commands" + config "github.com/lyx0/nourybot/pkg/config" + log "github.com/sirupsen/logrus" +) + +// HandleTwitchMessage takes in a twitch.Privatemessage and +// *twitch.Client 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 HandleTwitchMessage(message twitch.PrivateMessage, client *twitch.Client) { + log.Info("fn HandleTwitchMessage") + log.Info(message) + + // roomId is the Twitch UserID of the channel the message + // was sent in. + roomId := message.Tags["room-id"] + + // The message has no room-id so something went wrong. + if roomId == "" { + log.Errorf("Missing room-id in message tag", roomId) + return + } + + // Message was sent from the Bot. Don't act on it + // so that we don't repeat ourself. + if message.Tags["user-id"] == config.LoadConfig().BotUserId { + return + } + + if len(message.Message) >= 2 { + if message.Message[:2] == "()" { + commands.HandleCommand(message, client) + } + } + +}