From 4d3b32e85fdea670a49e4c23b06a2beac517aa74 Mon Sep 17 00:00:00 2001 From: "Gustavo L de Mello (Guz)" Date: Fri, 22 Nov 2024 10:38:22 -0300 Subject: [PATCH] feat(lib,commands,context): add Value of options for ChatCommandContext --- lib/command.go | 61 +++++++++++++++++++++++++++++++++++++++++++++ lib/command_chat.go | 9 +++++++ 2 files changed, 70 insertions(+) diff --git a/lib/command.go b/lib/command.go index f80b380..b641eb9 100644 --- a/lib/command.go +++ b/lib/command.go @@ -27,6 +27,67 @@ type Context struct { type ChatCommandContext struct { Context + Options ChatCommandContextOptions } +var ( + ErrChatCommandOptionNotExists = errors.New("Chat command option does not exist") + 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 74ef0f3..8ce348c 100644 --- a/lib/command_chat.go +++ b/lib/command_chat.go @@ -73,6 +73,7 @@ type ChatCommandOption interface { type ChatCommandStringOption struct { Name string + Value string NameLocalizations map[discordgo.Locale]string Description string DescriptionLocalizations map[discordgo.Locale]string @@ -129,6 +130,7 @@ func (o *ChatCommandStringOption) Validate() (bool, error) { type ChatCommandIntegerOption struct { Name string + Value int NameLocalizations map[discordgo.Locale]string Description string DescriptionLocalizations map[discordgo.Locale]string @@ -177,6 +179,7 @@ func (o *ChatCommandIntegerOption) Validate() (bool, error) { type ChatCommandBooleanOption struct { Name string + Value bool NameLocalizations map[discordgo.Locale]string Description string DescriptionLocalizations map[discordgo.Locale]string @@ -217,6 +220,7 @@ func (o *ChatCommandBooleanOption) Validate() (bool, error) { type ChatCommandUserOption struct { Name string + Value string NameLocalizations map[discordgo.Locale]string Description string DescriptionLocalizations map[discordgo.Locale]string @@ -253,6 +257,7 @@ func (o *ChatCommandUserOption) Validate() (bool, error) { type ChatCommandChannelOption struct { Name string + Value string NameLocalizations map[discordgo.Locale]string Description string DescriptionLocalizations map[discordgo.Locale]string @@ -289,6 +294,7 @@ func (o *ChatCommandChannelOption) Validate() (bool, error) { type ChatCommandRoleOption struct { Name string + Value string NameLocalizations map[discordgo.Locale]string Description string DescriptionLocalizations map[discordgo.Locale]string @@ -325,6 +331,7 @@ func (o *ChatCommandRoleOption) Validate() (bool, error) { type ChatCommandMentionableOption struct { Name string + Value string NameLocalizations map[discordgo.Locale]string Description string DescriptionLocalizations map[discordgo.Locale]string @@ -361,6 +368,7 @@ func (o *ChatCommandMentionableOption) Validate() (bool, error) { type ChatCommandNumberOption struct { Name string + Value float64 NameLocalizations map[discordgo.Locale]string Description string DescriptionLocalizations map[discordgo.Locale]string @@ -407,6 +415,7 @@ func (o *ChatCommandNumberOption) Validate() (bool, error) { type ChatCommandAttachmentOption struct { Name string + Value string NameLocalizations map[discordgo.Locale]string Description string DescriptionLocalizations map[discordgo.Locale]string