2024-06-18 17:26:44 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
2024-06-18 17:52:38 +02:00
|
|
|
"git.directme.in/Joren/SmsHook/ringbuffer"
|
2024-06-18 17:26:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2024-06-18 17:26:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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"`
|
2024-06-18 17:26:44 +02:00
|
|
|
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"`
|
2024-06-18 17:26:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-06-18 17:26:44 +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
|
|
|
|
2024-06-18 17:26:44 +02:00
|
|
|
Token = config.Discord.BotToken
|
|
|
|
Servers = config.Servers
|
2024-06-18 17:06:13 +02:00
|
|
|
|
2024-06-18 17:26:44 +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
|
|
|
|
2024-06-18 17:26:44 +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
|
|
|
}
|
2024-06-18 17:26:44 +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
|
|
|
}
|
|
|
|
|
2024-06-18 17:26:44 +02:00
|
|
|
if Token == "" {
|
|
|
|
fmt.Println("No token provided in config.toml.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2024-06-18 16:29:58 +02:00
|
|
|
|
2024-06-18 17:26:44 +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
|
|
|
|
2024-06-18 17:26:44 +02:00
|
|
|
dg.AddHandler(ready)
|
|
|
|
dg.AddHandler(messageCreate)
|
2024-06-18 16:29:58 +02:00
|
|
|
|
2024-06-18 17:26:44 +02:00
|
|
|
dg.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentMessageContent
|
2024-06-18 16:26:10 +02:00
|
|
|
|
2024-06-18 17:26:44 +02:00
|
|
|
err = dg.Open()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error opening connection:", err)
|
|
|
|
return
|
2024-06-18 16:26:10 +02:00
|
|
|
}
|
|
|
|
|
2024-06-18 17:26:44 +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
|
|
|
}
|
|
|
|
|
2024-06-18 17:26:44 +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
|
|
|
|
2024-06-18 17:26:44 +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-18 17:26:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-26 23:07:58 +02:00
|
|
|
func checkMessageContent(s *discordgo.Session, server ServerConfig, m *discordgo.MessageCreate, serverName string) {
|
2024-06-18 17:26:44 +02:00
|
|
|
for coin, regex := range CoinRegexes {
|
|
|
|
match := regex.FindStringSubmatch(m.Content)
|
|
|
|
if len(match) > 0 {
|
|
|
|
matchedAddress := match[0]
|
2024-06-26 23:07:58 +02:00
|
|
|
message := formatMessage(server, coin, m.Author.Username, matchedAddress, serverName)
|
2024-06-18 17:52:38 +02:00
|
|
|
|
|
|
|
if !server.AllowDuplicates && MessageHistory[server.ServerID].ContainsItem(message) {
|
2024-06-18 17:53:19 +02:00
|
|
|
return
|
2024-06-18 17:52:38 +02:00
|
|
|
}
|
|
|
|
|
2024-06-18 17:26:44 +02:00
|
|
|
s.ChannelMessageSend(server.OutputChannelID, message)
|
2024-06-18 17:52:38 +02:00
|
|
|
MessageHistory[server.ServerID].Add(message)
|
2024-06-18 17:26:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-26 23:07:58 +02:00
|
|
|
func checkEmbeds(s *discordgo.Session, server ServerConfig, m *discordgo.MessageCreate, serverName string) {
|
2024-06-18 17:26:44 +02:00
|
|
|
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-26 23:07:58 +02:00
|
|
|
message := formatMessage(server, coin, m.Author.Username, matchedAddress, serverName)
|
2024-06-18 17:52:38 +02:00
|
|
|
|
|
|
|
if !server.AllowDuplicates && MessageHistory[server.ServerID].ContainsItem(message) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-18 17:26:44 +02:00
|
|
|
s.ChannelMessageSend(server.OutputChannelID, message)
|
2024-06-18 17:52:38 +02:00
|
|
|
MessageHistory[server.ServerID].Add(message)
|
2024-06-18 17:26:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-18 16:29:58 +02:00
|
|
|
|
2024-06-26 23:07:58 +02:00
|
|
|
func formatMessage(server ServerConfig, coin, username, address, serverName string) string {
|
|
|
|
message := address
|
|
|
|
|
2024-06-18 17:26:44 +02:00
|
|
|
if server.PrefixEnabled {
|
2024-06-26 23:07:58 +02:00
|
|
|
message = fmt.Sprintf("%s: %s", coin, address)
|
2024-06-18 17:26:44 +02:00
|
|
|
}
|
2024-06-26 23:07:58 +02:00
|
|
|
|
|
|
|
if server.ServerIDEnabled {
|
|
|
|
message = fmt.Sprintf("%s (Server: %s)", message, serverName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return message
|
2024-06-18 17:26:44 +02:00
|
|
|
}
|
2024-06-18 16:29:58 +02:00
|
|
|
|
2024-06-18 17:26:44 +02:00
|
|
|
func isChannelBlacklisted(serverID, channelID string) bool {
|
|
|
|
blacklistedChannels, ok := ChannelBlacklist[serverID]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2024-06-18 16:29:58 +02:00
|
|
|
|
2024-06-18 17:26:44 +02:00
|
|
|
for _, id := range blacklistedChannels {
|
|
|
|
if id == channelID {
|
|
|
|
return true
|
|
|
|
}
|
2024-06-18 17:06:13 +02:00
|
|
|
}
|
2024-06-18 17:26:44 +02:00
|
|
|
return false
|
|
|
|
}
|
2024-06-18 16:26:10 +02:00
|
|
|
|