From c50b636c03592d9bf46b977d78688ff8d434a680 Mon Sep 17 00:00:00 2001 From: "Gustavo L de Mello (Guz)" Date: Fri, 22 Nov 2024 19:06:32 -0300 Subject: [PATCH] refactor(lib,commands,context): move ChatCommandContextOptions to commands_chat.go and rename Context to Ctx --- lib/command.go | 63 +++---------------------------------- lib/command_chat.go | 77 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 71 insertions(+), 69 deletions(-) diff --git a/lib/command.go b/lib/command.go index 481366e..19fbefb 100644 --- a/lib/command.go +++ b/lib/command.go @@ -21,13 +21,13 @@ func (h HandlerFunc[CTX]) Handle(ctx CTX) error { return h(ctx) } -type Context struct { +type Ctx struct { discordgo.Interaction } -type ChatCommandContext struct { - Context - Options ChatCommandContextOptions +type ChatCommandCtx struct { + Ctx + Options ChatCommandCtxOptions } var ( @@ -35,59 +35,4 @@ var ( ErrChatCommandOptionInvalidType = errors.New("chat command option is not of the type requested") ) -type ChatCommandContextOptions map[string]ChatCommandOption -func get[T ChatCommandOption](opts ChatCommandContextOptions, key string) (T, error) { - var v T - - mv, ok := opts[key] - if !ok { - return v, ErrChatCommandOptionNotExists - } - - if v, ok := mv.(T); !ok { - return v, ErrChatCommandOptionInvalidType - } else { - return v, nil - } -} - -func (opts ChatCommandContextOptions) GetAttachement( - key string, -) (*ChatCommandAttachmentOption, error) { - return get[*ChatCommandAttachmentOption](opts, key) -} - -func (opts ChatCommandContextOptions) GetBoolean(key string) (*ChatCommandBooleanOption, error) { - return get[*ChatCommandBooleanOption](opts, key) -} - -func (opts ChatCommandContextOptions) GetChannel(key string) (*ChatCommandChannelOption, error) { - return get[*ChatCommandChannelOption](opts, key) -} - -func (opts ChatCommandContextOptions) GetInteger(key string) (*ChatCommandIntegerOption, error) { - return get[*ChatCommandIntegerOption](opts, key) -} - -func (opts ChatCommandContextOptions) GetMentionable( - key string, -) (*ChatCommandMentionableOption, error) { - return get[*ChatCommandMentionableOption](opts, key) -} - -func (opts ChatCommandContextOptions) GetNumber(key string) (*ChatCommandNumberOption, error) { - return get[*ChatCommandNumberOption](opts, key) -} - -func (opts ChatCommandContextOptions) GetRole(key string) (*ChatCommandRoleOption, error) { - return get[*ChatCommandRoleOption](opts, key) -} - -func (opts ChatCommandContextOptions) GetString(key string) (*ChatCommandStringOption, error) { - return get[*ChatCommandStringOption](opts, key) -} - -func (opts ChatCommandContextOptions) GetUser(key string) (*ChatCommandUserOption, error) { - return get[*ChatCommandUserOption](opts, key) -} diff --git a/lib/command_chat.go b/lib/command_chat.go index 5837150..f84cd3c 100644 --- a/lib/command_chat.go +++ b/lib/command_chat.go @@ -17,7 +17,7 @@ type ChatCommand struct { Description string DescriptionLocalizations *map[discordgo.Locale]string Options []ChatCommandOption - Handler Handler[ChatCommandContext] + Handler Handler[ChatCommandCtx] } func (c *ChatCommand) ApplicationCommand() *discordgo.ApplicationCommand { @@ -67,6 +67,48 @@ func (c *ChatCommand) Validate() error { return nil } +type ChatCommandCtxOptions map[string]ChatCommandOption + +func (opts ChatCommandCtxOptions) GetAttachement( + key string, +) (*ChatCommandAttachmentOption, error) { + return get[*ChatCommandAttachmentOption](opts, key) +} + +func (opts ChatCommandCtxOptions) GetBoolean(key string) (*ChatCommandBooleanOption, error) { + return get[*ChatCommandBooleanOption](opts, key) +} + +func (opts ChatCommandCtxOptions) GetChannel(key string) (*ChatCommandChannelOption, error) { + return get[*ChatCommandChannelOption](opts, key) +} + +func (opts ChatCommandCtxOptions) GetInteger(key string) (*ChatCommandIntegerOption, error) { + return get[*ChatCommandIntegerOption](opts, key) +} + +func (opts ChatCommandCtxOptions) GetMentionable( + key string, +) (*ChatCommandMentionableOption, error) { + return get[*ChatCommandMentionableOption](opts, key) +} + +func (opts ChatCommandCtxOptions) GetNumber(key string) (*ChatCommandNumberOption, error) { + return get[*ChatCommandNumberOption](opts, key) +} + +func (opts ChatCommandCtxOptions) GetRole(key string) (*ChatCommandRoleOption, error) { + return get[*ChatCommandRoleOption](opts, key) +} + +func (opts ChatCommandCtxOptions) GetString(key string) (*ChatCommandStringOption, error) { + return get[*ChatCommandStringOption](opts, key) +} + +func (opts ChatCommandCtxOptions) GetUser(key string) (*ChatCommandUserOption, error) { + return get[*ChatCommandUserOption](opts, key) +} + type ChatCommandOption interface { ApplicationCommandOption() *discordgo.ApplicationCommandOption Validate() error @@ -408,6 +450,30 @@ func (o *ChatCommandUserOption) Validate() error { return validateOption(o) } +type ( + optionTypes interface{ string | int | float64 } + ChatCommandOptionChoice[T optionTypes] struct { + Name string + NameLocalizations map[discordgo.Locale]string + Value T + } +) + +func get[T ChatCommandOption](opts ChatCommandCtxOptions, key string) (T, error) { + var v T + + mv, ok := opts[key] + if !ok { + return v, ErrChatCommandOptionNotExists + } + + if v, ok := mv.(T); !ok { + return v, ErrChatCommandOptionInvalidType + } else { + return v, nil + } +} + func validateOption(opt interface { ApplicationCommandOption() *discordgo.ApplicationCommandOption }, @@ -427,12 +493,3 @@ func validateOption(opt interface { return nil } - -type ( - optionTypes interface{ string | int | float64 } - ChatCommandOptionChoice[T optionTypes] struct { - Name string - NameLocalizations map[discordgo.Locale]string - Value T - } -)