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": "",
"RealTimeChannel": {
"Enabled": false,
"MaxMessageLength": 183,
"RealTimeChannelID": ""
}
},

View File

@@ -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

28
main.go
View File

@@ -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()))
}

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.
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])
}
}

View File

@@ -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(`(?:<a?)?:(\w+):(?:\d{18}>)?`)
@@ -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
}