SolMonitor/main.go

204 lines
5.2 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"os"
"regexp"
2024-06-27 14:39:17 +02:00
"strings"
"github.com/BurntSushi/toml"
"github.com/bwmarrin/discordgo"
2024-06-18 17:52:38 +02:00
"git.directme.in/Joren/SmsHook/ringbuffer"
)
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 {
2024-06-26 23:07:58 +02:00
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-26 23:07:58 +02:00
PrefixEnabled bool `toml:"PREFIX_ENABLED"`
AllowDuplicates bool `toml:"ALLOW_DUPLICATES"`
ServerIDEnabled bool `toml:"SERVER_ID_ENABLED"`
ChannelIDEnabled bool `toml:"CHANNEL_ID_ENABLED"`
MessageLinkEnabled bool `toml:"MESSAGE_LINK_ENABLED"`
}
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:53:19 +02:00
MessageHistory[server.ServerID] = ringbuffer.NewRingBuffer(4096)
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
2024-06-26 23:07:58 +02:00
guild, err := s.Guild(m.GuildID)
if err != nil {
fmt.Println("Error getting guild:", err)
return
}
checkMessageContent(s, server, m, guild.Name)
checkEmbeds(s, server, m, guild.Name)
2024-06-18 17:06:13 +02:00
return
}
}
}
2024-06-26 23:07:58 +02:00
func checkMessageContent(s *discordgo.Session, server ServerConfig, m *discordgo.MessageCreate, serverName string) {
for coin, regex := range CoinRegexes {
match := regex.FindStringSubmatch(m.Content)
if len(match) > 0 {
matchedAddress := match[0]
2024-06-27 14:39:17 +02:00
matchedAddress = strings.TrimSpace(matchedAddress)
message := formatMessage(server, coin, m.Author.Username, matchedAddress, serverName, m)
2024-06-18 17:52:38 +02:00
2024-06-27 14:39:17 +02:00
if !server.AllowDuplicates && MessageHistory[server.ServerID].ContainsItem(matchedAddress) {
2024-06-18 17:53:19 +02:00
return
2024-06-18 17:52:38 +02:00
}
s.ChannelMessageSend(server.OutputChannelID, message)
2024-06-27 14:39:17 +02:00
MessageHistory[server.ServerID].Add(matchedAddress)
}
}
}
2024-06-26 23:07:58 +02:00
func checkEmbeds(s *discordgo.Session, server ServerConfig, m *discordgo.MessageCreate, serverName string) {
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]
2024-06-27 14:39:17 +02:00
matchedAddress = strings.TrimSpace(matchedAddress)
message := formatMessage(server, coin, m.Author.Username, matchedAddress, serverName, m)
2024-06-18 17:52:38 +02:00
2024-06-27 14:39:17 +02:00
if !server.AllowDuplicates && MessageHistory[server.ServerID].ContainsItem(matchedAddress) {
2024-06-18 17:52:38 +02:00
return
}
s.ChannelMessageSend(server.OutputChannelID, message)
2024-06-27 14:39:17 +02:00
MessageHistory[server.ServerID].Add(matchedAddress)
}
}
}
}
}
2024-06-18 16:29:58 +02:00
func formatMessage(server ServerConfig, coin, username, address, serverName string, m *discordgo.MessageCreate) string {
2024-06-26 23:07:58 +02:00
message := address
if server.PrefixEnabled {
2024-06-26 23:07:58 +02:00
message = fmt.Sprintf("%s: %s", coin, address)
}
2024-06-26 23:07:58 +02:00
if server.ServerIDEnabled {
message = fmt.Sprintf("%s (Server: %s)", message, serverName)
}
if server.ChannelIDEnabled {
message = fmt.Sprintf("%s (From: <#%s>)", message, m.ChannelID)
}
if server.MessageLinkEnabled {
message = fmt.Sprintf("%s ([Link](<https://discord.com/channels/%s/%s/%s>))", message, m.GuildID, m.ChannelID, m.ID)
}
2024-06-26 23:07:58 +02:00
return message
}
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