From 8fb0838cc070af61d48a212eacf36574d6d89e2c Mon Sep 17 00:00:00 2001 From: Joren Date: Tue, 18 Jun 2024 17:52:38 +0200 Subject: [PATCH] Add option to allow dupes --- go.mod | 1 + main.go | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index fcc43de..9d5647b 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.directme.in/Joren/SolMonitor go 1.22.4 require ( + git.directme.in/Joren/SmsHook v1.0.2 github.com/BurntSushi/toml v1.4.0 github.com/bwmarrin/discordgo v0.28.1 ) diff --git a/main.go b/main.go index fb56513..39f9d8c 100644 --- a/main.go +++ b/main.go @@ -5,16 +5,19 @@ import ( "os" "regexp" + "github.com/BurntSushi/toml" "github.com/bwmarrin/discordgo" + "git.directme.in/Joren/SmsHook/ringbuffer" ) + var ( Token string Servers []ServerConfig CoinRegexes map[string]*regexp.Regexp ChannelBlacklist map[string][]string - PrefixEnabled bool + MessageHistory map[string]*ringbuffer.RingBuffer ) type ServerConfig struct { @@ -24,7 +27,8 @@ type ServerConfig struct { ChannelBlacklist struct { Channels []string `toml:"CHANNELS"` } `toml:"CHANNEL_BLACKLIST"` - PrefixEnabled bool `toml:"PREFIX_ENABLED"` + PrefixEnabled bool `toml:"PREFIX_ENABLED"` + AllowDuplicates bool `toml:"ALLOW_DUPLICATES"` } func init() { @@ -45,6 +49,7 @@ func init() { CoinRegexes = make(map[string]*regexp.Regexp) ChannelBlacklist = make(map[string][]string) + MessageHistory = make(map[string]*ringbuffer.RingBuffer) for _, server := range Servers { for coin, regex := range server.CoinRegexes { @@ -52,6 +57,9 @@ func init() { CoinRegexes[coin] = regexp.MustCompile(fullRegex) } ChannelBlacklist[server.ServerID] = server.ChannelBlacklist.Channels + + // Initialize MessageHistory for each server + MessageHistory[server.ServerID] = ringbuffer.NewRingBuffer(100) // Adjust buffer size as needed } if Token == "" { @@ -116,7 +124,13 @@ func checkMessageContent(s *discordgo.Session, server ServerConfig, m *discordgo if len(match) > 0 { matchedAddress := match[0] message := formatMessage(server, coin, m.Author.Username, matchedAddress) + + if !server.AllowDuplicates && MessageHistory[server.ServerID].ContainsItem(message) { + return // Skip sending if duplicates are not allowed and message is in history + } + s.ChannelMessageSend(server.OutputChannelID, message) + MessageHistory[server.ServerID].Add(message) } } } @@ -129,7 +143,13 @@ func checkEmbeds(s *discordgo.Session, server ServerConfig, m *discordgo.Message if len(match) > 0 { matchedAddress := match[0] message := formatMessage(server, coin, m.Author.Username, matchedAddress) + + if !server.AllowDuplicates && MessageHistory[server.ServerID].ContainsItem(message) { + return + } + s.ChannelMessageSend(server.OutputChannelID, message) + MessageHistory[server.ServerID].Add(message) } } }