|
| 1 | +package discordfx |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/bwmarrin/discordgo" |
| 8 | + "github.com/rs/zerolog/log" |
| 9 | + "go.uber.org/fx" |
| 10 | +) |
| 11 | + |
| 12 | +type HandlerFunc func(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate) |
| 13 | + |
| 14 | +// ApplicationCommandWithHandler is a top level Command with a Handler function. |
| 15 | +// Subcommands are TODO |
| 16 | +// todo: define our own handler func? want to use channels to post back messages. |
| 17 | +type ApplicationCommandWithHandler struct { |
| 18 | + Command discordgo.ApplicationCommand |
| 19 | + Handler HandlerFunc |
| 20 | + // TODO (cthompson) a bit of a hack right now, these should probably be their own type |
| 21 | + MessageComponent bool |
| 22 | + GuildID string |
| 23 | +} |
| 24 | + |
| 25 | +type RegisterCommandsParams struct { |
| 26 | + fx.In |
| 27 | + Config DiscordConfig |
| 28 | + Session *discordgo.Session |
| 29 | + Commands []*ApplicationCommandWithHandler `group:"command"` |
| 30 | + Lifecycle fx.Lifecycle |
| 31 | +} |
| 32 | + |
| 33 | +type interactionHelper struct { |
| 34 | + i *discordgo.InteractionCreate |
| 35 | +} |
| 36 | + |
| 37 | +type InteractionHelper interface { |
| 38 | + GetInteraction() *discordgo.InteractionCreate |
| 39 | + Respond() (<-chan *discordgo.WebhookEdit, error) |
| 40 | + RespondWebhook() (<-chan *discordgo.WebhookEdit, error) |
| 41 | +} |
| 42 | + |
| 43 | +func NewInteractionHelper(i *discordgo.InteractionCreate) InteractionHelper { |
| 44 | + //return &interactionHelper{i: i} |
| 45 | + return nil |
| 46 | +} |
| 47 | + |
| 48 | +func RegisterCommands(p RegisterCommandsParams) error { |
| 49 | + handlerMap := make(map[string]HandlerFunc) |
| 50 | + registeredCommands := make([]*discordgo.ApplicationCommand, 0, len(p.Commands)) |
| 51 | + |
| 52 | + ctx, _ := context.WithCancel(context.Background()) |
| 53 | + |
| 54 | + // Set up command |
| 55 | + for _, commandHandler := range p.Commands { |
| 56 | + handlerMap[commandHandler.Command.Name] = commandHandler.Handler |
| 57 | + } |
| 58 | + |
| 59 | + p.Session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { |
| 60 | + ctx, cancel := context.WithTimeout(ctx, time.Second*15) |
| 61 | + defer cancel() |
| 62 | + |
| 63 | + switch i.Type { |
| 64 | + case discordgo.InteractionApplicationCommand: |
| 65 | + log.Info(). |
| 66 | + Str("handlerName", i.ApplicationCommandData().Name). |
| 67 | + Msg("invoking command handler") |
| 68 | + if h, ok := handlerMap[i.ApplicationCommandData().Name]; ok { |
| 69 | + h(ctx, s, i) |
| 70 | + } |
| 71 | + case discordgo.InteractionMessageComponent: |
| 72 | + customID := i.MessageComponentData().CustomID |
| 73 | + log.Info(). |
| 74 | + Str("customID", customID). |
| 75 | + Msg("invoking message component handler") |
| 76 | + if h, ok := handlerMap[customID]; ok { |
| 77 | + h(ctx, s, i) |
| 78 | + } |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + p.Lifecycle.Append(fx.Hook{OnStart: func(ctx context.Context) error { |
| 83 | + for _, v := range p.Commands { |
| 84 | + if v.MessageComponent { |
| 85 | + continue |
| 86 | + } |
| 87 | + |
| 88 | + ccmd, err := p.Session.ApplicationCommandCreate(p.Config.ApplicationID, v.GuildID, &v.Command) |
| 89 | + if err != nil { |
| 90 | + log.Error().Err(err).Msg("failed to register command") |
| 91 | + return err |
| 92 | + } |
| 93 | + log.Info(). |
| 94 | + Str("name", ccmd.Name). |
| 95 | + Str("id", ccmd.ID). |
| 96 | + Msg("registered command") |
| 97 | + |
| 98 | + registeredCommands = append(registeredCommands, ccmd) |
| 99 | + } |
| 100 | + return nil |
| 101 | + }, |
| 102 | + //OnStop: func(ctx context.Context) error { |
| 103 | + // cancel() |
| 104 | + // registeredCommands, err := p.Session.ApplicationCommands(p.Config.ApplicationID, "") |
| 105 | + // if err != nil { |
| 106 | + // log.Error(). |
| 107 | + // Err(err). |
| 108 | + // Msg("could not fetch registered commands") |
| 109 | + // return err |
| 110 | + // } |
| 111 | + // for _, v := range registeredCommands { |
| 112 | + // err := p.Session.ApplicationCommandDelete(p.Config.ApplicationID, v.GuildID, v.ID) |
| 113 | + // if err != nil { |
| 114 | + // log.Error().Err(err).Msg("failed to delete command") |
| 115 | + // return err |
| 116 | + // } |
| 117 | + // log.Debug(). |
| 118 | + // Str("handlerName", v.Name). |
| 119 | + // Str("id", v.ID). |
| 120 | + // Msg("deleted command") |
| 121 | + // } |
| 122 | + // return nil |
| 123 | + //} |
| 124 | + }) |
| 125 | + return nil |
| 126 | +} |
0 commit comments