feat: Discord basic implementation

This commit is contained in:
Matthew
2023-11-26 01:22:51 -05:00
parent 7e5fd73496
commit 33665130cf
4 changed files with 48 additions and 0 deletions

View File

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

View File

@@ -1,6 +1,7 @@
package channelserver package channelserver
import ( import (
"crypto"
"encoding/hex" "encoding/hex"
"erupe-ce/common/byteframe" "erupe-ce/common/byteframe"
"erupe-ce/common/mhfcourse" "erupe-ce/common/mhfcourse"
@@ -318,6 +319,15 @@ func parseChatCommand(s *Session, command string) {
} else { } else {
sendDisabledCommandMessage(s, commands["Teleport"]) 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))
}
} }
} }

View File

@@ -66,6 +66,27 @@ func getCharacterList(s *Server) string {
return message 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. // 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 our bot, or ones that are not in the correct channel.

View File

@@ -2,6 +2,7 @@ package channelserver
import ( import (
"fmt" "fmt"
"github.com/bwmarrin/discordgo"
"net" "net"
"strings" "strings"
"sync" "sync"
@@ -210,7 +211,17 @@ func (s *Server) Start() error {
// Start the discord bot for chat integration. // Start the discord bot for chat integration.
if s.erupeConfig.Discord.Enabled && s.discordBot != nil { 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.onDiscordMessage)
s.discordBot.Session.AddHandler(s.onInteraction)
} }
return nil return nil