Add option for multiple regexes to be matched

This commit is contained in:
Joren 2024-06-18 17:26:44 +02:00
parent 1b9cbd88c4
commit 06b66dd76f
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55
2 changed files with 164 additions and 85 deletions

View File

@ -1,2 +1,24 @@
DISCORD_BOT_TOKEN = "your_bot_token" [DISCORD]
DISCORD_SERVER_ID = "your_server_id" BOT_TOKEN = ""
[[SERVERS]]
SERVER_ID = ""
OUTPUT_CHANNEL_ID = ""
PREFIX_ENABLED = true
[SERVERS.COIN_REGEXES]
Bitcoin = "[13][a-km-zA-HJ-NP-Z1-9]{25,34}"
Ethereum = "0x[a-fA-F0-9]{40}"
[SERVERS.CHANNEL_BLACKLIST]
CHANNELS = [""]
[[SERVERS]]
SERVER_ID = ""
OUTPUT_CHANNEL_ID = ""
PREFIX_ENABLED = false
[SERVERS.COIN_REGEXES]
Ethereum = "0x[a-fA-F0-9]{40}"
Solana = "[1-9A-HJ-NP-Za-km-z]{32,44}"
[SERVERS.CHANNEL_BLACKLIST]
CHANNELS = []

223
main.go
View File

@ -1,102 +1,159 @@
package main package main
import ( import (
"fmt" "fmt"
"os" "os"
"regexp" "regexp"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
var ( var (
Token string Token string
ServerID string Servers []ServerConfig
OutputChannelID string CoinRegexes map[string]*regexp.Regexp
) ChannelBlacklist map[string][]string
PrefixEnabled bool
)
func init() { type ServerConfig struct {
var config struct { ServerID string `toml:"SERVER_ID"`
Token string `toml:"DISCORD_BOT_TOKEN"` OutputChannelID string `toml:"OUTPUT_CHANNEL_ID"`
ServerID string `toml:"DISCORD_SERVER_ID"` CoinRegexes map[string]string `toml:"COIN_REGEXES"`
OutputChannelID string `toml:"OUTPUT_CHANNEL_ID"` ChannelBlacklist struct {
} Channels []string `toml:"CHANNELS"`
} `toml:"CHANNEL_BLACKLIST"`
PrefixEnabled bool `toml:"PREFIX_ENABLED"`
}
if _, err := toml.DecodeFile("config.toml", &config); err != nil { func init() {
fmt.Println("Error loading config:", err) var config struct {
os.Exit(1) Discord struct {
} BotToken string `toml:"BOT_TOKEN"`
} `toml:"DISCORD"`
Token = config.Token Servers []ServerConfig `toml:"SERVERS"`
ServerID = config.ServerID
OutputChannelID = config.OutputChannelID
if Token == "" {
fmt.Println("No token provided in config.toml.")
os.Exit(1)
}
if ServerID == "" {
fmt.Println("No server ID provided in config.toml.")
os.Exit(1)
}
if OutputChannelID == "" {
fmt.Println("No output channel ID in config.toml")
os.Exit(1)
}
} }
func main() { if _, err := toml.DecodeFile("config.toml", &config); err != nil {
dg, err := discordgo.New("Bot " + Token) fmt.Println("Error loading config:", err)
if err != nil { os.Exit(1)
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) { Token = config.Discord.BotToken
s.UpdateGameStatus(0, "Monitoring messages") 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
} }
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if Token == "" {
if m.Author.ID == s.State.User.ID { fmt.Println("No token provided in config.toml.")
return os.Exit(1)
} }
}
channel, err := s.State.Channel(m.ChannelID) func main() {
if err != nil { dg, err := discordgo.New("Bot " + Token)
fmt.Println("Error getting channel:", err) if err != nil {
return fmt.Println("Error creating Discord session:", err)
} return
}
if channel.GuildID != ServerID { dg.AddHandler(ready)
return dg.AddHandler(messageCreate)
}
fmt.Printf("Message from %s: %s\n", m.Author.Username, m.Content) dg.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentMessageContent
err = dg.Open()
if err != nil {
fmt.Println("Error opening connection:", err)
return
}
re := regexp.MustCompile(`\b\w{36,44}\b`) fmt.Println("Bot is now running. Press CTRL+C to exit.")
match := re.FindStringSubmatch(m.Content) select {}
if len(match) > 0 { }
matchedWord := match[0]
message := fmt.Sprintf("%s", matchedWord) func ready(s *discordgo.Session, event *discordgo.Ready) {
s.ChannelMessageSend(OutputChannelID, message) 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
}