From b90fab0d03d8098cb89d87d3bdf7d002d5fe0702 Mon Sep 17 00:00:00 2001 From: eccentric Date: Sat, 16 Dec 2023 22:08:37 +0000 Subject: [PATCH] move commands + add more discord user info collection --- discord/account.go | 177 +++++++++++++++++ discord/admin.go | 294 ++++++++++++++++++++++++++++ discord/handlers.go | 454 -------------------------------------------- handlers/auth.go | 7 +- handlers/discord.go | 7 +- main.go | 2 +- storage/tables.go | 2 + 7 files changed, 484 insertions(+), 459 deletions(-) create mode 100644 discord/account.go create mode 100644 discord/admin.go diff --git a/discord/account.go b/discord/account.go new file mode 100644 index 0000000..b734cbb --- /dev/null +++ b/discord/account.go @@ -0,0 +1,177 @@ +package discord + +import ( + "strings" + + "github.com/bwmarrin/discordgo" + "github.com/ectrc/snow/aid" + "github.com/ectrc/snow/fortnite" + "github.com/ectrc/snow/person" + "github.com/ectrc/snow/storage" +) + +func createHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { + modal := &discordgo.InteractionResponseData{ + CustomID: "create://" + i.Member.User.ID, + Title: "Create an account", + Components: []discordgo.MessageComponent{ + &discordgo.ActionsRow{ + Components: []discordgo.MessageComponent{ + discordgo.TextInput{ + CustomID: "display", + Label: "DISPLAY NAME", + Style: discordgo.TextInputShort, + Placeholder: "Enter your crazy display name here!", + Required: true, + MaxLength: 20, + MinLength: 2, + }, + }, + }, + }, + } + + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseModal, + Data: modal, + }) +} + +func createModalHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { + data := i.ModalSubmitData() + if len(data.Components) <= 0 { + aid.Print("No components found") + return + } + + components, ok := data.Components[0].(*discordgo.ActionsRow) + if !ok { + aid.Print("Failed to assert TextInput") + return + } + + display, ok := components.Components[0].(*discordgo.TextInput) + if !ok { + aid.Print("Failed to assert TextInput") + return + } + + found := person.FindByDiscord(i.Member.User.ID) + if found != nil { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "You already have an account with the display name: `" + found.DisplayName + "`", + }, + }) + return + } + + found = person.FindByDisplay(display.Value) + if found != nil { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Someone already has an account with the display name: `" + found.DisplayName + "`, please choose another one.", + }, + }) + return + } + + account := fortnite.NewFortnitePerson(display.Value, false) // or aid.Config.Fortnite.Everything + discord := &storage.DB_DiscordPerson{ + ID: i.Member.User.ID, + PersonID: account.ID, + Username: i.Member.User.Username, + Avatar: i.Member.User.Avatar, + } + storage.Repo.SaveDiscordPerson(discord) + account.Discord = discord + account.Save() + + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Your account has been created with the display name: `" + account.DisplayName + "`", + }, + }) +} + +func deleteHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { + found := person.FindByDiscord(i.Member.User.ID) + if found == nil { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "You do not have an account with the bot.", + Flags: discordgo.MessageFlagsEphemeral, + }, + }) + return + } + + storage.Repo.DeleteDiscordPerson(found.Discord.ID) + found.Delete() + + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Your account has been deleted.", + Flags: discordgo.MessageFlagsEphemeral, + }, + }) +} + +func meHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { + player := person.FindByDiscord(i.Member.User.ID) + if player == nil { + s.InteractionRespond(i.Interaction, &ErrorNoAccount) + return + } + + playerVbucks := player.CommonCoreProfile.Items.GetItemByTemplateID("Currency:MtxPurchased") + if playerVbucks == nil { + return + } + + activeCharacter := func() string { + if player.AthenaProfile == nil { + return "None" + } + + characterId := "" + player.AthenaProfile.Loadouts.RangeLoadouts(func(key string, value *person.Loadout) bool { + characterId = value.CharacterID + return false + }) + + if characterId == "" { + return "None" + } + + character := player.AthenaProfile.Items.GetItem(characterId) + if character == nil { + return "None" + } + + return character.TemplateID + }() + + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{ + NewEmbedBuilder(). + SetTitle("Player Lookup"). + SetColor(0x2b2d31). + AddField("Display Name", player.DisplayName, true). + AddField("VBucks", aid.FormatNumber(playerVbucks.Quantity), true). + AddField("Discord Account", "<@"+player.Discord.ID+">", true). + AddField("ID", player.ID, true). + SetThumbnail("https://fortnite-api.com/images/cosmetics/br/"+ strings.Split(activeCharacter, ":")[1] +"/icon.png"). + Build(), + }, + Flags: discordgo.MessageFlagsEphemeral, + }, + }) +} \ No newline at end of file diff --git a/discord/admin.go b/discord/admin.go new file mode 100644 index 0000000..2c512fd --- /dev/null +++ b/discord/admin.go @@ -0,0 +1,294 @@ +package discord + +import ( + "strings" + + "github.com/bwmarrin/discordgo" + "github.com/ectrc/snow/aid" + "github.com/ectrc/snow/person" + "github.com/ectrc/snow/storage" +) + +func informationHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { + looker := person.FindByDiscord(i.Member.User.ID) + if looker == nil { + s.InteractionRespond(i.Interaction, &ErrorNoPermission) + return + } + + if !looker.HasPermission(person.PermissionInformation) { + s.InteractionRespond(i.Interaction, &ErrorNoPermission) + return + } + + playerCount := storage.Repo.GetPersonsCount() + totalVbucks := storage.Repo.TotalVBucks() + + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{ + NewEmbedBuilder(). + SetTitle("Information"). + SetColor(0x2b2d31). + AddField("Players Registered", aid.FormatNumber(playerCount), true). + AddField("Players Online", aid.FormatNumber(0), true). + AddField("VBucks in Circulation", aid.FormatNumber(totalVbucks), false). + Build(), + }, + Flags: discordgo.MessageFlagsEphemeral, + }, + }) +} + +func whoHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { + looker := person.FindByDiscord(i.Member.User.ID) + if looker == nil { + s.InteractionRespond(i.Interaction, &ErrorNoPermission) + return + } + + if !looker.HasPermission(person.PermissionLookup) { + s.InteractionRespond(i.Interaction, &ErrorNoPermission) + return + } + + player := getPersonFromOptions(i.ApplicationCommandData(), s) + if player == nil { + s.InteractionRespond(i.Interaction, &ErrorInvalidDisplayOrDiscord) + return + } + + playerVbucks := player.CommonCoreProfile.Items.GetItemByTemplateID("Currency:MtxPurchased") + if playerVbucks == nil { + return + } + + activeCharacter := func() string { + if player.AthenaProfile == nil { + return "None" + } + + characterId := "" + player.AthenaProfile.Loadouts.RangeLoadouts(func(key string, value *person.Loadout) bool { + characterId = value.CharacterID + return false + }) + + if characterId == "" { + return "None" + } + + character := player.AthenaProfile.Items.GetItem(characterId) + if character == nil { + return "None" + } + + return character.TemplateID + }() + + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{ + NewEmbedBuilder(). + SetTitle("Player Lookup"). + SetColor(0x2b2d31). + AddField("Display Name", player.DisplayName, true). + AddField("VBucks", aid.FormatNumber(playerVbucks.Quantity), true). + AddField("Discord Account", "<@"+player.Discord.ID+">", true). + AddField("ID", player.ID, true). + SetThumbnail("https://fortnite-api.com/images/cosmetics/br/" + strings.Split(activeCharacter, ":")[1] + "/icon.png"). + Build(), + }, + Flags: discordgo.MessageFlagsEphemeral, + }, + }) +} + +func banHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { + looker := person.FindByDiscord(i.Member.User.ID) + if looker == nil { + s.InteractionRespond(i.Interaction, &ErrorNoPermission) + return + } + + if !looker.HasPermission(person.PermissionBan) { + s.InteractionRespond(i.Interaction, &ErrorNoPermission) + return + } + + player := getPersonFromOptions(i.ApplicationCommandData(), s) + if player == nil { + s.InteractionRespond(i.Interaction, &ErrorInvalidDisplayOrDiscord) + return + } + + player.Ban() + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: player.DisplayName + " has been banned.", + }, + }) +} + +func unbanHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { + looker := person.FindByDiscord(i.Member.User.ID) + if looker == nil { + s.InteractionRespond(i.Interaction, &ErrorNoPermission) + return + } + + if !looker.HasPermission(person.PermissionBan) { + s.InteractionRespond(i.Interaction, &ErrorNoPermission) + return + } + + player := getPersonFromOptions(i.ApplicationCommandData(), s) + if player == nil { + s.InteractionRespond(i.Interaction, &ErrorInvalidDisplayOrDiscord) + return + } + + player.Unban() + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: player.DisplayName + " has been unbanned.", + }, + }) +} + +func giveItemHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { + looker := person.FindByDiscord(i.Member.User.ID) + if looker == nil { + s.InteractionRespond(i.Interaction, &ErrorNoAccount) + return + } + + if !looker.HasPermission(person.PermissionGiveItem) { + s.InteractionRespond(i.Interaction, &ErrorNoPermission) + return + } + + player := getPersonFromOptions(i.ApplicationCommandData(), s) + if player == nil { + s.InteractionRespond(i.Interaction, &ErrorInvalidDisplayOrDiscord) + return + } + + item := i.ApplicationCommandData().Options[0].StringValue() + if item == "" { + s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) + return + } + + qty := i.ApplicationCommandData().Options[1].IntValue() + if qty <= 0 { + s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) + return + } + + profile := i.ApplicationCommandData().Options[2].StringValue() + if profile == "" { + s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) + return + } + + if player.GetProfileFromType(profile) == nil { + s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) + return + } + + snapshot := player.GetProfileFromType(profile).Snapshot() + foundItem := player.GetProfileFromType(profile).Items.GetItemByTemplateID(item) + switch (foundItem) { + case nil: + foundItem = person.NewItem(item, int(qty)) + player.GetProfileFromType(profile).Items.AddItem(foundItem) + default: + foundItem.Quantity += int(qty) + } + foundItem.Save() + player.GetProfileFromType(profile).Diff(snapshot) + + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: player.DisplayName + " has been given or updated `" + item + "` in `" + profile + "`.", + }, + }) +} + +func takeItemHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { + looker := person.FindByDiscord(i.Member.User.ID) + if looker == nil { + s.InteractionRespond(i.Interaction, &ErrorNoAccount) + return + } + + if !looker.HasPermission(person.PermissionTakeItem) { + s.InteractionRespond(i.Interaction, &ErrorNoPermission) + return + } + + player := getPersonFromOptions(i.ApplicationCommandData(), s) + if player == nil { + s.InteractionRespond(i.Interaction, &ErrorInvalidDisplayOrDiscord) + return + } + + item := i.ApplicationCommandData().Options[0].StringValue() + if item == "" { + s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) + return + } + + qty := i.ApplicationCommandData().Options[1].IntValue() + if qty <= 0 { + s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) + return + } + + profile := i.ApplicationCommandData().Options[2].StringValue() + if profile == "" { + s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) + return + } + + if player.GetProfileFromType(profile) == nil { + s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) + return + } + + snapshot := player.GetProfileFromType(profile).Snapshot() + foundItem := player.GetProfileFromType(profile).Items.GetItemByTemplateID(item) + remove := false + switch (foundItem) { + case nil: + s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) + return + default: + foundItem.Quantity -= int(qty) + foundItem.Save() + + if foundItem.Quantity <= 0 { + player.GetProfileFromType(profile).Items.DeleteItem(foundItem.ID) + remove = true + } + } + player.GetProfileFromType(profile).Diff(snapshot) + + str := player.DisplayName + " has had `" + aid.FormatNumber(int(qty)) + "` of `" + item + "` removed from `" + profile + "`." + if remove { + str = player.DisplayName + " has had `" + item + "` removed from `" + profile + "`." + } + + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: str, + }, + }) +} diff --git a/discord/handlers.go b/discord/handlers.go index 6c78121..132f106 100644 --- a/discord/handlers.go +++ b/discord/handlers.go @@ -1,13 +1,8 @@ package discord import ( - "strings" - "github.com/bwmarrin/discordgo" - "github.com/ectrc/snow/aid" - "github.com/ectrc/snow/fortnite" "github.com/ectrc/snow/person" - "github.com/ectrc/snow/storage" ) func addCommand(command *DiscordCommand) { @@ -213,455 +208,6 @@ func addCommands() { }) } -func createHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { - modal := &discordgo.InteractionResponseData{ - CustomID: "create://" + i.Member.User.ID, - Title: "Create an account", - Components: []discordgo.MessageComponent{ - &discordgo.ActionsRow{ - Components: []discordgo.MessageComponent{ - discordgo.TextInput{ - CustomID: "display", - Label: "DISPLAY NAME", - Style: discordgo.TextInputShort, - Placeholder: "Enter your crazy display name here!", - Required: true, - MaxLength: 20, - MinLength: 2, - }, - }, - }, - }, - } - - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseModal, - Data: modal, - }) -} - -func createModalHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { - data := i.ModalSubmitData() - if len(data.Components) <= 0 { - aid.Print("No components found") - return - } - - components, ok := data.Components[0].(*discordgo.ActionsRow) - if !ok { - aid.Print("Failed to assert TextInput") - return - } - - display, ok := components.Components[0].(*discordgo.TextInput) - if !ok { - aid.Print("Failed to assert TextInput") - return - } - - found := person.FindByDiscord(i.Member.User.ID) - if found != nil { - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: "You already have an account with the display name: `"+ found.DisplayName +"`", - }, - }) - return - } - - found = person.FindByDisplay(display.Value) - if found != nil { - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: "Someone already has an account with the display name: `"+ found.DisplayName +"`, please choose another one.", - }, - }) - return - } - - account := fortnite.NewFortnitePerson(display.Value, false) // or aid.Config.Fortnite.Everything - discord := &storage.DB_DiscordPerson{ - ID: i.Member.User.ID, - PersonID: account.ID, - Username: i.Member.User.Username, - } - storage.Repo.SaveDiscordPerson(discord) - account.Discord = discord - account.Save() - - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: "Your account has been created with the display name: `"+ account.DisplayName +"`", - }, - }) -} - -func deleteHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { - found := person.FindByDiscord(i.Member.User.ID) - if found == nil { - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: "You do not have an account with the bot.", - Flags: discordgo.MessageFlagsEphemeral, - }, - }) - return - } - - storage.Repo.DeleteDiscordPerson(found.Discord.ID) - found.Delete() - - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: "Your account has been deleted.", - Flags: discordgo.MessageFlagsEphemeral, - }, - }) -} - -func informationHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { - looker := person.FindByDiscord(i.Member.User.ID) - if looker == nil { - s.InteractionRespond(i.Interaction, &ErrorNoPermission) - return - } - - if !looker.HasPermission(person.PermissionInformation) { - s.InteractionRespond(i.Interaction, &ErrorNoPermission) - return - } - - playerCount := storage.Repo.GetPersonsCount() - totalVbucks := storage.Repo.TotalVBucks() - - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Embeds: []*discordgo.MessageEmbed{ - NewEmbedBuilder(). - SetTitle("Information"). - SetColor(0x2b2d31). - AddField("Players Registered", aid.FormatNumber(playerCount), true). - AddField("Players Online", aid.FormatNumber(0), true). - AddField("VBucks in Circulation", aid.FormatNumber(totalVbucks), false). - Build(), - }, - Flags: discordgo.MessageFlagsEphemeral, - }, - }) -} - -func whoHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { - looker := person.FindByDiscord(i.Member.User.ID) - if looker == nil { - s.InteractionRespond(i.Interaction, &ErrorNoPermission) - return - } - - if !looker.HasPermission(person.PermissionLookup) { - s.InteractionRespond(i.Interaction, &ErrorNoPermission) - return - } - - player := getPersonFromOptions(i.ApplicationCommandData(), s) - if player == nil { - s.InteractionRespond(i.Interaction, &ErrorInvalidDisplayOrDiscord) - return - } - - playerVbucks := player.CommonCoreProfile.Items.GetItemByTemplateID("Currency:MtxPurchased") - if playerVbucks == nil { - return - } - - activeCharacter := func() string { - if player.AthenaProfile == nil { - return "None" - } - - characterId := "" - player.AthenaProfile.Loadouts.RangeLoadouts(func(key string, value *person.Loadout) bool { - characterId = value.CharacterID - return false - }) - - if characterId == "" { - return "None" - } - - character := player.AthenaProfile.Items.GetItem(characterId) - if character == nil { - return "None" - } - - return character.TemplateID - }() - - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Embeds: []*discordgo.MessageEmbed{ - NewEmbedBuilder(). - SetTitle("Player Lookup"). - SetColor(0x2b2d31). - AddField("Display Name", player.DisplayName, true). - AddField("VBucks", aid.FormatNumber(playerVbucks.Quantity), true). - AddField("Discord Account", "<@"+player.Discord.ID+">", true). - AddField("ID", player.ID, true). - SetThumbnail("https://fortnite-api.com/images/cosmetics/br/"+ strings.Split(activeCharacter, ":")[1] +"/icon.png"). - Build(), - }, - Flags: discordgo.MessageFlagsEphemeral, - }, - }) -} - -func meHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { - player := person.FindByDiscord(i.Member.User.ID) - if player == nil { - s.InteractionRespond(i.Interaction, &ErrorNoAccount) - return - } - - playerVbucks := player.CommonCoreProfile.Items.GetItemByTemplateID("Currency:MtxPurchased") - if playerVbucks == nil { - return - } - - activeCharacter := func() string { - if player.AthenaProfile == nil { - return "None" - } - - characterId := "" - player.AthenaProfile.Loadouts.RangeLoadouts(func(key string, value *person.Loadout) bool { - characterId = value.CharacterID - return false - }) - - if characterId == "" { - return "None" - } - - character := player.AthenaProfile.Items.GetItem(characterId) - if character == nil { - return "None" - } - - return character.TemplateID - }() - - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Embeds: []*discordgo.MessageEmbed{ - NewEmbedBuilder(). - SetTitle("Player Lookup"). - SetColor(0x2b2d31). - AddField("Display Name", player.DisplayName, true). - AddField("VBucks", aid.FormatNumber(playerVbucks.Quantity), true). - AddField("Discord Account", "<@"+player.Discord.ID+">", true). - AddField("ID", player.ID, true). - SetThumbnail("https://fortnite-api.com/images/cosmetics/br/"+ strings.Split(activeCharacter, ":")[1] +"/icon.png"). - Build(), - }, - Flags: discordgo.MessageFlagsEphemeral, - }, - }) -} - -func banHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { - looker := person.FindByDiscord(i.Member.User.ID) - if looker == nil { - s.InteractionRespond(i.Interaction, &ErrorNoPermission) - return - } - - if !looker.HasPermission(person.PermissionBan) { - s.InteractionRespond(i.Interaction, &ErrorNoPermission) - return - } - - player := getPersonFromOptions(i.ApplicationCommandData(), s) - if player == nil { - s.InteractionRespond(i.Interaction, &ErrorInvalidDisplayOrDiscord) - return - } - - player.Ban() - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: player.DisplayName + " has been banned.", - }, - }) -} - -func unbanHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { - looker := person.FindByDiscord(i.Member.User.ID) - if looker == nil { - s.InteractionRespond(i.Interaction, &ErrorNoPermission) - return - } - - if !looker.HasPermission(person.PermissionBan) { - s.InteractionRespond(i.Interaction, &ErrorNoPermission) - return - } - - player := getPersonFromOptions(i.ApplicationCommandData(), s) - if player == nil { - s.InteractionRespond(i.Interaction, &ErrorInvalidDisplayOrDiscord) - return - } - - player.Unban() - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: player.DisplayName + " has been unbanned.", - }, - }) -} - -func giveItemHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { - looker := person.FindByDiscord(i.Member.User.ID) - if looker == nil { - s.InteractionRespond(i.Interaction, &ErrorNoAccount) - return - } - - if !looker.HasPermission(person.PermissionGiveItem) { - s.InteractionRespond(i.Interaction, &ErrorNoPermission) - return - } - - player := getPersonFromOptions(i.ApplicationCommandData(), s) - if player == nil { - s.InteractionRespond(i.Interaction, &ErrorInvalidDisplayOrDiscord) - return - } - - item := i.ApplicationCommandData().Options[0].StringValue() - if item == "" { - s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) - return - } - - qty := i.ApplicationCommandData().Options[1].IntValue() - if qty <= 0 { - s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) - return - } - - profile := i.ApplicationCommandData().Options[2].StringValue() - if profile == "" { - s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) - return - } - - if player.GetProfileFromType(profile) == nil { - s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) - return - } - - snapshot := player.GetProfileFromType(profile).Snapshot() - foundItem := player.GetProfileFromType(profile).Items.GetItemByTemplateID(item) - switch (foundItem) { - case nil: - foundItem = person.NewItem(item, int(qty)) - player.GetProfileFromType(profile).Items.AddItem(foundItem) - default: - foundItem.Quantity += int(qty) - } - foundItem.Save() - player.GetProfileFromType(profile).Diff(snapshot) - - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: player.DisplayName + " has been given or updated `" + item + "` in `" + profile + "`.", - }, - }) -} - -func takeItemHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { - looker := person.FindByDiscord(i.Member.User.ID) - if looker == nil { - s.InteractionRespond(i.Interaction, &ErrorNoAccount) - return - } - - if !looker.HasPermission(person.PermissionTakeItem) { - s.InteractionRespond(i.Interaction, &ErrorNoPermission) - return - } - - player := getPersonFromOptions(i.ApplicationCommandData(), s) - if player == nil { - s.InteractionRespond(i.Interaction, &ErrorInvalidDisplayOrDiscord) - return - } - - item := i.ApplicationCommandData().Options[0].StringValue() - if item == "" { - s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) - return - } - - qty := i.ApplicationCommandData().Options[1].IntValue() - if qty <= 0 { - s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) - return - } - - profile := i.ApplicationCommandData().Options[2].StringValue() - if profile == "" { - s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) - return - } - - if player.GetProfileFromType(profile) == nil { - s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) - return - } - - snapshot := player.GetProfileFromType(profile).Snapshot() - foundItem := player.GetProfileFromType(profile).Items.GetItemByTemplateID(item) - remove := false - switch (foundItem) { - case nil: - s.InteractionRespond(i.Interaction, &ErrorInvalidArguments) - return - default: - foundItem.Quantity -= int(qty) - foundItem.Save() - - if foundItem.Quantity <= 0 { - player.GetProfileFromType(profile).Items.DeleteItem(foundItem.ID) - remove = true - } - } - player.GetProfileFromType(profile).Diff(snapshot) - - str := player.DisplayName + " has had `" + aid.FormatNumber(int(qty)) + "` of `" + item + "` removed from `" + profile + "`." - if remove { - str = player.DisplayName + " has had `" + item + "` removed from `" + profile + "`." - } - - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: str, - }, - }) -} - func getPersonFromOptions(data discordgo.ApplicationCommandInteractionData, s *discordgo.Session) *person.Person { options := data.Options diff --git a/handlers/auth.go b/handlers/auth.go index cdf47df..b704f30 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -49,7 +49,7 @@ func PostTokenClientCredentials(c *fiber.Ctx, body *FortniteTokenBody) error { "access_token": "eg1~"+credentials, "token_type": "bearer", "client_id": c.IP(), - "client_service": "snow", + "client_service": "fortnite", "internal_client": true, "expires_in": 3600, "expires_at": time.Now().Add(time.Hour).Format("2006-01-02T15:04:05.999Z"), @@ -86,7 +86,8 @@ func PostTokenPassword(c *fiber.Ctx, body *FortniteTokenBody) error { "access_token": "eg1~"+access, "account_id": person.ID, "client_id": c.IP(), - "client_service": "snow", + "client_service": "fortnite", + "app": "fortnite", "device_id": "default", "display_name": person.DisplayName, "expires_at": time.Now().Add(time.Hour * 24).Format("2006-01-02T15:04:05.999Z"), @@ -135,7 +136,7 @@ func GetOAuthVerify(c *fiber.Ctx) error { "session_id": "0", "device_id": "default", "internal_client": true, - "client_service": "snow", + "client_service": "fortnite", "in_app_id": person.ID, "account_id": person.ID, "displayName": person.DisplayName, diff --git a/handlers/discord.go b/handlers/discord.go index 2c01234..2d1ce1e 100644 --- a/handlers/discord.go +++ b/handlers/discord.go @@ -55,6 +55,8 @@ func GetDiscordOAuthURL(c *fiber.Ctx) error { var user struct { ID string `json:"id"` Username string `json:"username"` + Avatar string `json:"avatar"` + Banner string `json:"banner"` } err = json.NewDecoder(userResponse.Body).Decode(&user) if err != nil { @@ -63,11 +65,14 @@ func GetDiscordOAuthURL(c *fiber.Ctx) error { person := p.FindByDiscord(user.ID) if person == nil { - return c.Status(404).JSON(aid.ErrorNotFound) + return c.Status(404).JSON(aid.JSON{"error":"Person not found. Please create an account first."}) } person.Discord.AccessToken = body.AccessToken person.Discord.RefreshToken = body.RenewToken + person.Discord.Username = user.Username + person.Discord.Avatar = user.Avatar + person.Discord.Banner = user.Banner storage.Repo.SaveDiscordPerson(person.Discord) access, err := aid.JWTSign(aid.JSON{ diff --git a/main.go b/main.go index 9946b13..a83b070 100644 --- a/main.go +++ b/main.go @@ -118,7 +118,7 @@ func main() { player := snow.Group("/player") player.Use(handlers.FrontendMiddleware) - player.Post("/", handlers.AnyNoContent) + player.Get("/", handlers.GetPlayer) player.Get("/locker", handlers.GetPlayerLocker) r.Hooks().OnListen(func(ld fiber.ListenData) error { diff --git a/storage/tables.go b/storage/tables.go index e77e404..d97f381 100644 --- a/storage/tables.go +++ b/storage/tables.go @@ -153,6 +153,8 @@ type DB_DiscordPerson struct { ID string `gorm:"primary_key"` PersonID string Username string + Avatar string + Banner string AccessToken string RefreshToken string }