From 84dad980a9af0a700fbaa366ec2c9c7d0cbb056b Mon Sep 17 00:00:00 2001 From: "Gustavo L de Mello (Guz)" Date: Fri, 22 Nov 2024 18:46:16 -0300 Subject: [PATCH] refactor(lib,commands,options): create generic ChatCommandOptionChoice type --- lib/command_chat.go | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/lib/command_chat.go b/lib/command_chat.go index 382b445..34665c8 100644 --- a/lib/command_chat.go +++ b/lib/command_chat.go @@ -152,17 +152,11 @@ type ChatCommandIntegerOption struct { DescriptionLocalizations map[discordgo.Locale]string Required bool Autocomplete bool - Choices []*ChatCommandIntegerOptionChoice + Choices []*ChatCommandOptionChoice[int] MinValue int MaxValue int } -type ChatCommandIntegerOptionChoice struct { - Name string - NameLocalizations map[discordgo.Locale]string - Value int -} - func (o *ChatCommandIntegerOption) ApplicationCommandOption() *discordgo.ApplicationCommandOption { choices := make([]*discordgo.ApplicationCommandOptionChoice, len(o.Choices)) for i, v := range o.Choices { @@ -243,17 +237,11 @@ type ChatCommandNumberOption struct { DescriptionLocalizations map[discordgo.Locale]string Required bool Autocomplete bool - Choices []*ChatCommandNumberOptionChoice + Choices []*ChatCommandOptionChoice[float64] MinValue float64 MaxValue float64 } -type ChatCommandNumberOptionChoice struct { - Name string - NameLocalizations map[discordgo.Locale]string - Value float64 -} - func (o *ChatCommandNumberOption) ApplicationCommandOption() *discordgo.ApplicationCommandOption { choices := make([]*discordgo.ApplicationCommandOptionChoice, len(o.Choices)) for i, v := range o.Choices { @@ -332,17 +320,11 @@ type ChatCommandStringOption struct { DescriptionLocalizations map[discordgo.Locale]string Required bool Autocomplete bool - Choices []*ChatCommandStringOptionChoice + Choices []*ChatCommandOptionChoice[string] MinLength int MaxLength int } -type ChatCommandStringOptionChoice struct { - Name string - NameLocalizations map[discordgo.Locale]string - Value string -} - func (o *ChatCommandStringOption) ApplicationCommandOption() *discordgo.ApplicationCommandOption { choices := make([]*discordgo.ApplicationCommandOptionChoice, len(o.Choices)) for i, v := range o.Choices { @@ -445,3 +427,12 @@ func validateOption(opt interface { return true, nil } + +type ( + optionTypes interface{ string | int | float64 } + ChatCommandOptionChoice[T optionTypes] struct { + Name string + NameLocalizations map[discordgo.Locale]string + Value T + } +)