Add option for multiple regexes to be matched
This commit is contained in:
parent
1b9cbd88c4
commit
06b66dd76f
@ -1,2 +1,24 @@
|
||||
DISCORD_BOT_TOKEN = "your_bot_token"
|
||||
DISCORD_SERVER_ID = "your_server_id"
|
||||
[DISCORD]
|
||||
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 = []
|
||||
|
||||
|
||||
|
133
main.go
133
main.go
@ -1,25 +1,38 @@
|
||||
package main
|
||||
package main
|
||||
|
||||
import (
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
)
|
||||
|
||||
var (
|
||||
var (
|
||||
Token string
|
||||
ServerID string
|
||||
OutputChannelID string
|
||||
)
|
||||
Servers []ServerConfig
|
||||
CoinRegexes map[string]*regexp.Regexp
|
||||
ChannelBlacklist map[string][]string
|
||||
PrefixEnabled bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
var config struct {
|
||||
Token string `toml:"DISCORD_BOT_TOKEN"`
|
||||
ServerID string `toml:"DISCORD_SERVER_ID"`
|
||||
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 {
|
||||
@ -27,27 +40,27 @@
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
Token = config.Token
|
||||
ServerID = config.ServerID
|
||||
OutputChannelID = config.OutputChannelID
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
func main() {
|
||||
dg, err := discordgo.New("Bot " + Token)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating Discord session:", err)
|
||||
@ -67,36 +80,80 @@
|
||||
|
||||
fmt.Println("Bot is now running. Press CTRL+C to exit.")
|
||||
select {}
|
||||
}
|
||||
}
|
||||
|
||||
func ready(s *discordgo.Session, event *discordgo.Ready) {
|
||||
func ready(s *discordgo.Session, event *discordgo.Ready) {
|
||||
s.UpdateGameStatus(0, "Monitoring messages")
|
||||
}
|
||||
}
|
||||
|
||||
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
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 channel.GuildID != ServerID {
|
||||
if isChannelBlacklisted(server.ServerID, channel.ID) {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Message from %s: %s\n", m.Author.Username, m.Content)
|
||||
checkMessageContent(s, server, m)
|
||||
checkEmbeds(s, server, m)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
re := regexp.MustCompile(`\b\w{36,44}\b`)
|
||||
match := re.FindStringSubmatch(m.Content)
|
||||
func checkMessageContent(s *discordgo.Session, server ServerConfig, m *discordgo.MessageCreate) {
|
||||
for coin, regex := range CoinRegexes {
|
||||
match := regex.FindStringSubmatch(m.Content)
|
||||
if len(match) > 0 {
|
||||
matchedWord := match[0]
|
||||
message := fmt.Sprintf("%s", matchedWord)
|
||||
s.ChannelMessageSend(OutputChannelID, message)
|
||||
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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user