SolMonitor/main.go

180 lines
4.4 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"os"
"regexp"
2024-06-18 17:52:38 +02:00
"github.com/BurntSushi/toml"
"github.com/bwmarrin/discordgo"
2024-06-18 17:52:38 +02:00
"git.directme.in/Joren/SmsHook/ringbuffer"
)
2024-06-18 17:52:38 +02:00
var (
Token string
Servers []ServerConfig
CoinRegexes map[string]*regexp.Regexp
ChannelBlacklist map[string][]string
2024-06-18 17:52:38 +02:00
MessageHistory map[string]*ringbuffer.RingBuffer
)
type ServerConfig struct {
ServerID string `toml:"SERVER_ID"`
OutputChannelID string `toml:"OUTPUT_CHANNEL_ID"`
CoinRegexes map[string]string `toml:"COIN_REGEXES"`
ChannelBlacklist struct {
Channels []string `toml:"CHANNELS"`
} `toml:"CHANNEL_BLACKLIST"`
2024-06-18 17:52:38 +02:00
PrefixEnabled bool `toml:"PREFIX_ENABLED"`
AllowDuplicates bool `toml:"ALLOW_DUPLICATES"`
}
func init() {
var config struct {
Discord struct {
BotToken string `toml:"BOT_TOKEN"`
} `toml:"DISCORD"`
Servers []ServerConfig `toml:"SERVERS"`
}
2024-06-18 17:06:13 +02:00
if _, err := toml.DecodeFile("config.toml", &config); err != nil {
fmt.Println("Error loading config:", err)
os.Exit(1)
}
2024-06-18 17:06:13 +02:00
Token = config.Discord.BotToken
Servers = config.Servers
2024-06-18 17:06:13 +02:00
CoinRegexes = make(map[string]*regexp.Regexp)
ChannelBlacklist = make(map[string][]string)
2024-06-18 17:52:38 +02:00
MessageHistory = make(map[string]*ringbuffer.RingBuffer)
2024-06-18 17:06:13 +02:00
for _, server := range Servers {
for coin, regex := range server.CoinRegexes {
fullRegex := fmt.Sprintf(`\b%s\b`, regex)
CoinRegexes[coin] = regexp.MustCompile(fullRegex)
2024-06-18 17:06:13 +02:00
}
ChannelBlacklist[server.ServerID] = server.ChannelBlacklist.Channels
2024-06-18 17:52:38 +02:00
// Initialize MessageHistory for each server
MessageHistory[server.ServerID] = ringbuffer.NewRingBuffer(100) // Adjust buffer size as needed
2024-06-18 16:29:58 +02:00
}
if Token == "" {
fmt.Println("No token provided in config.toml.")
os.Exit(1)
}
}
2024-06-18 16:29:58 +02:00
func main() {
dg, err := discordgo.New("Bot " + Token)
if err != nil {
fmt.Println("Error creating Discord session:", err)
return
}
2024-06-18 16:29:58 +02:00
dg.AddHandler(ready)
dg.AddHandler(messageCreate)
2024-06-18 16:29:58 +02:00
dg.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentMessageContent
2024-06-18 16:26:10 +02:00
err = dg.Open()
if err != nil {
fmt.Println("Error opening connection:", err)
return
2024-06-18 16:26:10 +02:00
}
fmt.Println("Bot is now running. Press CTRL+C to exit.")
select {}
}
func ready(s *discordgo.Session, event *discordgo.Ready) {
s.UpdateGameStatus(0, "Monitoring messages")
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
2024-06-18 16:26:10 +02:00
}
for _, server := range Servers {
if m.GuildID == server.ServerID {
channel, err := s.State.Channel(m.ChannelID)
if err != nil {
fmt.Println("Error getting channel:", err)
return
}
2024-06-18 16:26:10 +02:00
if isChannelBlacklisted(server.ServerID, channel.ID) {
return
}
2024-06-18 16:29:58 +02:00
checkMessageContent(s, server, m)
checkEmbeds(s, server, m)
2024-06-18 17:06:13 +02:00
return
}
}
}
func checkMessageContent(s *discordgo.Session, server ServerConfig, m *discordgo.MessageCreate) {
for coin, regex := range CoinRegexes {
match := regex.FindStringSubmatch(m.Content)
if len(match) > 0 {
matchedAddress := match[0]
message := formatMessage(server, coin, m.Author.Username, matchedAddress)
2024-06-18 17:52:38 +02:00
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)
2024-06-18 17:52:38 +02:00
MessageHistory[server.ServerID].Add(message)
}
}
}
func checkEmbeds(s *discordgo.Session, server ServerConfig, m *discordgo.MessageCreate) {
for _, embed := range m.Message.Embeds {
if embed.Type == "rich" && embed.Description != "" {
for coin, regex := range CoinRegexes {
match := regex.FindStringSubmatch(embed.Description)
if len(match) > 0 {
matchedAddress := match[0]
message := formatMessage(server, coin, m.Author.Username, matchedAddress)
2024-06-18 17:52:38 +02:00
if !server.AllowDuplicates && MessageHistory[server.ServerID].ContainsItem(message) {
return
}
s.ChannelMessageSend(server.OutputChannelID, message)
2024-06-18 17:52:38 +02:00
MessageHistory[server.ServerID].Add(message)
}
}
}
}
}
2024-06-18 16:29:58 +02:00
func formatMessage(server ServerConfig, coin, username, address string) string {
if server.PrefixEnabled {
return fmt.Sprintf("%s: %s", coin, address)
}
return address
}
2024-06-18 16:29:58 +02:00
func isChannelBlacklisted(serverID, channelID string) bool {
blacklistedChannels, ok := ChannelBlacklist[serverID]
if !ok {
return false
}
2024-06-18 16:29:58 +02:00
for _, id := range blacklistedChannels {
if id == channelID {
return true
}
2024-06-18 17:06:13 +02:00
}
return false
}
2024-06-18 16:26:10 +02:00