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]
|
||||||
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 = []
|
||||||
|
|
||||||
|
|
||||||
|
133
main.go
133
main.go
@ -1,25 +1,38 @@
|
|||||||
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"`
|
|
||||||
ServerID string `toml:"DISCORD_SERVER_ID"`
|
|
||||||
OutputChannelID string `toml:"OUTPUT_CHANNEL_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 {
|
if _, err := toml.DecodeFile("config.toml", &config); err != nil {
|
||||||
@ -27,27 +40,27 @@
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
Token = config.Token
|
Token = config.Discord.BotToken
|
||||||
ServerID = config.ServerID
|
Servers = config.Servers
|
||||||
OutputChannelID = config.OutputChannelID
|
|
||||||
|
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 == "" {
|
if Token == "" {
|
||||||
fmt.Println("No token provided in config.toml.")
|
fmt.Println("No token provided in config.toml.")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ServerID == "" {
|
func main() {
|
||||||
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() {
|
|
||||||
dg, err := discordgo.New("Bot " + Token)
|
dg, err := discordgo.New("Bot " + Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error creating Discord session:", err)
|
fmt.Println("Error creating Discord session:", err)
|
||||||
@ -67,36 +80,80 @@
|
|||||||
|
|
||||||
fmt.Println("Bot is now running. Press CTRL+C to exit.")
|
fmt.Println("Bot is now running. Press CTRL+C to exit.")
|
||||||
select {}
|
select {}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ready(s *discordgo.Session, event *discordgo.Ready) {
|
func ready(s *discordgo.Session, event *discordgo.Ready) {
|
||||||
s.UpdateGameStatus(0, "Monitoring messages")
|
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 {
|
if m.Author.ID == s.State.User.ID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, server := range Servers {
|
||||||
|
if m.GuildID == server.ServerID {
|
||||||
channel, err := s.State.Channel(m.ChannelID)
|
channel, err := s.State.Channel(m.ChannelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error getting channel:", err)
|
fmt.Println("Error getting channel:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if channel.GuildID != ServerID {
|
if isChannelBlacklisted(server.ServerID, channel.ID) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Message from %s: %s\n", m.Author.Username, m.Content)
|
checkMessageContent(s, server, m)
|
||||||
|
checkEmbeds(s, server, m)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkMessageContent(s *discordgo.Session, server ServerConfig, m *discordgo.MessageCreate) {
|
||||||
re := regexp.MustCompile(`\b\w{36,44}\b`)
|
for coin, regex := range CoinRegexes {
|
||||||
match := re.FindStringSubmatch(m.Content)
|
match := regex.FindStringSubmatch(m.Content)
|
||||||
if len(match) > 0 {
|
if len(match) > 0 {
|
||||||
matchedWord := match[0]
|
matchedAddress := match[0]
|
||||||
message := fmt.Sprintf("%s", matchedWord)
|
message := formatMessage(server, coin, m.Author.Username, matchedAddress)
|
||||||
s.ChannelMessageSend(OutputChannelID, message)
|
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