From 33665130cf1b04d1fab560b3efc08c841f5d3a4b Mon Sep 17 00:00:00 2001 From: Matthew Date: Sun, 26 Nov 2023 01:22:51 -0500 Subject: [PATCH] feat: Discord basic implementation --- patch-schema/15-discord-password-resets.sql | 6 ++++++ server/channelserver/handlers_cast_binary.go | 10 ++++++++++ server/channelserver/handlers_discord.go | 21 ++++++++++++++++++++ server/channelserver/sys_channel_server.go | 11 ++++++++++ 4 files changed, 48 insertions(+) create mode 100644 patch-schema/15-discord-password-resets.sql diff --git a/patch-schema/15-discord-password-resets.sql b/patch-schema/15-discord-password-resets.sql new file mode 100644 index 000000000..bd2e83fea --- /dev/null +++ b/patch-schema/15-discord-password-resets.sql @@ -0,0 +1,6 @@ +BEGIN; + +ALTER TABLE IF EXISTS public.users ADD COLUMN discord_token text; +ALTER TABLE IF EXISTS public.users ADD COLUMN discord_id text; + +END; \ No newline at end of file diff --git a/server/channelserver/handlers_cast_binary.go b/server/channelserver/handlers_cast_binary.go index dbfafc68c..36edbbadb 100644 --- a/server/channelserver/handlers_cast_binary.go +++ b/server/channelserver/handlers_cast_binary.go @@ -1,6 +1,7 @@ package channelserver import ( + "crypto" "encoding/hex" "erupe-ce/common/byteframe" "erupe-ce/common/mhfcourse" @@ -318,6 +319,15 @@ func parseChatCommand(s *Session, command string) { } else { sendDisabledCommandMessage(s, commands["Teleport"]) } + case commands["Discord"].Prefix: + if commands["Discord"].Enabled { + token := crypto.MD5.New() + _, err := s.server.db.Exec("UPDATE users SET discord_token = ?", token) + if err != nil { + return + } + sendServerChatMessage(s, fmt.Sprintf(s.server.dict["commandDiscord"], token)) + } } } diff --git a/server/channelserver/handlers_discord.go b/server/channelserver/handlers_discord.go index 1f3398d18..551c74146 100644 --- a/server/channelserver/handlers_discord.go +++ b/server/channelserver/handlers_discord.go @@ -66,6 +66,27 @@ func getCharacterList(s *Server) string { return message } +// onInteraction handles slash commands +func (s *Server) onInteraction(ds *discordgo.Session, i *discordgo.InteractionCreate) { + switch i.Interaction.ApplicationCommandData().Name { + case "verify": + _, err := s.db.Exec("UPDATE users SET discord_id = ? WHERE discord_token = ?", i.User.ID, i.Interaction.ApplicationCommandData().Options[0].StringValue()) + if err != nil { + return + } + err = ds.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Account successfully linked", + }, + }) + if err != nil { + return + } + break + } +} + // 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. diff --git a/server/channelserver/sys_channel_server.go b/server/channelserver/sys_channel_server.go index 70f52e461..f7658c5cd 100644 --- a/server/channelserver/sys_channel_server.go +++ b/server/channelserver/sys_channel_server.go @@ -2,6 +2,7 @@ package channelserver import ( "fmt" + "github.com/bwmarrin/discordgo" "net" "strings" "sync" @@ -210,7 +211,17 @@ func (s *Server) Start() error { // Start the discord bot for chat integration. if s.erupeConfig.Discord.Enabled && s.discordBot != nil { + _, err := s.discordBot.Session.ApplicationCommandBulkOverwrite(s.discordBot.Session.State.User.ID, "", []*discordgo.ApplicationCommand{ + { + Name: "verify", + Description: "Verify your account with Discord", + }, + }) + if err != nil { + return err + } s.discordBot.Session.AddHandler(s.onDiscordMessage) + s.discordBot.Session.AddHandler(s.onInteraction) } return nil