Add option to allow dupes

This commit is contained in:
Joren 2024-06-18 17:52:38 +02:00
parent dca627e422
commit 8fb0838cc0
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55
2 changed files with 23 additions and 2 deletions

1
go.mod
View File

@ -3,6 +3,7 @@ module git.directme.in/Joren/SolMonitor
go 1.22.4 go 1.22.4
require ( require (
git.directme.in/Joren/SmsHook v1.0.2
github.com/BurntSushi/toml v1.4.0 github.com/BurntSushi/toml v1.4.0
github.com/bwmarrin/discordgo v0.28.1 github.com/bwmarrin/discordgo v0.28.1
) )

22
main.go
View File

@ -5,16 +5,19 @@ import (
"os" "os"
"regexp" "regexp"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"git.directme.in/Joren/SmsHook/ringbuffer"
) )
var ( var (
Token string Token string
Servers []ServerConfig Servers []ServerConfig
CoinRegexes map[string]*regexp.Regexp CoinRegexes map[string]*regexp.Regexp
ChannelBlacklist map[string][]string ChannelBlacklist map[string][]string
PrefixEnabled bool MessageHistory map[string]*ringbuffer.RingBuffer
) )
type ServerConfig struct { type ServerConfig struct {
@ -25,6 +28,7 @@ type ServerConfig struct {
Channels []string `toml:"CHANNELS"` Channels []string `toml:"CHANNELS"`
} `toml:"CHANNEL_BLACKLIST"` } `toml:"CHANNEL_BLACKLIST"`
PrefixEnabled bool `toml:"PREFIX_ENABLED"` PrefixEnabled bool `toml:"PREFIX_ENABLED"`
AllowDuplicates bool `toml:"ALLOW_DUPLICATES"`
} }
func init() { func init() {
@ -45,6 +49,7 @@ func init() {
CoinRegexes = make(map[string]*regexp.Regexp) CoinRegexes = make(map[string]*regexp.Regexp)
ChannelBlacklist = make(map[string][]string) ChannelBlacklist = make(map[string][]string)
MessageHistory = make(map[string]*ringbuffer.RingBuffer)
for _, server := range Servers { for _, server := range Servers {
for coin, regex := range server.CoinRegexes { for coin, regex := range server.CoinRegexes {
@ -52,6 +57,9 @@ func init() {
CoinRegexes[coin] = regexp.MustCompile(fullRegex) CoinRegexes[coin] = regexp.MustCompile(fullRegex)
} }
ChannelBlacklist[server.ServerID] = server.ChannelBlacklist.Channels ChannelBlacklist[server.ServerID] = server.ChannelBlacklist.Channels
// Initialize MessageHistory for each server
MessageHistory[server.ServerID] = ringbuffer.NewRingBuffer(100) // Adjust buffer size as needed
} }
if Token == "" { if Token == "" {
@ -116,7 +124,13 @@ func checkMessageContent(s *discordgo.Session, server ServerConfig, m *discordgo
if len(match) > 0 { if len(match) > 0 {
matchedAddress := match[0] matchedAddress := match[0]
message := formatMessage(server, coin, m.Author.Username, matchedAddress) 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) 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 { if len(match) > 0 {
matchedAddress := match[0] matchedAddress := match[0]
message := formatMessage(server, coin, m.Author.Username, matchedAddress) message := formatMessage(server, coin, m.Author.Username, matchedAddress)
if !server.AllowDuplicates && MessageHistory[server.ServerID].ContainsItem(message) {
return
}
s.ChannelMessageSend(server.OutputChannelID, message) s.ChannelMessageSend(server.OutputChannelID, message)
MessageHistory[server.ServerID].Add(message)
} }
} }
} }