Add option to toggle server name

This commit is contained in:
Joren 2024-06-26 23:07:58 +02:00
parent 74198b8930
commit c0076759a4
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55

44
main.go
View File

@ -5,13 +5,11 @@ import (
"os" "os"
"regexp" "regexp"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"git.directme.in/Joren/SmsHook/ringbuffer" "git.directme.in/Joren/SmsHook/ringbuffer"
) )
var ( var (
Token string Token string
Servers []ServerConfig Servers []ServerConfig
@ -21,14 +19,15 @@ var (
) )
type ServerConfig struct { type ServerConfig struct {
ServerID string `toml:"SERVER_ID"` ServerID string `toml:"SERVER_ID"`
OutputChannelID string `toml:"OUTPUT_CHANNEL_ID"` OutputChannelID string `toml:"OUTPUT_CHANNEL_ID"`
CoinRegexes map[string]string `toml:"COIN_REGEXES"` CoinRegexes map[string]string `toml:"COIN_REGEXES"`
ChannelBlacklist struct { ChannelBlacklist struct {
Channels []string `toml:"CHANNELS"` Channels []string `toml:"CHANNELS"`
} `toml:"CHANNEL_BLACKLIST"` } `toml:"CHANNEL_BLACKLIST"`
PrefixEnabled bool `toml:"PREFIX_ENABLED"` PrefixEnabled bool `toml:"PREFIX_ENABLED"`
AllowDuplicates bool `toml:"ALLOW_DUPLICATES"` AllowDuplicates bool `toml:"ALLOW_DUPLICATES"`
ServerIDEnabled bool `toml:"SERVER_ID_ENABLED"`
} }
func init() { func init() {
@ -110,19 +109,25 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
return return
} }
checkMessageContent(s, server, m) guild, err := s.Guild(m.GuildID)
checkEmbeds(s, server, m) if err != nil {
fmt.Println("Error getting guild:", err)
return
}
checkMessageContent(s, server, m, guild.Name)
checkEmbeds(s, server, m, guild.Name)
return 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 { for coin, regex := range CoinRegexes {
match := regex.FindStringSubmatch(m.Content) match := regex.FindStringSubmatch(m.Content)
if len(match) > 0 { if len(match) > 0 {
matchedAddress := 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) { if !server.AllowDuplicates && MessageHistory[server.ServerID].ContainsItem(message) {
return 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 { for _, embed := range m.Message.Embeds {
if embed.Type == "rich" && embed.Description != "" { if embed.Type == "rich" && embed.Description != "" {
for coin, regex := range CoinRegexes { for coin, regex := range CoinRegexes {
match := regex.FindStringSubmatch(embed.Description) match := regex.FindStringSubmatch(embed.Description)
if len(match) > 0 { if len(match) > 0 {
matchedAddress := 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) { if !server.AllowDuplicates && MessageHistory[server.ServerID].ContainsItem(message) {
return 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 { 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 { func isChannelBlacklisted(serverID, channelID string) bool {