snow/discord/discord.go

141 lines
3.6 KiB
Go
Raw Permalink Normal View History

2023-12-13 22:52:16 +00:00
package discord
import (
"strings"
"github.com/bwmarrin/discordgo"
"github.com/ectrc/snow/aid"
)
type DiscordCommand struct {
Command *discordgo.ApplicationCommand
Handler func(s *discordgo.Session, i *discordgo.InteractionCreate)
AdminOnly bool
}
type DiscordModal struct {
ID string
Handler func(s *discordgo.Session, i *discordgo.InteractionCreate)
}
type DiscordClient struct {
Client *discordgo.Session
Commands map[string]*DiscordCommand
Modals map[string]*DiscordModal
}
var StaticClient *DiscordClient
func NewDiscordClient(token string) *DiscordClient {
client, err := discordgo.New("Bot " + token)
if err != nil {
panic(err)
}
client.Identify.Intents = discordgo.IntentsAllWithoutPrivileged
return &DiscordClient{
Client: client,
Commands: make(map[string]*DiscordCommand),
Modals: make(map[string]*DiscordModal),
}
}
func IntialiseClient() {
StaticClient = NewDiscordClient(aid.Config.Discord.Token)
StaticClient.Client.AddHandler(StaticClient.readyHandler)
StaticClient.Client.AddHandler(StaticClient.interactionHandler)
addCommands()
2023-12-14 19:48:33 +00:00
if len(StaticClient.Commands) != len(StaticClient.GetRegisteredCommands()) {
2023-12-26 03:39:12 +00:00
// StaticClient.UnregisterCommands()
2023-12-14 19:47:54 +00:00
StaticClient.RegisterCommands()
2023-12-13 22:52:16 +00:00
}
err := StaticClient.Client.Open()
if err != nil {
aid.Print("(discord) failed to connect; will be disabled")
2023-12-13 22:52:16 +00:00
}
}
2023-12-14 19:47:54 +00:00
func (c *DiscordClient) UnregisterCommands() {
commands := c.GetRegisteredCommands()
if commands == nil {
return
}
for _, command := range commands {
err := c.Client.ApplicationCommandDelete(aid.Config.Discord.ID, aid.Config.Discord.Guild, command.ID)
if err != nil {
2024-01-03 19:56:23 +00:00
aid.Print("(discord) failed to delete command: " + command.Name)
2023-12-14 19:47:54 +00:00
}
}
commands = c.GetGlobalRegisteredCommands()
for _, command := range commands {
err := c.Client.ApplicationCommandDelete(aid.Config.Discord.ID, "", command.ID)
if err != nil {
2024-01-03 19:56:23 +00:00
aid.Print("(discord) failed to delete command: " + command.Name)
2023-12-14 19:47:54 +00:00
}
}
}
func (c *DiscordClient) RegisterCommands() {
adminPermission := int64(discordgo.PermissionAdministrator)
update := []*discordgo.ApplicationCommand{}
for _, command := range c.Commands {
if command.AdminOnly {
command.Command.DefaultMemberPermissions = &adminPermission
2024-02-10 00:34:12 +00:00
command.Command.Description += " (admin only)"
2023-12-14 19:47:54 +00:00
}
update = append(update, command.Command)
2023-12-13 22:52:16 +00:00
}
2023-12-14 19:47:54 +00:00
_, err := c.Client.ApplicationCommandBulkOverwrite(aid.Config.Discord.ID, aid.Config.Discord.Guild, update)
2023-12-13 22:52:16 +00:00
if err != nil {
2024-01-03 19:56:23 +00:00
aid.Print("(discord) failed to register commands", err)
2023-12-13 22:52:16 +00:00
return
}
}
2023-12-14 19:47:54 +00:00
func (c *DiscordClient) GetRegisteredCommands() []*discordgo.ApplicationCommand {
commands, err := c.Client.ApplicationCommands(aid.Config.Discord.ID, aid.Config.Discord.Guild)
if err != nil {
2024-01-03 19:56:23 +00:00
aid.Print("(discord) failed to get commands")
2023-12-14 19:47:54 +00:00
return nil
}
return commands
}
func (c *DiscordClient) GetGlobalRegisteredCommands() []*discordgo.ApplicationCommand {
commands, err := c.Client.ApplicationCommands(aid.Config.Discord.ID, "")
if err != nil {
2024-01-03 19:56:23 +00:00
aid.Print("(discord) failed to get commands")
2023-12-14 19:47:54 +00:00
return nil
}
return commands
}
2023-12-13 22:52:16 +00:00
func (c *DiscordClient) readyHandler(s *discordgo.Session, event *discordgo.Ready) {
2024-01-03 19:56:23 +00:00
aid.Print("(discord) bot is ready")
2023-12-13 22:52:16 +00:00
}
func (c *DiscordClient) interactionHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
if command, ok := c.Commands[i.ApplicationCommandData().Name]; ok {
command.Handler(s, i)
}
case discordgo.InteractionModalSubmit:
if modal, ok := c.Modals[strings.Split(i.ModalSubmitData().CustomID, "://")[0]]; ok {
modal.Handler(s, i)
}
}
}