discord: Use StringLocales, tweak naming

This commit adds better comments and more consistent field names in
discord/command.go.

It also renames CommandLocales to StringLocales, since the localization
is fairly generic and isn't specific to commands.
This commit is contained in:
diamondburned 2022-04-11 21:22:08 -07:00
parent 7ed7983b7b
commit 95e9145ddb
No known key found for this signature in database
GPG Key ID: D78C4471CE776659
1 changed files with 114 additions and 90 deletions

View File

@ -33,14 +33,15 @@ type Command struct {
// GuildID is the guild id of the command, if not global. // GuildID is the guild id of the command, if not global.
GuildID GuildID `json:"guild_id,omitempty"` GuildID GuildID `json:"guild_id,omitempty"`
// Name is the 1-32 lowercase character name matching ^[\w-]{1,32}$. // Name is the 1-32 lowercase character name matching ^[\w-]{1,32}$.
Name string `json:"name"` Name string `json:"name"`
NameLocalizations CommandLocales `json:"name_localizations,omitempty"` NameLocalizations StringLocales `json:"name_localizations,omitempty"`
// Description is the 1-100 character description. // Description is the 1-100 character description.
Description string `json:"description"` Description string `json:"description"`
DescriptionLocalizations CommandLocales `json:"description_localizations,omitempty"` DescriptionLocalizations StringLocales `json:"description_localizations,omitempty"`
// The fields LocalizedName, LocalizedDescription are // LocalizedName is only populated when this is received from Discord's API.
// populated only when data is received. LocalizedName string `json:"name_localized,omitempty"`
LocalizedName string `json:"name_localized,omitempty"` // LocalizedDescription is only populated when this is received from
// Discord's API.
LocalizedDescription string `json:"description_localized,omitempty"` LocalizedDescription string `json:"description_localized,omitempty"`
// Options are the parameters for the command. Its types are value types, // Options are the parameters for the command. Its types are value types,
// which can either be a SubcommandOption or a SubcommandGroupOption. // which can either be a SubcommandOption or a SubcommandGroupOption.
@ -59,12 +60,15 @@ type Command struct {
Version Snowflake `json:"version,omitempty"` Version Snowflake `json:"version,omitempty"`
} }
// Please refer to the documentation below for the languages available for localization. // Language is a string type for language codes, such as "en-US" or "fr". Refer
// to the constants for valid language codes.
// //
// The list of all valid language codes are at
// https://discord.com/developers/docs/reference#locales // https://discord.com/developers/docs/reference#locales
type Language string type Language string
type CommandLocales map[Language]string // StringLocales is the map mapping a language code to a localized string.
type StringLocales map[Language]string
const ( const (
Danish Language = "da" Danish Language = "da"
@ -328,14 +332,16 @@ type CommandOption interface {
// SubcommandGroupOption is a subcommand group that fits into a CommandOption. // SubcommandGroupOption is a subcommand group that fits into a CommandOption.
type SubcommandGroupOption struct { type SubcommandGroupOption struct {
OptionName string `json:"name"` OptionName string `json:"name"`
OptionNameLocalizations StringLocales `json:"name_localizations,omitempty"`
Description string `json:"description"` Description string `json:"description"`
NameLocalizations CommandLocales `json:"name_localizations,omitempty"` DescriptionLocalizations StringLocales `json:"description_localizations,omitempty"`
DescriptionLocalizations CommandLocales `json:"description_localizations,omitempty"`
Required bool `json:"required"` Required bool `json:"required"`
Subcommands []*SubcommandOption `json:"options"` Subcommands []*SubcommandOption `json:"options"`
// The fields LocalizedName, LocalizedDescription are // LocalizedOptionName is only populated when this is received from
// populated only when data is received. // Discord's API.
LocalizedName string `json:"name_localized,omitempty"` LocalizedOptionName string `json:"name_localized,omitempty"`
// LocalizedDescription is only populated when this is received from
// Discord's API.
LocalizedDescription string `json:"description_localized,omitempty"` LocalizedDescription string `json:"description_localized,omitempty"`
} }
@ -347,18 +353,20 @@ func (s *SubcommandGroupOption) Type() CommandOptionType { return SubcommandGrou
// SubcommandOption is a subcommand option that fits into a CommandOption. // SubcommandOption is a subcommand option that fits into a CommandOption.
type SubcommandOption struct { type SubcommandOption struct {
OptionName string `json:"name"` OptionName string `json:"name"`
Description string `json:"description"` OptionNameLocalizations StringLocales `json:"name_localizations,omitempty"`
NameLocalizations CommandLocales `json:"name_localizations,omitempty"` Description string `json:"description"`
DescriptionLocalizations CommandLocales `json:"description_localizations,omitempty"` DescriptionLocalizations StringLocales `json:"description_localizations,omitempty"`
Required bool `json:"required"` Required bool `json:"required"`
// Options contains command option values. All CommandOption types except // Options contains command option values. All CommandOption types except
// for SubcommandOption and SubcommandGroupOption will implement this // for SubcommandOption and SubcommandGroupOption will implement this
// interface. // interface.
Options []CommandOptionValue `json:"options"` Options []CommandOptionValue `json:"options"`
// The fields LocalizedName, LocalizedDescription are // LocalizedOptionName is only populated when this is received from
// populated only when data is received. // Discord's API.
LocalizedName string `json:"name_localized,omitempty"` LocalizedOptionName string `json:"name_localized,omitempty"`
// LocalizedDescription is only populated when this is received from
// Discord's API.
LocalizedDescription string `json:"description_localized,omitempty"` LocalizedDescription string `json:"description_localized,omitempty"`
} }
@ -422,16 +430,18 @@ type CommandOptionValue interface {
// StringOption is a subcommand option that fits into a CommandOptionValue. // StringOption is a subcommand option that fits into a CommandOptionValue.
type StringOption struct { type StringOption struct {
OptionName string `json:"name"` OptionName string `json:"name"`
OptionNameLocalizations StringLocales `json:"name_localizations,omitempty"`
Description string `json:"description"` Description string `json:"description"`
NameLocalizations CommandLocales `json:"name_localizations,omitempty"` DescriptionLocalizations StringLocales `json:"description_localizations,omitempty"`
DescriptionLocalizations CommandLocales `json:"description_localizations,omitempty"`
Required bool `json:"required"` Required bool `json:"required"`
Choices []StringChoice `json:"choices,omitempty"` Choices []StringChoice `json:"choices,omitempty"`
// Autocomplete must not be true if Choices are present. // Autocomplete must not be true if Choices are present.
Autocomplete bool `json:"autocomplete"` Autocomplete bool `json:"autocomplete"`
// The fields LocalizedName, LocalizedDescription are // LocalizedOptionName is only populated when this is received from
// populated only when data is received. // Discord's API.
LocalizedName string `json:"name_localized,omitempty"` LocalizedOptionName string `json:"name_localized,omitempty"`
// LocalizedDescription is only populated when this is received from
// Discord's API.
LocalizedDescription string `json:"description_localized,omitempty"` LocalizedDescription string `json:"description_localized,omitempty"`
} }
@ -444,28 +454,30 @@ func (s *StringOption) _val() {}
// StringChoice is a pair of string key to a string. // StringChoice is a pair of string key to a string.
type StringChoice struct { type StringChoice struct {
Name string `json:"name"` Name string `json:"name"`
NameLocalizations CommandLocales `json:"name_localizations,omitempty"` NameLocalizations StringLocales `json:"name_localizations,omitempty"`
Value string `json:"value"` Value string `json:"value"`
// The LocalizedName field is populated only when data is received. // LocalizedName is only populated when this is received from Discord's API.
LocalizedName string `json:"name_localized,omitempty"` LocalizedName string `json:"name_localized,omitempty"`
} }
// IntegerOption is a subcommand option that fits into a CommandOptionValue. // IntegerOption is a subcommand option that fits into a CommandOptionValue.
type IntegerOption struct { type IntegerOption struct {
OptionName string `json:"name"` OptionName string `json:"name"`
OptionNameLocalizations StringLocales `json:"name_localizations,omitempty"`
Description string `json:"description"` Description string `json:"description"`
NameLocalizations CommandLocales `json:"name_localizations,omitempty"` DescriptionLocalizations StringLocales `json:"description_localizations,omitempty"`
DescriptionLocalizations CommandLocales `json:"description_localizations,omitempty"`
Required bool `json:"required"` Required bool `json:"required"`
Min option.Int `json:"min_value,omitempty"` Min option.Int `json:"min_value,omitempty"`
Max option.Int `json:"max_value,omitempty"` Max option.Int `json:"max_value,omitempty"`
Choices []IntegerChoice `json:"choices,omitempty"` Choices []IntegerChoice `json:"choices,omitempty"`
// Autocomplete must not be true if Choices are present. // Autocomplete must not be true if Choices are present.
Autocomplete bool `json:"autocomplete"` Autocomplete bool `json:"autocomplete"`
// The fields LocalizedName, LocalizedDescription are // LocalizedOptionName is only populated when this is received from
// populated only when data is received. // Discord's API.
LocalizedName string `json:"name_localized,omitempty"` LocalizedOptionName string `json:"name_localized,omitempty"`
// LocalizedDescription is only populated when this is received from
// Discord's API.
LocalizedDescription string `json:"description_localized,omitempty"` LocalizedDescription string `json:"description_localized,omitempty"`
} }
@ -478,23 +490,25 @@ func (i *IntegerOption) _val() {}
// IntegerChoice is a pair of string key to an integer. // IntegerChoice is a pair of string key to an integer.
type IntegerChoice struct { type IntegerChoice struct {
Name string `json:"name"` Name string `json:"name"`
NameLocalizations CommandLocales `json:"name_localizations,omitempty"` NameLocalizations StringLocales `json:"name_localizations,omitempty"`
Value int `json:"value"` Value int `json:"value"`
// The LocalizedName field is populated only when data is received. // LocalizedName is only populated when this is received from Discord's API.
LocalizedName string `json:"name_localized,omitempty"` LocalizedName string `json:"name_localized,omitempty"`
} }
// BooleanOption is a subcommand option that fits into a CommandOptionValue. // BooleanOption is a subcommand option that fits into a CommandOptionValue.
type BooleanOption struct { type BooleanOption struct {
OptionName string `json:"name"` OptionName string `json:"name"`
Description string `json:"description"` OptionNameLocalizations StringLocales `json:"name_localizations,omitempty"`
NameLocalizations CommandLocales `json:"name_localizations,omitempty"` Description string `json:"description"`
DescriptionLocalizations CommandLocales `json:"description_localizations,omitempty"` DescriptionLocalizations StringLocales `json:"description_localizations,omitempty"`
Required bool `json:"required"` Required bool `json:"required"`
// The fields LocalizedName, LocalizedDescription are // LocalizedOptionName is only populated when this is received from
// populated only when data is received. // Discord's API.
LocalizedName string `json:"name_localized,omitempty"` LocalizedOptionName string `json:"name_localized,omitempty"`
// LocalizedDescription is only populated when this is received from
// Discord's API.
LocalizedDescription string `json:"description_localized,omitempty"` LocalizedDescription string `json:"description_localized,omitempty"`
} }
@ -507,14 +521,16 @@ func (b *BooleanOption) _val() {}
// UserOption is a subcommand option that fits into a CommandOptionValue. // UserOption is a subcommand option that fits into a CommandOptionValue.
type UserOption struct { type UserOption struct {
OptionName string `json:"name"` OptionName string `json:"name"`
Description string `json:"description"` OptionNameLocalizations StringLocales `json:"name_localizations,omitempty"`
NameLocalizations CommandLocales `json:"name_localizations,omitempty"` Description string `json:"description"`
DescriptionLocalizations CommandLocales `json:"description_localizations,omitempty"` DescriptionLocalizations StringLocales `json:"description_localizations,omitempty"`
Required bool `json:"required"` Required bool `json:"required"`
// The fields LocalizedName, LocalizedDescription are // LocalizedOptionName is only populated when this is received from
// populated only when data is received. // Discord's API.
LocalizedName string `json:"name_localized,omitempty"` LocalizedOptionName string `json:"name_localized,omitempty"`
// LocalizedDescription is only populated when this is received from
// Discord's API.
LocalizedDescription string `json:"description_localized,omitempty"` LocalizedDescription string `json:"description_localized,omitempty"`
} }
@ -527,15 +543,17 @@ func (u *UserOption) _val() {}
// ChannelOption is a subcommand option that fits into a CommandOptionValue. // ChannelOption is a subcommand option that fits into a CommandOptionValue.
type ChannelOption struct { type ChannelOption struct {
OptionName string `json:"name"` OptionName string `json:"name"`
Description string `json:"description"` OptionNameLocalizations StringLocales `json:"name_localizations,omitempty"`
NameLocalizations CommandLocales `json:"name_localizations,omitempty"` Description string `json:"description"`
DescriptionLocalizations CommandLocales `json:"description_localizations,omitempty"` DescriptionLocalizations StringLocales `json:"description_localizations,omitempty"`
Required bool `json:"required"` Required bool `json:"required"`
ChannelTypes []ChannelType `json:"channel_types,omitempty"` ChannelTypes []ChannelType `json:"channel_types,omitempty"`
// The fields LocalizedName, LocalizedDescription are // LocalizedOptionName is only populated when this is received from
// populated only when data is received. // Discord's API.
LocalizedName string `json:"name_localized,omitempty"` LocalizedOptionName string `json:"name_localized,omitempty"`
// LocalizedDescription is only populated when this is received from
// Discord's API.
LocalizedDescription string `json:"description_localized,omitempty"` LocalizedDescription string `json:"description_localized,omitempty"`
} }
@ -548,14 +566,16 @@ func (c *ChannelOption) _val() {}
// RoleOption is a subcommand option that fits into a CommandOptionValue. // RoleOption is a subcommand option that fits into a CommandOptionValue.
type RoleOption struct { type RoleOption struct {
OptionName string `json:"name"` OptionName string `json:"name"`
Description string `json:"description"` OptionNameLocalizations StringLocales `json:"name_localizations,omitempty"`
NameLocalizations CommandLocales `json:"name_localizations,omitempty"` Description string `json:"description"`
DescriptionLocalizations CommandLocales `json:"description_localizations,omitempty"` DescriptionLocalizations StringLocales `json:"description_localizations,omitempty"`
Required bool `json:"required"` Required bool `json:"required"`
// The fields LocalizedName, LocalizedDescription are // LocalizedOptionName is only populated when this is received from
// populated only when data is received. // Discord's API.
LocalizedName string `json:"name_localized,omitempty"` LocalizedOptionName string `json:"name_localized,omitempty"`
// LocalizedDescription is only populated when this is received from
// Discord's API.
LocalizedDescription string `json:"description_localized,omitempty"` LocalizedDescription string `json:"description_localized,omitempty"`
} }
@ -568,14 +588,16 @@ func (r *RoleOption) _val() {}
// MentionableOption is a subcommand option that fits into a CommandOptionValue. // MentionableOption is a subcommand option that fits into a CommandOptionValue.
type MentionableOption struct { type MentionableOption struct {
OptionName string `json:"name"` OptionName string `json:"name"`
Description string `json:"description"` OptionNameLocalizations StringLocales `json:"name_localizations,omitempty"`
NameLocalizations CommandLocales `json:"name_localizations,omitempty"` Description string `json:"description"`
DescriptionLocalizations CommandLocales `json:"description_localizations,omitempty"` DescriptionLocalizations StringLocales `json:"description_localizations,omitempty"`
Required bool `json:"required"` Required bool `json:"required"`
// The fields LocalizedName, LocalizedDescription are // LocalizedOptionName is only populated when this is received from
// populated only when data is received. // Discord's API.
LocalizedName string `json:"name_localized,omitempty"` LocalizedOptionName string `json:"name_localized,omitempty"`
// LocalizedDescription is only populated when this is received from
// Discord's API.
LocalizedDescription string `json:"description_localized,omitempty"` LocalizedDescription string `json:"description_localized,omitempty"`
} }
@ -589,18 +611,20 @@ func (m *MentionableOption) _val() {}
// NumberOption is a subcommand option that fits into a CommandOptionValue. // NumberOption is a subcommand option that fits into a CommandOptionValue.
type NumberOption struct { type NumberOption struct {
OptionName string `json:"name"` OptionName string `json:"name"`
OptionNameLocalizations StringLocales `json:"name_localizations,omitempty"`
Description string `json:"description"` Description string `json:"description"`
NameLocalizations CommandLocales `json:"name_localizations,omitempty"` DescriptionLocalizations StringLocales `json:"description_localizations,omitempty"`
DescriptionLocalizations CommandLocales `json:"description_localizations,omitempty"`
Required bool `json:"required"` Required bool `json:"required"`
Min option.Float `json:"min_value,omitempty"` Min option.Float `json:"min_value,omitempty"`
Max option.Float `json:"max_value,omitempty"` Max option.Float `json:"max_value,omitempty"`
Choices []NumberChoice `json:"choices,omitempty"` Choices []NumberChoice `json:"choices,omitempty"`
// Autocomplete must not be true if Choices are present. // Autocomplete must not be true if Choices are present.
Autocomplete bool `json:"autocomplete"` Autocomplete bool `json:"autocomplete"`
// The fields LocalizedName, LocalizedDescription are // LocalizedOptionName is only populated when this is received from
// populated only when data is received. // Discord's API.
LocalizedName string `json:"name_localized,omitempty"` LocalizedOptionName string `json:"name_localized,omitempty"`
// LocalizedDescription is only populated when this is received from
// Discord's API.
LocalizedDescription string `json:"description_localized,omitempty"` LocalizedDescription string `json:"description_localized,omitempty"`
} }
@ -613,10 +637,10 @@ func (n *NumberOption) _val() {}
// NumberChoice is a pair of string key to a float64 values. // NumberChoice is a pair of string key to a float64 values.
type NumberChoice struct { type NumberChoice struct {
Name string `json:"name"` Name string `json:"name"`
NameLocalizations CommandLocales `json:"name_localizations,omitempty"` NameLocalizations StringLocales `json:"name_localizations,omitempty"`
Value float64 `json:"value"` Value float64 `json:"value"`
// The LocalizedName field is populated only when data is received. // LocalizedName is only populated when this is received from Discord's API.
LocalizedName string `json:"name_localized,omitempty"` LocalizedName string `json:"name_localized,omitempty"`
} }