snow/discord/handlers.go

255 lines
5.6 KiB
Go
Raw Normal View History

2023-12-13 22:52:16 +00:00
package discord
import (
"github.com/bwmarrin/discordgo"
"github.com/ectrc/snow/person"
)
func addCommand(command *DiscordCommand) {
StaticClient.Commands[command.Command.Name] = command
}
func addModal(modal *DiscordModal) {
StaticClient.Modals[modal.ID] = modal
}
func addCommands() {
if StaticClient == nil {
panic("StaticClient is nil")
}
2024-02-10 00:34:12 +00:00
personOptions := []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionUser,
Name: "discord",
Description: "The discord account of the player.",
Required: false,
},
{
Type: discordgo.ApplicationCommandOptionString,
Name: "display",
Description: "The display name of the player.",
Required: false,
},
}
2023-12-13 22:52:16 +00:00
addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{
Name: "create",
Description: "Create an account with the bot.",
},
Handler: createHandler,
})
addModal(&DiscordModal{
ID: "create",
Handler: createModalHandler,
})
2023-12-26 03:39:12 +00:00
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,
})
2023-12-13 22:52:16 +00:00
addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{
Name: "information",
2024-02-10 00:34:12 +00:00
Description: "Useful information about this server's activity!",
2023-12-13 22:52:16 +00:00
},
Handler: informationHandler,
AdminOnly: true,
})
2023-12-14 19:47:54 +00:00
addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{
Name: "who",
Description: "Lookup a player's information.",
2024-02-10 00:34:12 +00:00
Options: personOptions,
2023-12-14 19:47:54 +00:00
},
Handler: whoHandler,
AdminOnly: true,
})
addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{
Name: "ban",
Description: "Ban a player from using the bot.",
2024-02-10 00:34:12 +00:00
Options: personOptions,
2023-12-14 19:47:54 +00:00
},
Handler: banHandler,
AdminOnly: true,
})
addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{
Name: "unban",
Description: "Unban a player from using the bot.",
2024-02-10 00:34:12 +00:00
Options: personOptions,
2023-12-14 19:47:54 +00:00
},
Handler: unbanHandler,
AdminOnly: true,
})
2024-02-10 00:34:12 +00:00
grantOptions := append([]*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "template_id",
Description: "The item id of the cosmetic to give/take.",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "quantity",
Description: "The amount of the item to give/take.",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionString,
Name: "profile",
Description: "common_core, athena, common_public, profile0, collections, creative",
Required: true,
},
}, personOptions...)
2023-12-14 19:47:54 +00:00
addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{
Name: "give",
Description: "Grant a player an item in the game.",
2024-02-10 00:34:12 +00:00
Options: grantOptions,
2023-12-14 19:47:54 +00:00
},
Handler: giveItemHandler,
AdminOnly: true,
})
addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{
Name: "take",
Description: "Take an item from a player in the game.",
2024-02-10 00:34:12 +00:00
Options: grantOptions,
2023-12-14 19:47:54 +00:00
},
Handler: takeItemHandler,
AdminOnly: true,
})
addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{
Name: "everything",
Description: "Give a player full locker",
2024-02-10 00:34:12 +00:00
Options: personOptions,
},
Handler: giveEverythingHandler,
AdminOnly: true,
})
2023-12-13 22:52:16 +00:00
2024-02-10 00:34:12 +00:00
permissionOptionChoices := []*discordgo.ApplicationCommandOptionChoice{
{
Name: "All",
Value: person.PermissionAll,
},
{
Name: "Lookup",
Value: person.PermissionLookup,
},
{
Name: "Information",
Value: person.PermissionInformation,
},
{
Name: "Donator",
Value: person.PermissionDonator,
},
{
Name: "ItemControl",
Value: person.PermissionItemControl,
},
{
Name: "LockerControl",
Value: person.PermissionLockerControl,
},
{
Name: "Owner",
Value: person.PermissionOwner,
},
{
Name: "PermissionControl",
Value: person.PermissionPermissionControl,
},
}
permissionOptions := append([]*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "permission",
Description: "The permission to add/take.",
Required: true,
Choices: permissionOptionChoices,
},
}, personOptions...)
permissionSubCommands := []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "add",
Description: "Add a permission to a player.",
Options: permissionOptions,
},
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "remove",
Description: "Rake a permission from a player.",
Options: permissionOptions,
},
}
addCommand(&DiscordCommand{
Command: &discordgo.ApplicationCommand{
Name: "permission",
Description: "Give or take permissions from a player.",
Options: permissionSubCommands,
},
Handler: permissionHandler,
AdminOnly: true,
})
}
2023-12-14 19:47:54 +00:00
2024-02-10 00:34:12 +00:00
func getPersonFromOptions(opts []*discordgo.ApplicationCommandInteractionDataOption, s *discordgo.Session) *person.Person {
if len(opts) <= 0 {
2023-12-14 19:47:54 +00:00
return nil
}
2024-02-10 00:34:12 +00:00
for _, option := range opts {
2023-12-14 19:47:54 +00:00
switch option.Type {
case discordgo.ApplicationCommandOptionUser:
if option.Name != "discord" {
continue
}
return person.FindByDiscord(option.UserValue(s).ID)
case discordgo.ApplicationCommandOptionString:
if option.Name != "display" {
continue
}
return person.FindByDisplay(option.StringValue())
}
}
return nil
2023-12-13 22:52:16 +00:00
}