From c0076759a41b8b230451c5e75c7754249df823f8 Mon Sep 17 00:00:00 2001 From: Joren Date: Wed, 26 Jun 2024 23:07:58 +0200 Subject: [PATCH 1/4] Add option to toggle server name --- main.go | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index b96be03..68d6985 100644 --- a/main.go +++ b/main.go @@ -5,13 +5,11 @@ import ( "os" "regexp" - "github.com/BurntSushi/toml" "github.com/bwmarrin/discordgo" "git.directme.in/Joren/SmsHook/ringbuffer" ) - var ( Token string Servers []ServerConfig @@ -21,14 +19,15 @@ var ( ) type ServerConfig struct { - ServerID string `toml:"SERVER_ID"` - OutputChannelID string `toml:"OUTPUT_CHANNEL_ID"` - CoinRegexes map[string]string `toml:"COIN_REGEXES"` + 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"` - AllowDuplicates bool `toml:"ALLOW_DUPLICATES"` + PrefixEnabled bool `toml:"PREFIX_ENABLED"` + AllowDuplicates bool `toml:"ALLOW_DUPLICATES"` + ServerIDEnabled bool `toml:"SERVER_ID_ENABLED"` } func init() { @@ -110,19 +109,25 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { return } - checkMessageContent(s, server, m) - checkEmbeds(s, server, m) + 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) return } } } -func checkMessageContent(s *discordgo.Session, server ServerConfig, m *discordgo.MessageCreate) { +func checkMessageContent(s *discordgo.Session, server ServerConfig, m *discordgo.MessageCreate, serverName string) { 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) + message := formatMessage(server, coin, m.Author.Username, matchedAddress, serverName) if !server.AllowDuplicates && MessageHistory[server.ServerID].ContainsItem(message) { return @@ -134,14 +139,14 @@ func checkMessageContent(s *discordgo.Session, server ServerConfig, m *discordgo } } -func checkEmbeds(s *discordgo.Session, server ServerConfig, m *discordgo.MessageCreate) { +func checkEmbeds(s *discordgo.Session, server ServerConfig, m *discordgo.MessageCreate, serverName string) { 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) + message := formatMessage(server, coin, m.Author.Username, matchedAddress, serverName) if !server.AllowDuplicates && MessageHistory[server.ServerID].ContainsItem(message) { return @@ -155,11 +160,18 @@ func checkEmbeds(s *discordgo.Session, server ServerConfig, m *discordgo.Message } } -func formatMessage(server ServerConfig, coin, username, address string) string { +func formatMessage(server ServerConfig, coin, username, address, serverName string) string { + message := address + if server.PrefixEnabled { - return fmt.Sprintf("%s: %s", coin, address) + message = fmt.Sprintf("%s: %s", coin, address) } - return address + + if server.ServerIDEnabled { + message = fmt.Sprintf("%s (Server: %s)", message, serverName) + } + + return message } func isChannelBlacklisted(serverID, channelID string) bool { From 95a05eef256711f0834a8206f4af0e5612c4ff35 Mon Sep 17 00:00:00 2001 From: Joren Date: Thu, 27 Jun 2024 11:52:31 +0200 Subject: [PATCH 2/4] Add ability to send the id of the channel the message is found in --- config.toml.example | 4 ++++ main.go | 13 +++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/config.toml.example b/config.toml.example index abbe7c2..d2c2135 100644 --- a/config.toml.example +++ b/config.toml.example @@ -5,6 +5,8 @@ SERVER_ID = "" OUTPUT_CHANNEL_ID = "" PREFIX_ENABLED = true + SERVER_ID_ENABLED = false + CHANNEL_ID_ENABLED = true [SERVERS.COIN_REGEXES] Bitcoin = "[13][a-km-zA-HJ-NP-Z1-9]{25,34}" Ethereum = "0x[a-fA-F0-9]{40}" @@ -15,6 +17,8 @@ SERVER_ID = "" OUTPUT_CHANNEL_ID = "" PREFIX_ENABLED = false + SERVER_ID_ENABLED = false + CHANNEL_ID_ENABLED = true [SERVERS.COIN_REGEXES] Ethereum = "0x[a-fA-F0-9]{40}" Solana = "[1-9A-HJ-NP-Za-km-z]{32,44}" diff --git a/main.go b/main.go index 68d6985..730104a 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,8 @@ type ServerConfig struct { } `toml:"CHANNEL_BLACKLIST"` PrefixEnabled bool `toml:"PREFIX_ENABLED"` AllowDuplicates bool `toml:"ALLOW_DUPLICATES"` - ServerIDEnabled bool `toml:"SERVER_ID_ENABLED"` + ServerIDEnabled bool `toml:"SERVER_ID_ENABLED"` + ChannelIDEnabled bool `toml:"CHANNEL_ID_ENABLED"` } func init() { @@ -127,7 +128,7 @@ func checkMessageContent(s *discordgo.Session, server ServerConfig, m *discordgo match := regex.FindStringSubmatch(m.Content) if len(match) > 0 { matchedAddress := match[0] - message := formatMessage(server, coin, m.Author.Username, matchedAddress, serverName) + message := formatMessage(server, coin, m.Author.Username, matchedAddress, serverName, m) if !server.AllowDuplicates && MessageHistory[server.ServerID].ContainsItem(message) { return @@ -146,7 +147,7 @@ func checkEmbeds(s *discordgo.Session, server ServerConfig, m *discordgo.Message match := regex.FindStringSubmatch(embed.Description) if len(match) > 0 { matchedAddress := match[0] - message := formatMessage(server, coin, m.Author.Username, matchedAddress, serverName) + message := formatMessage(server, coin, m.Author.Username, matchedAddress, serverName, m) if !server.AllowDuplicates && MessageHistory[server.ServerID].ContainsItem(message) { return @@ -160,7 +161,7 @@ func checkEmbeds(s *discordgo.Session, server ServerConfig, m *discordgo.Message } } -func formatMessage(server ServerConfig, coin, username, address, serverName string) string { +func formatMessage(server ServerConfig, coin, username, address, serverName string, m *discordgo.MessageCreate) string { message := address if server.PrefixEnabled { @@ -171,6 +172,10 @@ func formatMessage(server ServerConfig, coin, username, address, serverName stri message = fmt.Sprintf("%s (Server: %s)", message, serverName) } + if server.ChannelIDEnabled { + message = fmt.Sprintf("%s (From: <#%s>)", message, m.ChannelID) + } + return message } From 3fe974d3c30825e2290b51eb984822bd48c503e0 Mon Sep 17 00:00:00 2001 From: Joren Date: Thu, 27 Jun 2024 12:14:55 +0200 Subject: [PATCH 3/4] Add option to send the link of the message --- config.toml.example | 2 ++ main.go | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/config.toml.example b/config.toml.example index d2c2135..68051a4 100644 --- a/config.toml.example +++ b/config.toml.example @@ -7,6 +7,7 @@ PREFIX_ENABLED = true SERVER_ID_ENABLED = false CHANNEL_ID_ENABLED = true + MESSAGE_LINK_ENABLED = false [SERVERS.COIN_REGEXES] Bitcoin = "[13][a-km-zA-HJ-NP-Z1-9]{25,34}" Ethereum = "0x[a-fA-F0-9]{40}" @@ -19,6 +20,7 @@ PREFIX_ENABLED = false SERVER_ID_ENABLED = false CHANNEL_ID_ENABLED = true + MESSAGE_LINK_ENABLED = false [SERVERS.COIN_REGEXES] Ethereum = "0x[a-fA-F0-9]{40}" Solana = "[1-9A-HJ-NP-Za-km-z]{32,44}" diff --git a/main.go b/main.go index 730104a..77c3e63 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ type ServerConfig struct { AllowDuplicates bool `toml:"ALLOW_DUPLICATES"` ServerIDEnabled bool `toml:"SERVER_ID_ENABLED"` ChannelIDEnabled bool `toml:"CHANNEL_ID_ENABLED"` + MessageLinkEnabled bool `toml:"MESSAGE_LINK_ENABLED"` } func init() { @@ -176,6 +177,10 @@ func formatMessage(server ServerConfig, coin, username, address, serverName stri message = fmt.Sprintf("%s (From: <#%s>)", message, m.ChannelID) } + if server.MessageLinkEnabled { + message = fmt.Sprintf("%s ([Link]())", message, m.GuildID, m.ChannelID, m.ID) + } + return message } From 32987927f8265795174788280e4d9e2adad97534 Mon Sep 17 00:00:00 2001 From: Joren Date: Thu, 27 Jun 2024 12:17:09 +0200 Subject: [PATCH 4/4] readme --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b0fc2e1..6018a10 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,9 @@ SERVER_ID = "1050204101826334999" OUTPUT_CHANNEL_ID = "1250585834726621999" PREFIX_ENABLED = true - ALLOW_DUPLICATES = false + SERVER_ID_ENABLED = false + CHANNEL_ID_ENABLED = true + MESSAGE_LINK_ENABLED = false [SERVERS.COIN_REGEXES] Bitcoin = "[13][a-km-zA-HJ-NP-Z1-9]{25,34}" Ethereum = "0x[a-fA-F0-9]{40}" @@ -19,8 +21,10 @@ [[SERVERS]] SERVER_ID = "1250885747062345999" OUTPUT_CHANNEL_ID = "1250585834726621999" - PREFIX_ENABLED = true - ALLOW_DUPLICATES = false + PREFIX_ENABLED = false + SERVER_ID_ENABLED = false + CHANNEL_ID_ENABLED = true + MESSAGE_LINK_ENABLED = false [SERVERS.COIN_REGEXES] Ethereum = "0x[a-fA-F0-9]{40}" Solana = "[1-9A-HJ-NP-Za-km-z]{32,44}"