From 0d2863709591a7487e3e23f2b42d2218e5cb15b9 Mon Sep 17 00:00:00 2001 From: wish Date: Mon, 1 Jan 2024 21:22:51 +1100 Subject: [PATCH] support long messages, rename to RelayChannel, move commands out of main --- config.json | 1 + config/config.go | 13 +++--- main.go | 28 +---------- server/channelserver/handlers_discord.go | 23 +++++++-- server/discordbot/discord_bot.go | 59 +++++++++++++++++------- 5 files changed, 70 insertions(+), 54 deletions(-) diff --git a/config.json b/config.json index 20da75a87..15f51d9df 100644 --- a/config.json +++ b/config.json @@ -86,6 +86,7 @@ "BotToken": "", "RealTimeChannel": { "Enabled": false, + "MaxMessageLength": 183, "RealTimeChannelID": "" } }, diff --git a/config/config.go b/config/config.go index 8b1c0764d..3c6f6a0a7 100644 --- a/config/config.go +++ b/config/config.go @@ -171,14 +171,15 @@ type GameplayOptions struct { // Discord holds the discord integration config. type Discord struct { - Enabled bool - BotToken string - RealTimeChannel DiscordRealTime + Enabled bool + BotToken string + RelayChannel DiscordRelay } -type DiscordRealTime struct { - Enabled bool - RealtimeChannelID string +type DiscordRelay struct { + Enabled bool + MaxMessageLength int + RelayChannelID string } // Command is a channelserver chat command diff --git a/main.go b/main.go index 94f3b6e24..a7d368930 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( _config "erupe-ce/config" "fmt" - "github.com/bwmarrin/discordgo" "net" "os" "os/signal" @@ -93,32 +92,7 @@ func main() { discordBot = bot - _, err = discordBot.Session.ApplicationCommandBulkOverwrite(discordBot.Session.State.User.ID, "", []*discordgo.ApplicationCommand{ - { - Name: "verify", - Description: "Verify your account with Discord", - Options: []*discordgo.ApplicationCommandOption{ - { - Type: discordgo.ApplicationCommandOptionString, - Name: "token", - Description: "The access token provided by !discord command within the game client.", - Required: true, - }, - }, - }, - { - Name: "password", - Description: "Reset your account password on Erupe", - Options: []*discordgo.ApplicationCommandOption{ - { - Type: discordgo.ApplicationCommandOptionString, - Name: "password", - Description: "The password to change your account to.", - Required: true, - }, - }, - }, - }) + _, err = discordBot.Session.ApplicationCommandBulkOverwrite(discordBot.Session.State.User.ID, "", discordbot.Commands) if err != nil { preventClose(fmt.Sprintf("Discord: Failed to start, %s", err.Error())) } diff --git a/server/channelserver/handlers_discord.go b/server/channelserver/handlers_discord.go index 1772e2d06..8e3ed3cb9 100644 --- a/server/channelserver/handlers_discord.go +++ b/server/channelserver/handlers_discord.go @@ -113,8 +113,8 @@ func (s *Server) onInteraction(ds *discordgo.Session, i *discordgo.InteractionCr // onDiscordMessage handles receiving messages from discord and forwarding them ingame. func (s *Server) onDiscordMessage(ds *discordgo.Session, m *discordgo.MessageCreate) { - // Ignore messages from our bot, or ones that are not in the correct channel. - if m.Author.Bot || m.ChannelID != s.erupeConfig.Discord.RealTimeChannel.RealtimeChannelID { + // Ignore messages from bots, or messages that are not in the correct channel. + if m.Author.Bot || m.ChannelID != s.erupeConfig.Discord.RelayChannel.RelayChannelID { return } @@ -124,11 +124,24 @@ func (s *Server) onDiscordMessage(ds *discordgo.Session, m *discordgo.MessageCre } return r }, m.Author.Username)) - for i := 0; i < 8-len(m.Author.Username); i++ { paddedName += " " } + message := s.discordBot.NormalizeDiscordMessage(fmt.Sprintf("[D] %s > %s", paddedName, m.Content)) + if len(message) > s.erupeConfig.Discord.RelayChannel.MaxMessageLength { + return + } - message := fmt.Sprintf("[D] %s > %s", paddedName, m.Content) - s.BroadcastChatMessage(s.discordBot.NormalizeDiscordMessage(message)) + var messages []string + lineLength := 61 + for i := 0; i < len(message); i += lineLength { + end := i + lineLength + if end > len(message) { + end = len(message) + } + messages = append(messages, message[i:end]) + } + for i := range messages { + s.BroadcastChatMessage(messages[i]) + } } diff --git a/server/discordbot/discord_bot.go b/server/discordbot/discord_bot.go index 0d774fff7..a9b327cc3 100644 --- a/server/discordbot/discord_bot.go +++ b/server/discordbot/discord_bot.go @@ -7,12 +7,39 @@ import ( "regexp" ) +var Commands = []*discordgo.ApplicationCommand{ + { + Name: "link", + Description: "Link your Erupe account to Discord", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "token", + Description: "The token provided by the Discord command in-game", + Required: true, + }, + }, + }, + { + Name: "password", + Description: "Change your Erupe account password", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "password", + Description: "Your new password", + Required: true, + }, + }, + }, +} + type DiscordBot struct { - Session *discordgo.Session - config *_config.Config - logger *zap.Logger - MainGuild *discordgo.Guild - RealtimeChannel *discordgo.Channel + Session *discordgo.Session + config *_config.Config + logger *zap.Logger + MainGuild *discordgo.Guild + RelayChannel *discordgo.Channel } type Options struct { @@ -28,22 +55,22 @@ func NewDiscordBot(options Options) (discordBot *DiscordBot, err error) { return nil, err } - var realtimeChannel *discordgo.Channel + var relayChannel *discordgo.Channel - if options.Config.Discord.RealTimeChannel.Enabled { - realtimeChannel, err = session.Channel(options.Config.Discord.RealTimeChannel.RealtimeChannelID) + if options.Config.Discord.RelayChannel.Enabled { + relayChannel, err = session.Channel(options.Config.Discord.RelayChannel.RelayChannelID) } if err != nil { - options.Logger.Fatal("Discord failed to create realtimeChannel", zap.Error(err)) + options.Logger.Fatal("Discord failed to create relayChannel", zap.Error(err)) return nil, err } discordBot = &DiscordBot{ - config: options.Config, - logger: options.Logger, - Session: session, - RealtimeChannel: realtimeChannel, + config: options.Config, + logger: options.Logger, + Session: session, + RelayChannel: relayChannel, } return @@ -55,7 +82,7 @@ func (bot *DiscordBot) Start() (err error) { return } -// Replace all mentions to real name from the message. +// NormalizeDiscordMessage replaces all mentions to real name from the message. func (bot *DiscordBot) NormalizeDiscordMessage(message string) string { userRegex := regexp.MustCompile(`<@!?(\d{17,19})>`) emojiRegex := regexp.MustCompile(`(?:)?`) @@ -78,11 +105,11 @@ func (bot *DiscordBot) NormalizeDiscordMessage(message string) string { } func (bot *DiscordBot) RealtimeChannelSend(message string) (err error) { - if bot.RealtimeChannel == nil { + if bot.RelayChannel == nil { return } - _, err = bot.Session.ChannelMessageSend(bot.RealtimeChannel.ID, message) + _, err = bot.Session.ChannelMessageSend(bot.RelayChannel.ID, message) return }