From 32254856334a6d557f26a2a98ed065df356d0f5c Mon Sep 17 00:00:00 2001 From: gempir Date: Sun, 8 Jan 2023 19:45:57 +0100 Subject: [PATCH] prevent double messages by keeping ids in map, clearchat has no ID so still might be double --- bot/main.go | 14 ++++++++++++++ go.mod | 1 + go.sum | 4 ++++ 3 files changed, 19 insertions(+) diff --git a/bot/main.go b/bot/main.go index 536181b..0203524 100644 --- a/bot/main.go +++ b/bot/main.go @@ -8,6 +8,7 @@ import ( "github.com/gempir/justlog/config" "github.com/gempir/justlog/filelog" + expiremap "github.com/nursik/go-expire-map" twitch "github.com/gempir/go-twitch-irc/v3" "github.com/gempir/justlog/helix" @@ -24,6 +25,7 @@ type Bot struct { channels map[string]helix.UserData clearchats sync.Map OptoutCodes sync.Map + msgMap *expiremap.ExpireMap } type worker struct { @@ -52,6 +54,7 @@ func NewBot(cfg *config.Config, helixClient helix.TwitchApiClient, fileLogger fi channels: channels, worker: []*worker{}, OptoutCodes: sync.Map{}, + msgMap: expiremap.New(), } } @@ -72,6 +75,7 @@ func (b *Bot) Connect() { log.Info("[bot] joining as user " + b.cfg.Username) } + defer b.msgMap.Close() log.Fatal(client.Connect()) } @@ -146,6 +150,11 @@ func (b *Bot) newClient() *twitch.Client { } func (b *Bot) handlePrivateMessage(message twitch.PrivateMessage) { + if _, ok := b.msgMap.Get(message.ID); ok { + return + } + b.msgMap.Set(message.ID, true, time.Second*3) + b.handlePrivateMessageCommands(message) if b.cfg.IsOptedOut(message.User.ID) || b.cfg.IsOptedOut(message.RoomID) { @@ -168,6 +177,11 @@ func (b *Bot) handlePrivateMessage(message twitch.PrivateMessage) { } func (b *Bot) handleUserNotice(message twitch.UserNoticeMessage) { + if _, ok := b.msgMap.Get(message.ID); ok { + return + } + b.msgMap.Set(message.ID, true, time.Second*3) + if b.cfg.IsOptedOut(message.User.ID) || b.cfg.IsOptedOut(message.RoomID) { return } diff --git a/go.mod b/go.mod index 2c30bcc..f4f5349 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.16 require ( github.com/gempir/go-twitch-irc/v3 v3.0.0 github.com/nicklaw5/helix v1.25.0 + github.com/nursik/go-expire-map v1.1.0 // indirect github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.7.0 // indirect ) diff --git a/go.sum b/go.sum index 27020d2..cd6e800 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,10 @@ github.com/golang-jwt/jwt v3.2.1+incompatible h1:73Z+4BJcrTC+KczS6WvTPvRGOp1WmfE github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/nicklaw5/helix v1.25.0 h1:Mrz537izZVsGdM3I46uGAAlslj61frgkhS/9xQqyT/M= github.com/nicklaw5/helix v1.25.0/go.mod h1:yvXZFapT6afIoxnAvlWiJiUMsYnoHl7tNs+t0bloAMw= +github.com/nursik/go-expire-map v1.1.0 h1:C+OJ81JtHDSPJXfuu0g3e8RRjHLd5of8dVzyfDOB9KY= +github.com/nursik/go-expire-map v1.1.0/go.mod h1:wdQsai5n32Uw1IuVXXZoopePGCFh5vb0Dka/TRcboHs= +github.com/nursik/go-ordered-set v0.0.0-20190626022851-0e8872c36517 h1:jau4pavdQo5lHeVTjZEGrm4+zvVGZj8SFQt4awsLLXE= +github.com/nursik/go-ordered-set v0.0.0-20190626022851-0e8872c36517/go.mod h1:qFI7Mmmx8i+Qz8a52FarvpgPQzylvD3w77JAwvnFtKg= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=