diff --git a/internal/actors/announce/actor.go b/internal/actors/announce/actor.go index 10bf629..e1c4014 100644 --- a/internal/actors/announce/actor.go +++ b/internal/actors/announce/actor.go @@ -14,14 +14,14 @@ import ( ) var ( - botTwitchClient *twitch.Client + 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() + botTwitchClient = args.GetTwitchClient args.RegisterMessageModFunc("/announce", handleChatCommand) @@ -36,7 +36,7 @@ func handleChatCommand(m *irc.Message) error { 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") } diff --git a/internal/actors/ban/actor.go b/internal/actors/ban/actor.go index 918be39..5c2fc43 100644 --- a/internal/actors/ban/actor.go +++ b/internal/actors/ban/actor.go @@ -20,7 +20,7 @@ import ( const actorName = "ban" var ( - botTwitchClient *twitch.Client + botTwitchClient func() *twitch.Client formatMessage plugins.MsgFormatter banChatcommandRegex = regexp.MustCompile(`^/ban +([^\s]+) +(.+)$`) @@ -28,7 +28,7 @@ var ( // Register provides the plugins.RegisterFunc func Register(args plugins.RegistrationArguments) (err error) { - botTwitchClient = args.GetTwitchClient() + botTwitchClient = args.GetTwitchClient formatMessage = args.FormatMessage 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( - botTwitchClient.BanUser( + botTwitchClient().BanUser( context.Background(), plugins.DeriveChannel(m, eventData), plugins.DeriveUser(m, eventData), @@ -132,7 +132,7 @@ func handleAPIBan(w http.ResponseWriter, r *http.Request) { 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) return } @@ -148,7 +148,7 @@ func handleChatCommand(m *irc.Message) error { 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") } diff --git a/internal/actors/clipdetector/actor.go b/internal/actors/clipdetector/actor.go index 10251ad..3891f64 100644 --- a/internal/actors/clipdetector/actor.go +++ b/internal/actors/clipdetector/actor.go @@ -18,13 +18,13 @@ import ( const actorName = "clipdetector" var ( - botTwitchClient *twitch.Client + botTwitchClient func() *twitch.Client clipIDScanner = regexp.MustCompile(`(?:clips\.twitch\.tv|www\.twitch\.tv/[^/]*/clip)/([A-Za-z0-9_-]+)`) ) // Register provides the plugins.RegisterFunc func Register(args plugins.RegistrationArguments) error { - botTwitchClient = args.GetTwitchClient() + botTwitchClient = args.GetTwitchClient 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 } - clipInfo, err := botTwitchClient.GetClipByID(context.Background(), clipIDMatch[1]) + clipInfo, err := botTwitchClient().GetClipByID(context.Background(), clipIDMatch[1]) if err != nil { return false, errors.Wrap(err, "getting clip info") } diff --git a/internal/actors/delete/actor.go b/internal/actors/delete/actor.go index 5052fe1..304a033 100644 --- a/internal/actors/delete/actor.go +++ b/internal/actors/delete/actor.go @@ -14,11 +14,11 @@ import ( const actorName = "delete" -var botTwitchClient *twitch.Client +var botTwitchClient func() *twitch.Client // Register provides the plugins.RegisterFunc func Register(args plugins.RegistrationArguments) error { - botTwitchClient = args.GetTwitchClient() + botTwitchClient = args.GetTwitchClient 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( - botTwitchClient.DeleteMessage( + botTwitchClient().DeleteMessage( context.Background(), plugins.DeriveChannel(m, eventData), msgID, diff --git a/internal/actors/linkprotect/actor.go b/internal/actors/linkprotect/actor.go index bf6f596..39931ec 100644 --- a/internal/actors/linkprotect/actor.go +++ b/internal/actors/linkprotect/actor.go @@ -22,13 +22,13 @@ import ( const actorName = "linkprotect" var ( - botTwitchClient *twitch.Client + botTwitchClient func() *twitch.Client clipLink = regexp.MustCompile(`.*(?:clips\.twitch\.tv|www\.twitch\.tv/[^/]*/clip)/.*`) ) // Register provides the plugins.RegisterFunc func Register(args plugins.RegistrationArguments) error { - botTwitchClient = args.GetTwitchClient() + botTwitchClient = args.GetTwitchClient 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 switch lt := attrs.MustString("action", helpers.Ptr("")); lt { case "ban": - if err = botTwitchClient.BanUser( + if err = botTwitchClient().BanUser( context.Background(), plugins.DeriveChannel(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") } - if err = botTwitchClient.DeleteMessage( + if err = botTwitchClient().DeleteMessage( context.Background(), plugins.DeriveChannel(m, eventData), 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") } - if err = botTwitchClient.BanUser( + if err = botTwitchClient().BanUser( context.Background(), plugins.DeriveChannel(m, eventData), strings.TrimLeft(plugins.DeriveUser(m, eventData), "@"), diff --git a/internal/actors/nuke/actions.go b/internal/actors/nuke/actions.go index 9af6397..2acc5d0 100644 --- a/internal/actors/nuke/actions.go +++ b/internal/actors/nuke/actions.go @@ -14,7 +14,7 @@ type ( func actionBan(channel, match, _, user string) error { return errors.Wrap( - botTwitchClient.BanUser( + botTwitchClient().BanUser( context.Background(), channel, user, @@ -27,7 +27,7 @@ func actionBan(channel, match, _, user string) error { func actionDelete(channel, _, msgid, _ string) (err error) { return errors.Wrap( - botTwitchClient.DeleteMessage( + botTwitchClient().DeleteMessage( context.Background(), channel, msgid, @@ -39,7 +39,7 @@ func actionDelete(channel, _, msgid, _ string) (err error) { func getActionTimeout(duration time.Duration) actionFn { return func(channel, match, _, user string) error { return errors.Wrap( - botTwitchClient.BanUser( + botTwitchClient().BanUser( context.Background(), channel, user, diff --git a/internal/actors/nuke/actor.go b/internal/actors/nuke/actor.go index bf44df8..397a484 100644 --- a/internal/actors/nuke/actor.go +++ b/internal/actors/nuke/actor.go @@ -27,7 +27,7 @@ const ( ) var ( - botTwitchClient *twitch.Client + botTwitchClient func() *twitch.Client formatMessage plugins.MsgFormatter messageStore = map[string][]*storedMessage{} @@ -36,7 +36,7 @@ var ( // Register provides the plugins.RegisterFunc func Register(args plugins.RegistrationArguments) error { - botTwitchClient = args.GetTwitchClient() + botTwitchClient = args.GetTwitchClient formatMessage = args.FormatMessage args.RegisterActor(actorName, func() plugins.Actor { return &actor{} }) diff --git a/internal/actors/punish/actor.go b/internal/actors/punish/actor.go index eacb894..9bd1f21 100644 --- a/internal/actors/punish/actor.go +++ b/internal/actors/punish/actor.go @@ -28,7 +28,7 @@ const ( ) var ( - botTwitchClient *twitch.Client + botTwitchClient func() *twitch.Client db database.Connector formatMessage plugins.MsgFormatter ) @@ -44,7 +44,7 @@ func Register(args plugins.RegistrationArguments) error { return database.CopyObjects(src, target, &punishLevel{}) }) - botTwitchClient = args.GetTwitchClient() + botTwitchClient = args.GetTwitchClient formatMessage = args.FormatMessage 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 { case "ban": - if err = botTwitchClient.BanUser( + if err = botTwitchClient().BanUser( context.Background(), plugins.DeriveChannel(m, eventData), 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") } - if err = botTwitchClient.DeleteMessage( + if err = botTwitchClient().DeleteMessage( context.Background(), plugins.DeriveChannel(m, eventData), 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") } - if err = botTwitchClient.BanUser( + if err = botTwitchClient().BanUser( context.Background(), plugins.DeriveChannel(m, eventData), strings.TrimLeft(user, "@"), diff --git a/internal/actors/shield/actor.go b/internal/actors/shield/actor.go index 6fd197e..b6eb0a3 100644 --- a/internal/actors/shield/actor.go +++ b/internal/actors/shield/actor.go @@ -17,11 +17,11 @@ import ( const actorName = "shield" -var botTwitchClient *twitch.Client +var botTwitchClient func() *twitch.Client // Register provides the plugins.RegisterFunc func Register(args plugins.RegistrationArguments) error { - botTwitchClient = args.GetTwitchClient() + botTwitchClient = args.GetTwitchClient 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) { return false, errors.Wrap( - botTwitchClient.UpdateShieldMode( + botTwitchClient().UpdateShieldMode( context.Background(), plugins.DeriveChannel(m, eventData), attrs.MustBool("enable", helpers.Ptr(false)), diff --git a/internal/actors/shoutout/actor.go b/internal/actors/shoutout/actor.go index 612e51b..c3d2827 100644 --- a/internal/actors/shoutout/actor.go +++ b/internal/actors/shoutout/actor.go @@ -19,7 +19,7 @@ import ( const actorName = "shoutout" var ( - botTwitchClient *twitch.Client + botTwitchClient func() *twitch.Client formatMessage plugins.MsgFormatter ptrStringEmpty = func(v string) *string { return &v }("") @@ -28,7 +28,7 @@ var ( // Register provides the plugins.RegisterFunc func Register(args plugins.RegistrationArguments) error { - botTwitchClient = args.GetTwitchClient() + botTwitchClient = args.GetTwitchClient formatMessage = args.FormatMessage 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( - botTwitchClient.SendShoutout( + botTwitchClient().SendShoutout( context.Background(), plugins.DeriveChannel(m, eventData), user, @@ -96,7 +96,7 @@ func handleChatCommand(m *irc.Message) error { 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") } diff --git a/internal/actors/timeout/actor.go b/internal/actors/timeout/actor.go index 0c3c985..e84e18d 100644 --- a/internal/actors/timeout/actor.go +++ b/internal/actors/timeout/actor.go @@ -20,7 +20,7 @@ import ( const actorName = "timeout" var ( - botTwitchClient *twitch.Client + botTwitchClient func() *twitch.Client formatMessage plugins.MsgFormatter ptrStringEmpty = func(v string) *string { return &v }("") @@ -29,7 +29,7 @@ var ( // Register provides the plugins.RegisterFunc func Register(args plugins.RegistrationArguments) error { - botTwitchClient = args.GetTwitchClient() + botTwitchClient = args.GetTwitchClient formatMessage = args.FormatMessage 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( - botTwitchClient.BanUser( + botTwitchClient().BanUser( context.Background(), plugins.DeriveChannel(m, eventData), plugins.DeriveUser(m, eventData), @@ -118,7 +118,7 @@ func handleChatCommand(m *irc.Message) error { 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") } diff --git a/internal/actors/whisper/actor.go b/internal/actors/whisper/actor.go index 48bee1c..3f00c87 100644 --- a/internal/actors/whisper/actor.go +++ b/internal/actors/whisper/actor.go @@ -17,13 +17,13 @@ import ( const actorName = "whisper" var ( - botTwitchClient *twitch.Client + botTwitchClient func() *twitch.Client formatMessage plugins.MsgFormatter ) // Register provides the plugins.RegisterFunc func Register(args plugins.RegistrationArguments) error { - botTwitchClient = args.GetTwitchClient() + botTwitchClient = args.GetTwitchClient formatMessage = args.FormatMessage 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( - botTwitchClient.SendWhisper(context.Background(), to, msg), + botTwitchClient().SendWhisper(context.Background(), to, msg), "sending whisper", ) }