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-13 23:37:46 +02:00
|
|
|
cfg "github.com/lyx0/nourybot/pkg/config"
|
2021-10-14 00:45:32 +02:00
|
|
|
"github.com/lyx0/nourybot/pkg/handlers"
|
2021-10-13 21:30:31 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Bot struct {
|
|
|
|
twitchClient *twitch.Client
|
|
|
|
cfg *cfg.Config
|
2021-10-14 19:02:07 +02:00
|
|
|
Uptime time.Time
|
2021-10-13 21:30:31 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 23:29:29 +02:00
|
|
|
func NewBot(cfg *cfg.Config) *Bot {
|
2021-10-13 23:24:02 +02:00
|
|
|
|
2021-10-13 23:29:29 +02:00
|
|
|
log.Info("fn Newbot")
|
2021-10-13 23:24:02 +02:00
|
|
|
twitchClient := twitch.NewClient(cfg.Username, cfg.Oauth)
|
2021-10-13 23:29:29 +02:00
|
|
|
|
2021-10-13 21:30:31 +02:00
|
|
|
return &Bot{
|
|
|
|
cfg: cfg,
|
|
|
|
twitchClient: twitchClient,
|
2021-10-14 19:02:07 +02:00
|
|
|
Uptime: time.Now(),
|
2021-10-13 21:30:31 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-13 21:39:34 +02:00
|
|
|
|
|
|
|
func (b *Bot) Connect() error {
|
2021-10-13 23:29:29 +02:00
|
|
|
log.Info("fn Connect")
|
2021-10-14 00:55:07 +02:00
|
|
|
cfg := cfg.LoadConfig()
|
2021-10-14 02:27:01 +02:00
|
|
|
|
2021-10-14 00:19:35 +02:00
|
|
|
b.twitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
|
2021-10-14 19:02:07 +02:00
|
|
|
handlers.HandlePrivateMessage(message, b.twitchClient, cfg, b.Uptime)
|
2021-10-14 00:19:35 +02:00
|
|
|
})
|
|
|
|
|
2021-10-14 15:14:38 +02:00
|
|
|
b.twitchClient.OnWhisperMessage(func(message twitch.WhisperMessage) {
|
|
|
|
handlers.HandleWhisperMessage(message, b.twitchClient)
|
|
|
|
})
|
|
|
|
|
2021-10-13 21:39:34 +02:00
|
|
|
err := b.twitchClient.Connect()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error Connecting from Twitch: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bot) Disconnect() error {
|
|
|
|
err := b.twitchClient.Disconnect()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error Disconnecting from Twitch: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2021-10-13 21:51:43 +02:00
|
|
|
|
|
|
|
func (b *Bot) Say(channel string, message string) {
|
2021-10-13 23:24:02 +02:00
|
|
|
b.twitchClient.Say(channel, message)
|
2021-10-13 21:51:43 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 23:29:29 +02:00
|
|
|
func (b *Bot) Join(channel string) {
|
|
|
|
log.Info("fn Join")
|
|
|
|
b.twitchClient.Join(channel)
|
|
|
|
}
|