twitch-bot/internal/actors/announce/actor.go
Knut Ahlers acf96c31ad
[core] Fix: Do not cache nil-TwitchClient
as client is assigned after plugins are initialized

Signed-off-by: Knut Ahlers <knut@ahlers.me>
2024-04-07 13:37:13 +02:00

45 lines
1.1 KiB
Go

// Package announce contains a chat essage handler to create
// announcements from the bot
package announce
import (
"context"
"regexp"
"github.com/pkg/errors"
"gopkg.in/irc.v4"
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
var (
botTwitchClient func() *twitch.Client
announceChatcommandRegex = regexp.MustCompile(`^/announce(|blue|green|orange|purple) +(.+)$`)
)
// Register provides the plugins.RegisterFunc
func Register(args plugins.RegistrationArguments) error {
botTwitchClient = args.GetTwitchClient
args.RegisterMessageModFunc("/announce", handleChatCommand)
return nil
}
func handleChatCommand(m *irc.Message) error {
channel := plugins.DeriveChannel(m, nil)
matches := announceChatcommandRegex.FindStringSubmatch(m.Trailing())
if matches == nil {
return errors.New("announce message does not match required format")
}
if err := botTwitchClient().SendChatAnnouncement(context.Background(), channel, matches[1], matches[2]); err != nil {
return errors.Wrap(err, "sending announcement")
}
return plugins.ErrSkipSendingMessage
}