2024-01-01 16:52:18 +00:00
|
|
|
// Package announce contains a chat essage handler to create
|
|
|
|
// announcements from the bot
|
2022-10-25 16:47:30 +00:00
|
|
|
package announce
|
|
|
|
|
|
|
|
import (
|
2024-01-01 16:52:18 +00:00
|
|
|
"context"
|
2022-10-25 16:47:30 +00:00
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2022-10-25 16:47:30 +00:00
|
|
|
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2022-10-25 16:47:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
botTwitchClient *twitch.Client
|
|
|
|
|
|
|
|
announceChatcommandRegex = regexp.MustCompile(`^/announce(|blue|green|orange|purple) +(.+)$`)
|
|
|
|
)
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Register provides the plugins.RegisterFunc
|
2022-10-25 16:47:30 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
if err := botTwitchClient.SendChatAnnouncement(context.Background(), channel, matches[1], matches[2]); err != nil {
|
2022-10-25 16:47:30 +00:00
|
|
|
return errors.Wrap(err, "sending announcement")
|
|
|
|
}
|
|
|
|
|
|
|
|
return plugins.ErrSkipSendingMessage
|
|
|
|
}
|