support long messages, rename to RelayChannel, move commands out of main

This commit is contained in:
wish
2024-01-01 21:22:51 +11:00
parent a602bda47b
commit 0d28637095
5 changed files with 70 additions and 54 deletions

View File

@@ -86,6 +86,7 @@
"BotToken": "", "BotToken": "",
"RealTimeChannel": { "RealTimeChannel": {
"Enabled": false, "Enabled": false,
"MaxMessageLength": 183,
"RealTimeChannelID": "" "RealTimeChannelID": ""
} }
}, },

View File

@@ -171,14 +171,15 @@ type GameplayOptions struct {
// Discord holds the discord integration config. // Discord holds the discord integration config.
type Discord struct { type Discord struct {
Enabled bool Enabled bool
BotToken string BotToken string
RealTimeChannel DiscordRealTime RelayChannel DiscordRelay
} }
type DiscordRealTime struct { type DiscordRelay struct {
Enabled bool Enabled bool
RealtimeChannelID string MaxMessageLength int
RelayChannelID string
} }
// Command is a channelserver chat command // Command is a channelserver chat command

28
main.go
View File

@@ -3,7 +3,6 @@ package main
import ( import (
_config "erupe-ce/config" _config "erupe-ce/config"
"fmt" "fmt"
"github.com/bwmarrin/discordgo"
"net" "net"
"os" "os"
"os/signal" "os/signal"
@@ -93,32 +92,7 @@ func main() {
discordBot = bot discordBot = bot
_, err = discordBot.Session.ApplicationCommandBulkOverwrite(discordBot.Session.State.User.ID, "", []*discordgo.ApplicationCommand{ _, err = discordBot.Session.ApplicationCommandBulkOverwrite(discordBot.Session.State.User.ID, "", discordbot.Commands)
{
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,
},
},
},
})
if err != nil { if err != nil {
preventClose(fmt.Sprintf("Discord: Failed to start, %s", err.Error())) preventClose(fmt.Sprintf("Discord: Failed to start, %s", err.Error()))
} }

View File

@@ -113,8 +113,8 @@ func (s *Server) onInteraction(ds *discordgo.Session, i *discordgo.InteractionCr
// onDiscordMessage handles receiving messages from discord and forwarding them ingame. // onDiscordMessage handles receiving messages from discord and forwarding them ingame.
func (s *Server) onDiscordMessage(ds *discordgo.Session, m *discordgo.MessageCreate) { func (s *Server) onDiscordMessage(ds *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore messages from our bot, or ones that are not in the correct channel. // Ignore messages from bots, or messages that are not in the correct channel.
if m.Author.Bot || m.ChannelID != s.erupeConfig.Discord.RealTimeChannel.RealtimeChannelID { if m.Author.Bot || m.ChannelID != s.erupeConfig.Discord.RelayChannel.RelayChannelID {
return return
} }
@@ -124,11 +124,24 @@ func (s *Server) onDiscordMessage(ds *discordgo.Session, m *discordgo.MessageCre
} }
return r return r
}, m.Author.Username)) }, m.Author.Username))
for i := 0; i < 8-len(m.Author.Username); i++ { for i := 0; i < 8-len(m.Author.Username); i++ {
paddedName += " " 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) var messages []string
s.BroadcastChatMessage(s.discordBot.NormalizeDiscordMessage(message)) 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])
}
} }

View File

@@ -7,12 +7,39 @@ import (
"regexp" "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 { type DiscordBot struct {
Session *discordgo.Session Session *discordgo.Session
config *_config.Config config *_config.Config
logger *zap.Logger logger *zap.Logger
MainGuild *discordgo.Guild MainGuild *discordgo.Guild
RealtimeChannel *discordgo.Channel RelayChannel *discordgo.Channel
} }
type Options struct { type Options struct {
@@ -28,22 +55,22 @@ func NewDiscordBot(options Options) (discordBot *DiscordBot, err error) {
return nil, err return nil, err
} }
var realtimeChannel *discordgo.Channel var relayChannel *discordgo.Channel
if options.Config.Discord.RealTimeChannel.Enabled { if options.Config.Discord.RelayChannel.Enabled {
realtimeChannel, err = session.Channel(options.Config.Discord.RealTimeChannel.RealtimeChannelID) relayChannel, err = session.Channel(options.Config.Discord.RelayChannel.RelayChannelID)
} }
if err != nil { 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 return nil, err
} }
discordBot = &DiscordBot{ discordBot = &DiscordBot{
config: options.Config, config: options.Config,
logger: options.Logger, logger: options.Logger,
Session: session, Session: session,
RealtimeChannel: realtimeChannel, RelayChannel: relayChannel,
} }
return return
@@ -55,7 +82,7 @@ func (bot *DiscordBot) Start() (err error) {
return 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 { func (bot *DiscordBot) NormalizeDiscordMessage(message string) string {
userRegex := regexp.MustCompile(`<@!?(\d{17,19})>`) userRegex := regexp.MustCompile(`<@!?(\d{17,19})>`)
emojiRegex := regexp.MustCompile(`(?:<a?)?:(\w+):(?:\d{18}>)?`) emojiRegex := regexp.MustCompile(`(?:<a?)?:(\w+):(?:\d{18}>)?`)
@@ -78,11 +105,11 @@ func (bot *DiscordBot) NormalizeDiscordMessage(message string) string {
} }
func (bot *DiscordBot) RealtimeChannelSend(message string) (err error) { func (bot *DiscordBot) RealtimeChannelSend(message string) (err error) {
if bot.RealtimeChannel == nil { if bot.RelayChannel == nil {
return return
} }
_, err = bot.Session.ChannelMessageSend(bot.RealtimeChannel.ID, message) _, err = bot.Session.ChannelMessageSend(bot.RelayChannel.ID, message)
return return
} }