diff options
| author | ascpial <mail@ascpial.fr> | 2025-09-27 23:35:32 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-27 23:35:32 +0200 |
| commit | bc86bb4859c4537032f9ca8d57ac32cc14dbd629 (patch) | |
| tree | e94686d7b091857788fe3f1b582f6ce540e00d71 /dynamicid/handling.go | |
| parent | cfdba5f417bb31aac564d13becc09874f17d075d (diff) | |
[Feat] Role reaction (#15)
* first draft of rolereact
* fix(rolereact): fill description when setting it
* fix(rolereact): fix some issues
* feat(rolereact): split the code in multiple files
Diffstat (limited to 'dynamicid/handling.go')
| -rw-r--r-- | dynamicid/handling.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/dynamicid/handling.go b/dynamicid/handling.go new file mode 100644 index 0000000..8369e27 --- /dev/null +++ b/dynamicid/handling.go @@ -0,0 +1,74 @@ +package dynamicid + +import ( + "strings" + + "github.com/anhgelus/gokord" + "github.com/anhgelus/gokord/cmd" + "github.com/nyttikord/gokord/bot" + "github.com/nyttikord/gokord/discord/types" + "github.com/nyttikord/gokord/event" + "github.com/nyttikord/gokord/interaction" +) + +func HandleDynamicMessageComponent[DynamicData any]( + b *gokord.Bot, + handler func( + bot.Session, + *event.InteractionCreate, + *interaction.MessageComponentData, + *DynamicData, *cmd.ResponseBuilder, + ), + base string, +) { + b.AddHandler(func(s bot.Session, i *event.InteractionCreate) { + if i.Type != types.InteractionMessageComponent { + return + } + data := i.MessageComponentData() + if !strings.HasPrefix(data.CustomID, base+";") { + return + } + dynamicID := data.CustomID[len(base)+1:] + dynamicData := new(DynamicData) + err := UnmarshallCSV(dynamicID, dynamicData) + if err != nil { + s.Logger().Error("Unable to parse CustomID", "error", err, "CustomID", data.CustomID, "base", base) + return + } + handler(s, i, data, dynamicData, cmd.NewResponseBuilder(s, i)) + }) +} + +func HandleDynamicModalComponent[DynamicData any]( + b *gokord.Bot, + handler func( + bot.Session, + *event.InteractionCreate, + *interaction.ModalSubmitData, + *DynamicData, + *cmd.ResponseBuilder, + ), + base string, +) { + b.AddHandler(func(s bot.Session, i *event.InteractionCreate) { + if i.Type != types.InteractionModalSubmit { + return + } + data := i.ModalSubmitData() + if strings.HasPrefix(data.CustomID, base+";") { + dynamicID := data.CustomID[len(base)+1:] + dynamicData := new(DynamicData) + err := UnmarshallCSV(dynamicID, dynamicData) + if err != nil { + s.Logger().Error("Unable to parse CustomID", "error", err, "CustomID", data.CustomID, "base", base) + return + } + handler(s, i, data, dynamicData, cmd.NewResponseBuilder(s, i)) + } + }) +} + +func FormatCustomID(base string, dynamicData any) string { + return base + ";" + MarshallCSV(dynamicData) +} |
