76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
package discord
|
|
|
|
import (
|
|
"log/slog"
|
|
"strings"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
"github.com/leolionad58/ticketbot/internal/discord/commands"
|
|
"github.com/leolionad58/ticketbot/internal/discord/components"
|
|
)
|
|
|
|
// Router dispatches Discord interactions to the appropriate handler.
|
|
type Router struct {
|
|
PanelCmd *commands.PanelCommand
|
|
TicketCmd *commands.TicketCommand
|
|
ConvocCmd *commands.ConvocationCommand
|
|
PanelComp *components.PanelComponent
|
|
TicketComp *components.TicketComponent
|
|
}
|
|
|
|
func (r *Router) Handle(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
switch i.Type {
|
|
case discordgo.InteractionApplicationCommand:
|
|
r.routeCommand(s, i)
|
|
case discordgo.InteractionMessageComponent:
|
|
r.routeComponent(s, i)
|
|
}
|
|
}
|
|
|
|
func (r *Router) routeCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
name := i.ApplicationCommandData().Name
|
|
slog.Debug("command received", "name", name, "user_id", memberUserID(i), "guild_id", i.GuildID)
|
|
switch name {
|
|
case "panel_send":
|
|
r.PanelCmd.Handle(s, i)
|
|
case "ticket":
|
|
r.TicketCmd.Handle(s, i)
|
|
case "convocation":
|
|
r.ConvocCmd.Handle(s, i)
|
|
default:
|
|
slog.Warn("unknown command", "name", name)
|
|
}
|
|
}
|
|
|
|
func (r *Router) routeComponent(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
customID := i.MessageComponentData().CustomID
|
|
slog.Debug("component received", "custom_id", customID, "user_id", memberUserID(i))
|
|
|
|
switch {
|
|
case strings.HasPrefix(customID, "panel:open:"):
|
|
r.PanelComp.Handle(s, i)
|
|
case strings.HasPrefix(customID, "claim:take:"):
|
|
r.TicketComp.HandleClaim(s, i)
|
|
case strings.HasPrefix(customID, "ticket:delete:confirm:"):
|
|
r.TicketComp.HandleDeleteConfirm(s, i)
|
|
case strings.HasPrefix(customID, "ticket:delete:yes:"):
|
|
r.TicketComp.HandleDeleteYes(s, i)
|
|
case customID == "ticket:delete:cancel":
|
|
r.TicketComp.HandleDeleteCancel(s, i)
|
|
case strings.HasPrefix(customID, "convoc:delete:"):
|
|
r.TicketComp.HandleConvocationDelete(s, i)
|
|
default:
|
|
slog.Warn("unknown component", "custom_id", customID)
|
|
}
|
|
}
|
|
|
|
func memberUserID(i *discordgo.InteractionCreate) string {
|
|
if i.Member != nil && i.Member.User != nil {
|
|
return i.Member.User.ID
|
|
}
|
|
if i.User != nil {
|
|
return i.User.ID
|
|
}
|
|
return ""
|
|
}
|