ImplMessageMonitor #1

Merged
Joren merged 9 commits from ImplMessageMonitor into main 2024-06-18 17:55:06 +02:00
2 changed files with 23 additions and 2 deletions
Showing only changes of commit 8fb0838cc0 - Show all commits

1
go.mod
View File

@ -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
)

24
main.go
View File

@ -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)
}
}
}