SolMonitor/main.go

160 lines
3.7 KiB
Go

package main
import (
"fmt"
"os"
"regexp"
"github.com/BurntSushi/toml"
"github.com/bwmarrin/discordgo"
)
var (
Token string
Servers []ServerConfig
CoinRegexes map[string]*regexp.Regexp
ChannelBlacklist map[string][]string
PrefixEnabled bool
)
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"`
PrefixEnabled bool `toml:"PREFIX_ENABLED"`
}
func init() {
var config struct {
Discord struct {
BotToken string `toml:"BOT_TOKEN"`
} `toml:"DISCORD"`
Servers []ServerConfig `toml:"SERVERS"`
}
if _, err := toml.DecodeFile("config.toml", &config); err != nil {
fmt.Println("Error loading config:", err)
os.Exit(1)
}
Token = config.Discord.BotToken
Servers = config.Servers
CoinRegexes = make(map[string]*regexp.Regexp)
ChannelBlacklist = make(map[string][]string)
for _, server := range Servers {
for coin, regex := range server.CoinRegexes {
fullRegex := fmt.Sprintf(`\b%s\b`, regex)
CoinRegexes[coin] = regexp.MustCompile(fullRegex)
}
ChannelBlacklist[server.ServerID] = server.ChannelBlacklist.Channels
}
if Token == "" {
fmt.Println("No token provided in config.toml.")
os.Exit(1)
}
}
func main() {
dg, err := discordgo.New("Bot " + Token)
if err != nil {
fmt.Println("Error creating Discord session:", err)
return
}
dg.AddHandler(ready)
dg.AddHandler(messageCreate)
dg.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentMessageContent
err = dg.Open()
if err != nil {
fmt.Println("Error opening connection:", err)
return
}
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
}
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
}
if isChannelBlacklisted(server.ServerID, channel.ID) {
return
}
checkMessageContent(s, server, m)
checkEmbeds(s, server, m)
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)
s.ChannelMessageSend(server.OutputChannelID, 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)
s.ChannelMessageSend(server.OutputChannelID, message)
}
}
}
}
}
func formatMessage(server ServerConfig, coin, username, address string) string {
if server.PrefixEnabled {
return fmt.Sprintf("%s: %s", coin, address)
}
return address
}
func isChannelBlacklisted(serverID, channelID string) bool {
blacklistedChannels, ok := ChannelBlacklist[serverID]
if !ok {
return false
}
for _, id := range blacklistedChannels {
if id == channelID {
return true
}
}
return false
}