mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-09 00:30:02 +00:00
Knut Ahlers
acf96c31ad
as client is assigned after plugins are initialized Signed-off-by: Knut Ahlers <knut@ahlers.me>
44 lines
1.1 KiB
Go
44 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
|
|
}
|