Add exchange code discord command

This commit is contained in:
Eccentric 2023-12-26 03:39:12 +00:00
parent 7d51c8dd07
commit 169948fff5
5 changed files with 62 additions and 23 deletions

View File

@ -2,6 +2,7 @@ package discord
import ( import (
"strings" "strings"
"time"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/ectrc/snow/aid" "github.com/ectrc/snow/aid"
@ -174,4 +175,34 @@ func meHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
Flags: discordgo.MessageFlagsEphemeral, Flags: discordgo.MessageFlagsEphemeral,
}, },
}) })
}
func codeHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
player := person.FindByDiscord(i.Member.User.ID)
if player == nil {
s.InteractionRespond(i.Interaction, &ErrorNoAccount)
return
}
code := player.ID + "=" + time.Now().Format("2006-01-02T15:04:05.999Z")
encrypted, sig := aid.KeyPair.EncryptAndSignB64([]byte(code))
decrypt, err := aid.KeyPair.DecryptAndVerifyB64(encrypted, sig)
if err || string(decrypt) != code {
aid.Print("Failed to verify code that was just generated so we're not going to send it")
return
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
NewEmbedBuilder().
SetColor(0x2b2d31).
SetDescription("`" + encrypted + "`").
Build(),
},
Flags: discordgo.MessageFlagsEphemeral,
},
})
} }

View File

@ -49,7 +49,7 @@ func IntialiseClient() {
addCommands() addCommands()
if len(StaticClient.Commands) != len(StaticClient.GetRegisteredCommands()) { if len(StaticClient.Commands) != len(StaticClient.GetRegisteredCommands()) {
StaticClient.UnregisterCommands() // StaticClient.UnregisterCommands()
StaticClient.RegisterCommands() StaticClient.RegisterCommands()
} }

View File

@ -31,6 +31,30 @@ func addCommands() {
Handler: createModalHandler, Handler: createModalHandler,
}) })
addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{
Name: "me",
Description: "Lookup your own information.",
},
Handler: meHandler,
})
addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{
Name: "delete",
Description: "Delete your account with the bot.",
},
Handler: deleteHandler,
})
addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{
Name: "code",
Description: "Generate a one-time use code to link your account.",
},
Handler: codeHandler,
})
addCommand(&DiscordCommand{ addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{ Command: &discordgo.ApplicationCommand{
Name: "information", Name: "information",
@ -63,22 +87,6 @@ func addCommands() {
AdminOnly: true, AdminOnly: true,
}) })
addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{
Name: "me",
Description: "Lookup your own information.",
},
Handler: meHandler,
})
addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{
Name: "delete",
Description: "Delete your account with the bot.",
},
Handler: deleteHandler,
})
addCommand(&DiscordCommand{ addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{ Command: &discordgo.ApplicationCommand{
Name: "ban", Name: "ban",

View File

@ -171,7 +171,7 @@ func PostTokenPassword(c *fiber.Ctx, body *FortniteTokenBody) error {
}) })
} }
func GetOAuthVerify(c *fiber.Ctx) error { func GetTokenVerify(c *fiber.Ctx) error {
auth := c.Get("Authorization") auth := c.Get("Authorization")
if auth == "" { if auth == "" {
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Authorization Header is empty")) return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Authorization Header is empty"))
@ -216,6 +216,10 @@ func GetOAuthVerify(c *fiber.Ctx) error {
}) })
} }
func DeleteToken(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(aid.JSON{})
}
func MiddlewareFortnite(c *fiber.Ctx) error { func MiddlewareFortnite(c *fiber.Ctx) error {
auth := c.Get("Authorization") auth := c.Get("Authorization")
if auth == "" { if auth == "" {
@ -279,10 +283,6 @@ func MiddlewareWeb(c *fiber.Ctx) error {
return c.Next() return c.Next()
} }
func DeleteToken(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(aid.JSON{})
}
func GetPublicAccount(c *fiber.Ctx) error { func GetPublicAccount(c *fiber.Ctx) error {
person := p.Find(c.Params("accountId")) person := p.Find(c.Params("accountId"))
if person == nil { if person == nil {

View File

@ -70,7 +70,7 @@ func main() {
account.Get("/public/account/:accountId", handlers.GetPublicAccount) account.Get("/public/account/:accountId", handlers.GetPublicAccount)
account.Get("/public/account/:accountId/externalAuths", handlers.GetPublicAccountExternalAuths) account.Get("/public/account/:accountId/externalAuths", handlers.GetPublicAccountExternalAuths)
account.Get("/public/account/displayName/:displayName", handlers.GetPublicAccountByDisplayName) account.Get("/public/account/displayName/:displayName", handlers.GetPublicAccountByDisplayName)
account.Get("/oauth/verify", handlers.GetOAuthVerify) account.Get("/oauth/verify", handlers.GetTokenVerify)
account.Post("/oauth/token", handlers.PostFortniteToken) account.Post("/oauth/token", handlers.PostFortniteToken)
account.Delete("/oauth/sessions/kill", handlers.DeleteToken) account.Delete("/oauth/sessions/kill", handlers.DeleteToken)