From e3c8d27fef82f4869ce80d982090175744da721d Mon Sep 17 00:00:00 2001 From: nitroflap Date: Sat, 26 Mar 2022 22:23:58 +0300 Subject: [PATCH 1/2] feat(interactions): application commands localization --- examples/slash_commands/main.go | 57 +++++++++++++++++++++++++++++++++ interactions.go | 19 +++++++---- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/examples/slash_commands/main.go b/examples/slash_commands/main.go index 5ac892635..ad8af303e 100644 --- a/examples/slash_commands/main.go +++ b/examples/slash_commands/main.go @@ -46,6 +46,45 @@ var ( Name: "basic-command-with-files", Description: "Basic command with files", }, + { + Name: "localized-command", + Description: "Localized command. Description and name may vary depending on the Language setting", + NameLocalizations: &map[discordgo.Locale]string{ + discordgo.ChineseCN: "本地化的命令", + }, + DescriptionLocalizations: &map[discordgo.Locale]string{ + discordgo.ChineseCN: "这是一个本地化的命令", + }, + Options: []*discordgo.ApplicationCommandOption{ + { + Name: "localized-option", + Description: "Localized option. Description and name may vary depending on the Language setting", + NameLocalizations: map[discordgo.Locale]string{ + discordgo.ChineseCN: "一个本地化的选项", + }, + DescriptionLocalizations: map[discordgo.Locale]string{ + discordgo.ChineseCN: "这是一个本地化的选项", + }, + Type: discordgo.ApplicationCommandOptionInteger, + Choices: []*discordgo.ApplicationCommandOptionChoice{ + { + Name: "First", + NameLocalizations: map[discordgo.Locale]string{ + discordgo.ChineseCN: "一的", + }, + Value: 1, + }, + { + Name: "Second", + NameLocalizations: map[discordgo.Locale]string{ + discordgo.ChineseCN: "二的", + }, + Value: 2, + }, + }, + }, + }, + }, { Name: "options", Description: "Command for demonstrating options", @@ -196,6 +235,24 @@ var ( }, }) }, + "localized-command": func(s *discordgo.Session, i *discordgo.InteractionCreate) { + responses := map[discordgo.Locale]string{ + discordgo.ChineseCN: "你好! 这是一个本地化的命令", + } + response := "Hi! This is a localized message" + if r, ok := responses[i.Locale]; ok { + response = r + } + err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: response, + }, + }) + if err != nil { + panic(err) + } + }, "options": func(s *discordgo.Session, i *discordgo.InteractionCreate) { margs := []interface{}{ // Here we need to convert raw interface{} value to wanted type. diff --git a/interactions.go b/interactions.go index c65cb3f1e..8f7df3857 100644 --- a/interactions.go +++ b/interactions.go @@ -35,12 +35,14 @@ type ApplicationCommand struct { Version string `json:"version,omitempty"` Type ApplicationCommandType `json:"type,omitempty"` Name string `json:"name"` + NameLocalizations *map[Locale]string `json:"name_localizations,omitempty"` DefaultPermission *bool `json:"default_permission,omitempty"` // NOTE: Chat commands only. Otherwise it mustn't be set. - Description string `json:"description,omitempty"` - Options []*ApplicationCommandOption `json:"options"` + Description string `json:"description,omitempty"` + DescriptionLocalizations *map[Locale]string `json:"description_localizations,omitempty"` + Options []*ApplicationCommandOption `json:"options"` } // ApplicationCommandOptionType indicates the type of a slash command's option. @@ -91,9 +93,11 @@ func (t ApplicationCommandOptionType) String() string { // ApplicationCommandOption represents an option/subcommand/subcommands group. type ApplicationCommandOption struct { - Type ApplicationCommandOptionType `json:"type"` - Name string `json:"name"` - Description string `json:"description,omitempty"` + Type ApplicationCommandOptionType `json:"type"` + Name string `json:"name"` + NameLocalizations map[Locale]string `json:"name_localizations"` + Description string `json:"description,omitempty"` + DescriptionLocalizations map[Locale]string `json:"description_localizations"` // NOTE: This feature was on the API, but at some point developers decided to remove it. // So I commented it, until it will be officially on the docs. // Default bool `json:"default"` @@ -113,8 +117,9 @@ type ApplicationCommandOption struct { // ApplicationCommandOptionChoice represents a slash command option choice. type ApplicationCommandOptionChoice struct { - Name string `json:"name"` - Value interface{} `json:"value"` + Name string `json:"name"` + NameLocalizations map[Locale]string `json:"name_localizations"` + Value interface{} `json:"value"` } // ApplicationCommandPermissions represents a single user or role permission for a command. From 944756591ecd4b4c2322f875f27c3b5c67461408 Mon Sep 17 00:00:00 2001 From: Fedor Lapshin Date: Sun, 27 Mar 2022 00:09:53 +0300 Subject: [PATCH 2/2] feat(interactions): add missing omitempty to localization fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: ToπSenpai <15636011+TopiSenpai@users.noreply.github.com> --- interactions.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/interactions.go b/interactions.go index 8f7df3857..0551e476b 100644 --- a/interactions.go +++ b/interactions.go @@ -95,9 +95,9 @@ func (t ApplicationCommandOptionType) String() string { type ApplicationCommandOption struct { Type ApplicationCommandOptionType `json:"type"` Name string `json:"name"` - NameLocalizations map[Locale]string `json:"name_localizations"` + NameLocalizations map[Locale]string `json:"name_localizations,omitempty"` Description string `json:"description,omitempty"` - DescriptionLocalizations map[Locale]string `json:"description_localizations"` + DescriptionLocalizations map[Locale]string `json:"description_localizations,omitempty"` // NOTE: This feature was on the API, but at some point developers decided to remove it. // So I commented it, until it will be officially on the docs. // Default bool `json:"default"` @@ -118,7 +118,7 @@ type ApplicationCommandOption struct { // ApplicationCommandOptionChoice represents a slash command option choice. type ApplicationCommandOptionChoice struct { Name string `json:"name"` - NameLocalizations map[Locale]string `json:"name_localizations"` + NameLocalizations map[Locale]string `json:"name_localizations,omitempty"` Value interface{} `json:"value"` }