2021-05-14 11:15:38 +00:00
|
|
|
package twitch
|
|
|
|
|
|
|
|
import (
|
2021-09-17 08:54:55 +00:00
|
|
|
"context"
|
2021-05-14 11:15:38 +00:00
|
|
|
"strings"
|
2021-06-05 23:18:31 +00:00
|
|
|
"sync"
|
2021-09-18 20:06:22 +00:00
|
|
|
"text/template"
|
2021-05-14 11:15:38 +00:00
|
|
|
"time"
|
|
|
|
|
2021-09-18 20:06:22 +00:00
|
|
|
"github.com/Masterminds/sprig"
|
2021-05-14 11:15:38 +00:00
|
|
|
irc "github.com/gempir/go-twitch-irc/v2"
|
2021-09-17 08:54:55 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2021-05-14 11:15:38 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-09-17 08:54:55 +00:00
|
|
|
"github.com/strimertul/strimertul/database"
|
2021-05-14 11:15:38 +00:00
|
|
|
"github.com/strimertul/strimertul/modules/loyalty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Bot struct {
|
|
|
|
Client *irc.Client
|
|
|
|
|
|
|
|
api *Client
|
|
|
|
username string
|
|
|
|
config BotConfig
|
|
|
|
logger logrus.FieldLogger
|
|
|
|
lastMessage time.Time
|
|
|
|
activeUsers map[string]bool
|
|
|
|
banlist map[string]bool
|
2021-09-15 15:46:57 +00:00
|
|
|
chatHistory []irc.PrivateMessage
|
2021-05-14 11:15:38 +00:00
|
|
|
|
2021-09-18 20:06:22 +00:00
|
|
|
commands map[string]BotCommand
|
|
|
|
customCommands map[string]BotCustomCommand
|
|
|
|
customTemplates map[string]*template.Template
|
|
|
|
customFunctions template.FuncMap
|
2021-09-17 08:54:55 +00:00
|
|
|
|
2021-06-05 23:18:31 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
|
2021-05-14 11:15:38 +00:00
|
|
|
// Module specific vars
|
|
|
|
Loyalty *loyalty.Manager
|
2021-11-03 13:48:30 +00:00
|
|
|
Timers *BotTimerModule
|
2021-05-14 11:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBot(api *Client, config BotConfig) *Bot {
|
|
|
|
// Create client
|
|
|
|
client := irc.NewClient(config.Username, config.Token)
|
|
|
|
|
|
|
|
bot := &Bot{
|
2021-09-18 20:06:22 +00:00
|
|
|
Client: client,
|
|
|
|
username: strings.ToLower(config.Username), // Normalize username
|
|
|
|
config: config,
|
|
|
|
logger: api.logger,
|
|
|
|
api: api,
|
|
|
|
lastMessage: time.Now(),
|
|
|
|
activeUsers: make(map[string]bool),
|
|
|
|
banlist: make(map[string]bool),
|
|
|
|
mu: sync.Mutex{},
|
|
|
|
commands: make(map[string]BotCommand),
|
|
|
|
customCommands: make(map[string]BotCustomCommand),
|
|
|
|
customTemplates: make(map[string]*template.Template),
|
2021-05-14 11:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client.OnPrivateMessage(func(message irc.PrivateMessage) {
|
|
|
|
// Ignore messages for a while or twitch will get mad!
|
|
|
|
if message.Time.Before(bot.lastMessage.Add(time.Second * 2)) {
|
|
|
|
bot.logger.Debug("message received too soon, ignoring")
|
|
|
|
return
|
|
|
|
}
|
2021-06-05 23:18:31 +00:00
|
|
|
bot.mu.Lock()
|
2021-05-14 11:15:38 +00:00
|
|
|
bot.activeUsers[message.User.Name] = true
|
|
|
|
|
2021-10-05 11:06:56 +00:00
|
|
|
lcmessage := strings.ToLower(message.Message)
|
|
|
|
|
2021-05-14 11:15:38 +00:00
|
|
|
// Check if it's a command
|
|
|
|
if strings.HasPrefix(message.Message, "!") {
|
|
|
|
// Run through supported commands
|
2021-09-17 08:54:55 +00:00
|
|
|
for cmd, data := range bot.commands {
|
2021-09-16 13:55:43 +00:00
|
|
|
if !data.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
2021-10-05 11:06:56 +00:00
|
|
|
if strings.HasPrefix(lcmessage, cmd) {
|
2021-06-05 23:18:31 +00:00
|
|
|
go data.Handler(bot, message)
|
2021-05-14 11:15:38 +00:00
|
|
|
bot.lastMessage = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-15 15:46:57 +00:00
|
|
|
|
2021-09-16 13:55:43 +00:00
|
|
|
// Run through custom commands
|
2021-09-17 08:54:55 +00:00
|
|
|
for cmd, data := range bot.customCommands {
|
2021-09-16 13:55:43 +00:00
|
|
|
if !data.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
2021-10-05 11:06:56 +00:00
|
|
|
lc := strings.ToLower(cmd)
|
|
|
|
if strings.HasPrefix(lcmessage, lc) {
|
2021-09-18 20:06:22 +00:00
|
|
|
go cmdCustom(bot, cmd, data, message)
|
2021-09-16 13:55:43 +00:00
|
|
|
bot.lastMessage = time.Now()
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 08:54:55 +00:00
|
|
|
bot.mu.Unlock()
|
2021-09-16 13:55:43 +00:00
|
|
|
|
2021-09-15 15:46:57 +00:00
|
|
|
if bot.config.EnableChatKeys {
|
2021-09-16 15:11:49 +00:00
|
|
|
bot.api.db.PutJSON(ChatEventKey, message)
|
2021-09-15 15:46:57 +00:00
|
|
|
if bot.config.ChatHistory > 0 {
|
|
|
|
if len(bot.chatHistory) >= bot.config.ChatHistory {
|
|
|
|
bot.chatHistory = bot.chatHistory[len(bot.chatHistory)-bot.config.ChatHistory+1:]
|
|
|
|
}
|
|
|
|
bot.chatHistory = append(bot.chatHistory, message)
|
2021-09-16 15:11:49 +00:00
|
|
|
bot.api.db.PutJSON(ChatHistoryKey, bot.chatHistory)
|
2021-09-15 15:46:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-03 13:48:30 +00:00
|
|
|
|
|
|
|
if bot.Timers != nil {
|
|
|
|
go bot.Timers.OnMessage(message)
|
|
|
|
}
|
2021-05-14 11:15:38 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
client.OnUserJoinMessage(func(message irc.UserJoinMessage) {
|
|
|
|
if strings.ToLower(message.User) == bot.username {
|
|
|
|
bot.logger.WithField("channel", message.Channel).Info("joined channel")
|
|
|
|
} else {
|
|
|
|
bot.logger.WithFields(logrus.Fields{
|
|
|
|
"username": message.User,
|
|
|
|
"channel": message.Channel,
|
|
|
|
}).Debug("user joined channel")
|
|
|
|
}
|
|
|
|
})
|
2021-06-05 23:18:31 +00:00
|
|
|
|
2021-05-14 11:15:38 +00:00
|
|
|
client.OnUserPartMessage(func(message irc.UserPartMessage) {
|
|
|
|
if strings.ToLower(message.User) == bot.username {
|
|
|
|
bot.logger.WithField("channel", message.Channel).Info("left channel")
|
|
|
|
} else {
|
|
|
|
bot.logger.WithFields(logrus.Fields{
|
|
|
|
"username": message.User,
|
|
|
|
"channel": message.Channel,
|
|
|
|
}).Debug("user left channel")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
bot.Client.Join(config.Channel)
|
|
|
|
|
2021-11-03 13:48:30 +00:00
|
|
|
// Load modules
|
|
|
|
err := bot.LoadModules()
|
|
|
|
if err != nil {
|
|
|
|
bot.logger.WithError(err).Error("failed to load modules")
|
|
|
|
}
|
|
|
|
|
2021-09-17 08:54:55 +00:00
|
|
|
// Load custom commands
|
2021-09-18 20:06:22 +00:00
|
|
|
bot.setupFunctions()
|
2021-09-17 08:54:55 +00:00
|
|
|
api.db.GetJSON(CustomCommandsKey, &bot.customCommands)
|
2021-11-03 13:48:30 +00:00
|
|
|
err = bot.updateTemplates()
|
2021-09-18 20:06:22 +00:00
|
|
|
if err != nil {
|
|
|
|
bot.logger.WithError(err).Error("failed to load custom commands")
|
|
|
|
}
|
2021-09-17 08:54:55 +00:00
|
|
|
go api.db.Subscribe(context.Background(), bot.updateCommands, CustomCommandsKey)
|
2021-11-12 22:23:30 +00:00
|
|
|
go api.db.Subscribe(context.Background(), bot.handleWriteMessageRPC, WriteMessageRPC)
|
2021-09-17 08:54:55 +00:00
|
|
|
|
2021-05-14 11:15:38 +00:00
|
|
|
return bot
|
|
|
|
}
|
|
|
|
|
2021-09-17 08:54:55 +00:00
|
|
|
func (b *Bot) updateCommands(kvs []database.ModifiedKV) error {
|
|
|
|
for _, kv := range kvs {
|
2021-10-28 09:01:52 +00:00
|
|
|
switch kv.Key {
|
2021-09-17 08:54:55 +00:00
|
|
|
case CustomCommandsKey:
|
2021-09-18 20:06:22 +00:00
|
|
|
err := func() error {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
return jsoniter.ConfigFastest.Unmarshal(kv.Data, &b.customCommands)
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Recreate templates
|
|
|
|
if err := b.updateTemplates(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 22:23:30 +00:00
|
|
|
func (b *Bot) handleWriteMessageRPC(kvs []database.ModifiedKV) error {
|
|
|
|
for _, kv := range kvs {
|
|
|
|
switch kv.Key {
|
|
|
|
case WriteMessageRPC:
|
|
|
|
b.Client.Say(b.config.Channel, string(kv.Data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-18 20:06:22 +00:00
|
|
|
func (b *Bot) updateTemplates() error {
|
|
|
|
for cmd, tmpl := range b.customCommands {
|
|
|
|
var err error
|
|
|
|
b.customTemplates[cmd], err = template.New("").Funcs(sprig.TxtFuncMap()).Funcs(b.customFunctions).Parse(tmpl.Response)
|
|
|
|
if err != nil {
|
2021-09-17 08:54:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 11:15:38 +00:00
|
|
|
func (b *Bot) Connect() error {
|
|
|
|
return b.Client.Connect()
|
|
|
|
}
|
2021-11-03 13:48:30 +00:00
|
|
|
|
|
|
|
func (b *Bot) WriteMessage(message string) {
|
|
|
|
b.Client.Say(b.config.Channel, message)
|
|
|
|
}
|