basic message handling

This commit is contained in:
lyx0 2021-10-14 00:45:32 +02:00
parent 809a2f83a8
commit d83c74a8ac
3 changed files with 48 additions and 5 deletions

View file

@ -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()

View file

@ -10,6 +10,7 @@ import (
type Config struct {
Username string
Oauth string
BotUserId string
}
func LoadConfig() *Config {
@ -21,6 +22,7 @@ func LoadConfig() *Config {
cfg := &Config{
Username: os.Getenv("TWITCH_USER"),
Oauth: os.Getenv("TWITCH_PASS"),
BotUserId: os.Getenv("BOT_USER_ID"),
}
log.Info("Config loaded succesfully")

View file

@ -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)
}
}
}