Skip to content

Commit

Permalink
Application commands localization (#1143)
Browse files Browse the repository at this point in the history
* feat(interactions): application commands localization

* feat(interactions): add missing omitempty to localization fields

Co-authored-by: ToπSenpai <15636011+TopiSenpai@users.noreply.github.com>

Co-authored-by: ToπSenpai <15636011+TopiSenpai@users.noreply.github.com>
  • Loading branch information
FedorLap2006 and topi314 committed Mar 31, 2022
1 parent cd724aa commit 9356a84
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
57 changes: 57 additions & 0 deletions examples/slash_commands/main.go
Expand Up @@ -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",
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 12 additions & 7 deletions interactions.go
Expand Up @@ -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.
Expand Down Expand Up @@ -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,omitempty"`
Description string `json:"description,omitempty"`
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"`
Expand All @@ -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,omitempty"`
Value interface{} `json:"value"`
}

// ApplicationCommandPermissions represents a single user or role permission for a command.
Expand Down

0 comments on commit 9356a84

Please sign in to comment.