mirror-nourybot/cmd/bot/bot.go

58 lines
1.3 KiB
Go
Raw Normal View History

2021-10-13 21:30:31 +02:00
package bot
import (
2021-10-14 19:02:07 +02:00
"time"
2021-10-13 21:30:31 +02:00
twitch "github.com/gempir/go-twitch-irc/v2"
2021-10-28 17:20:24 +02:00
"go.mongodb.org/mongo-driver/mongo"
2021-10-13 21:30:31 +02:00
)
type Bot struct {
2021-10-19 22:25:47 +02:00
TwitchClient *twitch.Client
2021-10-28 17:20:24 +02:00
MongoClient *mongo.Client
2021-10-14 19:02:07 +02:00
Uptime time.Time
2021-10-13 21:30:31 +02:00
}
2021-10-20 22:59:05 +02:00
// Send checks the message against a banphrase api
// and also splits the message into two if the message
// is too long for a single twitch chat message.
2021-10-19 22:25:47 +02:00
func (b *Bot) Send(target, text string) {
if len(text) == 0 {
return
2021-10-13 21:39:34 +02:00
}
if text[0] == '.' || text[0] == '/' || text[0] == '!' {
text = ":tf: " + text
}
2021-10-20 22:59:05 +02:00
2021-10-21 01:46:25 +02:00
// If a message is too long for a single twitch
// message, split it into two messages.
if len(text) > 500 {
firstMessage := text[0:499]
secondMessage := text[499:]
b.TwitchClient.Say(target, firstMessage)
b.TwitchClient.Say(target, secondMessage)
return
}
2021-10-20 22:59:05 +02:00
// check the message for bad words before we say it
messageBanned, banReason := CheckMessage(text)
if messageBanned {
// Bad message, replace message with a small
// notice on why it's banned.
b.TwitchClient.Say(target, banReason)
2021-10-20 22:35:36 +02:00
return
} else {
2021-10-20 22:59:05 +02:00
// Message was okay.
2021-10-20 22:35:36 +02:00
b.TwitchClient.Say(target, text)
return
}
}
2021-10-13 21:39:34 +02:00
2021-10-29 17:29:44 +02:00
// SkipChecking sends the message without banphrase check
func (b *Bot) SkipChecking(target, text string) {
b.TwitchClient.Say(target, text)
2021-10-13 23:29:29 +02:00
}