[core] Fix: Do not cache nil-TwitchClient

as client is assigned after plugins are initialized

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-04-05 12:19:22 +02:00
parent afe2963d33
commit acf96c31ad
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E
12 changed files with 43 additions and 43 deletions

View file

@ -14,14 +14,14 @@ import (
) )
var ( var (
botTwitchClient *twitch.Client botTwitchClient func() *twitch.Client
announceChatcommandRegex = regexp.MustCompile(`^/announce(|blue|green|orange|purple) +(.+)$`) announceChatcommandRegex = regexp.MustCompile(`^/announce(|blue|green|orange|purple) +(.+)$`)
) )
// Register provides the plugins.RegisterFunc // Register provides the plugins.RegisterFunc
func Register(args plugins.RegistrationArguments) error { func Register(args plugins.RegistrationArguments) error {
botTwitchClient = args.GetTwitchClient() botTwitchClient = args.GetTwitchClient
args.RegisterMessageModFunc("/announce", handleChatCommand) args.RegisterMessageModFunc("/announce", handleChatCommand)
@ -36,7 +36,7 @@ func handleChatCommand(m *irc.Message) error {
return errors.New("announce message does not match required format") return errors.New("announce message does not match required format")
} }
if err := botTwitchClient.SendChatAnnouncement(context.Background(), channel, matches[1], matches[2]); err != nil { if err := botTwitchClient().SendChatAnnouncement(context.Background(), channel, matches[1], matches[2]); err != nil {
return errors.Wrap(err, "sending announcement") return errors.Wrap(err, "sending announcement")
} }

View file

@ -20,7 +20,7 @@ import (
const actorName = "ban" const actorName = "ban"
var ( var (
botTwitchClient *twitch.Client botTwitchClient func() *twitch.Client
formatMessage plugins.MsgFormatter formatMessage plugins.MsgFormatter
banChatcommandRegex = regexp.MustCompile(`^/ban +([^\s]+) +(.+)$`) banChatcommandRegex = regexp.MustCompile(`^/ban +([^\s]+) +(.+)$`)
@ -28,7 +28,7 @@ var (
// Register provides the plugins.RegisterFunc // Register provides the plugins.RegisterFunc
func Register(args plugins.RegistrationArguments) (err error) { func Register(args plugins.RegistrationArguments) (err error) {
botTwitchClient = args.GetTwitchClient() botTwitchClient = args.GetTwitchClient
formatMessage = args.FormatMessage formatMessage = args.FormatMessage
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} }) args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
@ -98,7 +98,7 @@ func (actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *
} }
return false, errors.Wrap( return false, errors.Wrap(
botTwitchClient.BanUser( botTwitchClient().BanUser(
context.Background(), context.Background(),
plugins.DeriveChannel(m, eventData), plugins.DeriveChannel(m, eventData),
plugins.DeriveUser(m, eventData), plugins.DeriveUser(m, eventData),
@ -132,7 +132,7 @@ func handleAPIBan(w http.ResponseWriter, r *http.Request) {
reason = r.FormValue("reason") reason = r.FormValue("reason")
) )
if err := botTwitchClient.BanUser(r.Context(), channel, user, 0, reason); err != nil { if err := botTwitchClient().BanUser(r.Context(), channel, user, 0, reason); err != nil {
http.Error(w, errors.Wrap(err, "issuing ban").Error(), http.StatusInternalServerError) http.Error(w, errors.Wrap(err, "issuing ban").Error(), http.StatusInternalServerError)
return return
} }
@ -148,7 +148,7 @@ func handleChatCommand(m *irc.Message) error {
return errors.New("ban message does not match required format") return errors.New("ban message does not match required format")
} }
if err := botTwitchClient.BanUser(context.Background(), channel, matches[1], 0, matches[2]); err != nil { if err := botTwitchClient().BanUser(context.Background(), channel, matches[1], 0, matches[2]); err != nil {
return errors.Wrap(err, "executing ban") return errors.Wrap(err, "executing ban")
} }

View file

@ -18,13 +18,13 @@ import (
const actorName = "clipdetector" const actorName = "clipdetector"
var ( var (
botTwitchClient *twitch.Client botTwitchClient func() *twitch.Client
clipIDScanner = regexp.MustCompile(`(?:clips\.twitch\.tv|www\.twitch\.tv/[^/]*/clip)/([A-Za-z0-9_-]+)`) clipIDScanner = regexp.MustCompile(`(?:clips\.twitch\.tv|www\.twitch\.tv/[^/]*/clip)/([A-Za-z0-9_-]+)`)
) )
// Register provides the plugins.RegisterFunc // Register provides the plugins.RegisterFunc
func Register(args plugins.RegistrationArguments) error { func Register(args plugins.RegistrationArguments) error {
botTwitchClient = args.GetTwitchClient() botTwitchClient = args.GetTwitchClient
args.RegisterActor(actorName, func() plugins.Actor { return &Actor{} }) args.RegisterActor(actorName, func() plugins.Actor { return &Actor{} })
@ -64,7 +64,7 @@ func (Actor) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData *
continue continue
} }
clipInfo, err := botTwitchClient.GetClipByID(context.Background(), clipIDMatch[1]) clipInfo, err := botTwitchClient().GetClipByID(context.Background(), clipIDMatch[1])
if err != nil { if err != nil {
return false, errors.Wrap(err, "getting clip info") return false, errors.Wrap(err, "getting clip info")
} }

View file

@ -14,11 +14,11 @@ import (
const actorName = "delete" const actorName = "delete"
var botTwitchClient *twitch.Client var botTwitchClient func() *twitch.Client
// Register provides the plugins.RegisterFunc // Register provides the plugins.RegisterFunc
func Register(args plugins.RegistrationArguments) error { func Register(args plugins.RegistrationArguments) error {
botTwitchClient = args.GetTwitchClient() botTwitchClient = args.GetTwitchClient
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} }) args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
@ -40,7 +40,7 @@ func (actor) Execute(_ *irc.Client, m *irc.Message, _ *plugins.Rule, eventData *
} }
return false, errors.Wrap( return false, errors.Wrap(
botTwitchClient.DeleteMessage( botTwitchClient().DeleteMessage(
context.Background(), context.Background(),
plugins.DeriveChannel(m, eventData), plugins.DeriveChannel(m, eventData),
msgID, msgID,

View file

@ -22,13 +22,13 @@ import (
const actorName = "linkprotect" const actorName = "linkprotect"
var ( var (
botTwitchClient *twitch.Client botTwitchClient func() *twitch.Client
clipLink = regexp.MustCompile(`.*(?:clips\.twitch\.tv|www\.twitch\.tv/[^/]*/clip)/.*`) clipLink = regexp.MustCompile(`.*(?:clips\.twitch\.tv|www\.twitch\.tv/[^/]*/clip)/.*`)
) )
// Register provides the plugins.RegisterFunc // Register provides the plugins.RegisterFunc
func Register(args plugins.RegistrationArguments) error { func Register(args plugins.RegistrationArguments) error {
botTwitchClient = args.GetTwitchClient() botTwitchClient = args.GetTwitchClient
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} }) args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
@ -167,7 +167,7 @@ func (a actor) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData
// That message misbehaved so we need to punish them // That message misbehaved so we need to punish them
switch lt := attrs.MustString("action", helpers.Ptr("")); lt { switch lt := attrs.MustString("action", helpers.Ptr("")); lt {
case "ban": case "ban":
if err = botTwitchClient.BanUser( if err = botTwitchClient().BanUser(
context.Background(), context.Background(),
plugins.DeriveChannel(m, eventData), plugins.DeriveChannel(m, eventData),
strings.TrimLeft(plugins.DeriveUser(m, eventData), "@"), strings.TrimLeft(plugins.DeriveUser(m, eventData), "@"),
@ -183,7 +183,7 @@ func (a actor) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData
return false, errors.New("found no mesage id") return false, errors.New("found no mesage id")
} }
if err = botTwitchClient.DeleteMessage( if err = botTwitchClient().DeleteMessage(
context.Background(), context.Background(),
plugins.DeriveChannel(m, eventData), plugins.DeriveChannel(m, eventData),
msgID, msgID,
@ -197,7 +197,7 @@ func (a actor) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData
return false, errors.Wrap(err, "parsing punishment level") return false, errors.Wrap(err, "parsing punishment level")
} }
if err = botTwitchClient.BanUser( if err = botTwitchClient().BanUser(
context.Background(), context.Background(),
plugins.DeriveChannel(m, eventData), plugins.DeriveChannel(m, eventData),
strings.TrimLeft(plugins.DeriveUser(m, eventData), "@"), strings.TrimLeft(plugins.DeriveUser(m, eventData), "@"),

View file

@ -14,7 +14,7 @@ type (
func actionBan(channel, match, _, user string) error { func actionBan(channel, match, _, user string) error {
return errors.Wrap( return errors.Wrap(
botTwitchClient.BanUser( botTwitchClient().BanUser(
context.Background(), context.Background(),
channel, channel,
user, user,
@ -27,7 +27,7 @@ func actionBan(channel, match, _, user string) error {
func actionDelete(channel, _, msgid, _ string) (err error) { func actionDelete(channel, _, msgid, _ string) (err error) {
return errors.Wrap( return errors.Wrap(
botTwitchClient.DeleteMessage( botTwitchClient().DeleteMessage(
context.Background(), context.Background(),
channel, channel,
msgid, msgid,
@ -39,7 +39,7 @@ func actionDelete(channel, _, msgid, _ string) (err error) {
func getActionTimeout(duration time.Duration) actionFn { func getActionTimeout(duration time.Duration) actionFn {
return func(channel, match, _, user string) error { return func(channel, match, _, user string) error {
return errors.Wrap( return errors.Wrap(
botTwitchClient.BanUser( botTwitchClient().BanUser(
context.Background(), context.Background(),
channel, channel,
user, user,

View file

@ -27,7 +27,7 @@ const (
) )
var ( var (
botTwitchClient *twitch.Client botTwitchClient func() *twitch.Client
formatMessage plugins.MsgFormatter formatMessage plugins.MsgFormatter
messageStore = map[string][]*storedMessage{} messageStore = map[string][]*storedMessage{}
@ -36,7 +36,7 @@ var (
// Register provides the plugins.RegisterFunc // Register provides the plugins.RegisterFunc
func Register(args plugins.RegistrationArguments) error { func Register(args plugins.RegistrationArguments) error {
botTwitchClient = args.GetTwitchClient() botTwitchClient = args.GetTwitchClient
formatMessage = args.FormatMessage formatMessage = args.FormatMessage
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} }) args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })

View file

@ -28,7 +28,7 @@ const (
) )
var ( var (
botTwitchClient *twitch.Client botTwitchClient func() *twitch.Client
db database.Connector db database.Connector
formatMessage plugins.MsgFormatter formatMessage plugins.MsgFormatter
) )
@ -44,7 +44,7 @@ func Register(args plugins.RegistrationArguments) error {
return database.CopyObjects(src, target, &punishLevel{}) return database.CopyObjects(src, target, &punishLevel{})
}) })
botTwitchClient = args.GetTwitchClient() botTwitchClient = args.GetTwitchClient
formatMessage = args.FormatMessage formatMessage = args.FormatMessage
args.RegisterActor(actorNamePunish, func() plugins.Actor { return &actorPunish{} }) args.RegisterActor(actorNamePunish, func() plugins.Actor { return &actorPunish{} })
@ -172,7 +172,7 @@ func (actorPunish) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, event
switch lt := levels[nLvl]; lt { switch lt := levels[nLvl]; lt {
case "ban": case "ban":
if err = botTwitchClient.BanUser( if err = botTwitchClient().BanUser(
context.Background(), context.Background(),
plugins.DeriveChannel(m, eventData), plugins.DeriveChannel(m, eventData),
strings.TrimLeft(user, "@"), strings.TrimLeft(user, "@"),
@ -188,7 +188,7 @@ func (actorPunish) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, event
return false, errors.New("found no mesage id") return false, errors.New("found no mesage id")
} }
if err = botTwitchClient.DeleteMessage( if err = botTwitchClient().DeleteMessage(
context.Background(), context.Background(),
plugins.DeriveChannel(m, eventData), plugins.DeriveChannel(m, eventData),
msgID, msgID,
@ -202,7 +202,7 @@ func (actorPunish) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, event
return false, errors.Wrap(err, "parsing punishment level") return false, errors.Wrap(err, "parsing punishment level")
} }
if err = botTwitchClient.BanUser( if err = botTwitchClient().BanUser(
context.Background(), context.Background(),
plugins.DeriveChannel(m, eventData), plugins.DeriveChannel(m, eventData),
strings.TrimLeft(user, "@"), strings.TrimLeft(user, "@"),

View file

@ -17,11 +17,11 @@ import (
const actorName = "shield" const actorName = "shield"
var botTwitchClient *twitch.Client var botTwitchClient func() *twitch.Client
// Register provides the plugins.RegisterFunc // Register provides the plugins.RegisterFunc
func Register(args plugins.RegistrationArguments) error { func Register(args plugins.RegistrationArguments) error {
botTwitchClient = args.GetTwitchClient() botTwitchClient = args.GetTwitchClient
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} }) args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
@ -50,7 +50,7 @@ type actor struct{}
func (actor) Execute(_ *irc.Client, m *irc.Message, _ *plugins.Rule, eventData *fieldcollection.FieldCollection, attrs *fieldcollection.FieldCollection) (preventCooldown bool, err error) { func (actor) Execute(_ *irc.Client, m *irc.Message, _ *plugins.Rule, eventData *fieldcollection.FieldCollection, attrs *fieldcollection.FieldCollection) (preventCooldown bool, err error) {
return false, errors.Wrap( return false, errors.Wrap(
botTwitchClient.UpdateShieldMode( botTwitchClient().UpdateShieldMode(
context.Background(), context.Background(),
plugins.DeriveChannel(m, eventData), plugins.DeriveChannel(m, eventData),
attrs.MustBool("enable", helpers.Ptr(false)), attrs.MustBool("enable", helpers.Ptr(false)),

View file

@ -19,7 +19,7 @@ import (
const actorName = "shoutout" const actorName = "shoutout"
var ( var (
botTwitchClient *twitch.Client botTwitchClient func() *twitch.Client
formatMessage plugins.MsgFormatter formatMessage plugins.MsgFormatter
ptrStringEmpty = func(v string) *string { return &v }("") ptrStringEmpty = func(v string) *string { return &v }("")
@ -28,7 +28,7 @@ var (
// Register provides the plugins.RegisterFunc // Register provides the plugins.RegisterFunc
func Register(args plugins.RegistrationArguments) error { func Register(args plugins.RegistrationArguments) error {
botTwitchClient = args.GetTwitchClient() botTwitchClient = args.GetTwitchClient
formatMessage = args.FormatMessage formatMessage = args.FormatMessage
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} }) args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
@ -65,7 +65,7 @@ func (actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *
} }
return false, errors.Wrap( return false, errors.Wrap(
botTwitchClient.SendShoutout( botTwitchClient().SendShoutout(
context.Background(), context.Background(),
plugins.DeriveChannel(m, eventData), plugins.DeriveChannel(m, eventData),
user, user,
@ -96,7 +96,7 @@ func handleChatCommand(m *irc.Message) error {
return errors.New("shoutout message does not match required format") return errors.New("shoutout message does not match required format")
} }
if err := botTwitchClient.SendShoutout(context.Background(), channel, matches[1]); err != nil { if err := botTwitchClient().SendShoutout(context.Background(), channel, matches[1]); err != nil {
return errors.Wrap(err, "executing shoutout") return errors.Wrap(err, "executing shoutout")
} }

View file

@ -20,7 +20,7 @@ import (
const actorName = "timeout" const actorName = "timeout"
var ( var (
botTwitchClient *twitch.Client botTwitchClient func() *twitch.Client
formatMessage plugins.MsgFormatter formatMessage plugins.MsgFormatter
ptrStringEmpty = func(v string) *string { return &v }("") ptrStringEmpty = func(v string) *string { return &v }("")
@ -29,7 +29,7 @@ var (
// Register provides the plugins.RegisterFunc // Register provides the plugins.RegisterFunc
func Register(args plugins.RegistrationArguments) error { func Register(args plugins.RegistrationArguments) error {
botTwitchClient = args.GetTwitchClient() botTwitchClient = args.GetTwitchClient
formatMessage = args.FormatMessage formatMessage = args.FormatMessage
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} }) args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
@ -75,7 +75,7 @@ func (actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *
} }
return false, errors.Wrap( return false, errors.Wrap(
botTwitchClient.BanUser( botTwitchClient().BanUser(
context.Background(), context.Background(),
plugins.DeriveChannel(m, eventData), plugins.DeriveChannel(m, eventData),
plugins.DeriveUser(m, eventData), plugins.DeriveUser(m, eventData),
@ -118,7 +118,7 @@ func handleChatCommand(m *irc.Message) error {
return errors.Wrap(err, "parsing timeout duration") return errors.Wrap(err, "parsing timeout duration")
} }
if err = botTwitchClient.BanUser(context.Background(), channel, matches[1], time.Duration(duration)*time.Second, matches[3]); err != nil { if err = botTwitchClient().BanUser(context.Background(), channel, matches[1], time.Duration(duration)*time.Second, matches[3]); err != nil {
return errors.Wrap(err, "executing timeout") return errors.Wrap(err, "executing timeout")
} }

View file

@ -17,13 +17,13 @@ import (
const actorName = "whisper" const actorName = "whisper"
var ( var (
botTwitchClient *twitch.Client botTwitchClient func() *twitch.Client
formatMessage plugins.MsgFormatter formatMessage plugins.MsgFormatter
) )
// Register provides the plugins.RegisterFunc // Register provides the plugins.RegisterFunc
func Register(args plugins.RegistrationArguments) error { func Register(args plugins.RegistrationArguments) error {
botTwitchClient = args.GetTwitchClient() botTwitchClient = args.GetTwitchClient
formatMessage = args.FormatMessage formatMessage = args.FormatMessage
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} }) args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
@ -72,7 +72,7 @@ func (actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *
} }
return false, errors.Wrap( return false, errors.Wrap(
botTwitchClient.SendWhisper(context.Background(), to, msg), botTwitchClient().SendWhisper(context.Background(), to, msg),
"sending whisper", "sending whisper",
) )
} }