Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add application command localization support. #245

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions examples/slash_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#
# You may want to have a separate script for registering your commands so you don't need to do this every
# time you start your bot.
bot.register_application_command(:example, 'Example commands', server_id: ENV.fetch('SLASH_COMMAND_BOT_SERVER_ID', nil)) do |cmd|
bot.register_application_command(:example, 'Example commands', server_id: ENV.fetch('SLASH_COMMAND_BOT_SERVER_ID', nil), name_localizations: { 'de' => 'beispiel' }, description_localizations: { 'de' => 'Beispielbefehle' }) do |cmd|
cmd.subcommand_group(:fun, 'Fun things!') do |group|
group.subcommand('8ball', 'Shake the magic 8 ball') do |sub|
sub.string('question', 'Ask a question to receive wisdom', required: true)
sub.string('question', 'Ask a question to receive wisdom', required: true, name_localizations: { 'de' => 'frage' }, description_localizations: { 'de' => 'Stell eine Frage um Weisheit zu erlangen' })
end

group.subcommand('java', 'What if it was java?')
Expand Down
24 changes: 12 additions & 12 deletions lib/discordrb/api/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ module Discordrb::API::Application

# Get a list of global application commands.
# https://discord.com/developers/docs/interactions/slash-commands#get-global-application-commands
def get_global_commands(token, application_id)
def get_global_commands(token, application_id, with_localizations: nil)
Discordrb::API.request(
:applications_aid_commands,
nil,
:get,
"#{Discordrb::API.api_base}/applications/#{application_id}/commands",
"#{Discordrb::API.api_base}/applications/#{application_id}/commands#{"?with_localizations=#{!!with_localizations}" unless with_localizations.nil?}",
Authorization: token
)
end
Expand All @@ -30,27 +30,27 @@ def get_global_command(token, application_id, command_id)

# Create a global application command.
# https://discord.com/developers/docs/interactions/slash-commands#create-global-application-command
def create_global_command(token, application_id, name, description, options = [], default_permission = nil, type = 1, default_member_permissions = nil, contexts = nil)
def create_global_command(token, application_id, name, description, options = [], default_permission = nil, type = 1, default_member_permissions = nil, contexts = nil, name_localizations = {}, description_localizations = {})
Discordrb::API.request(
:applications_aid_commands,
nil,
:post,
"#{Discordrb::API.api_base}/applications/#{application_id}/commands",
{ name: name, description: description, options: options, default_permission: default_permission, type: type, default_member_permissions: default_member_permissions, contexts: contexts }.to_json,
{ name: name, description: description, options: options, default_permission: default_permission, type: type, default_member_permissions: default_member_permissions, contexts: contexts, name_localizations: name_localizations, description_localizations: description_localizations }.to_json,
Authorization: token,
content_type: :json
)
end

# Edit a global application command.
# https://discord.com/developers/docs/interactions/slash-commands#edit-global-application-command
def edit_global_command(token, application_id, command_id, name = nil, description = nil, options = nil, default_permission = nil, type = 1, default_member_permissions = nil, contexts = nil)
def edit_global_command(token, application_id, command_id, name = nil, description = nil, options = nil, default_permission = nil, type = 1, default_member_permissions = nil, contexts = nil, name_localizations = {}, description_localizations = {})
Discordrb::API.request(
:applications_aid_commands_cid,
nil,
:patch,
"#{Discordrb::API.api_base}/applications/#{application_id}/commands/#{command_id}",
{ name: name, description: description, options: options, default_permission: default_permission, type: type, default_member_permissions: default_member_permissions, contexts: contexts }.compact.to_json,
{ name: name, description: description, options: options, default_permission: default_permission, type: type, default_member_permissions: default_member_permissions, contexts: contexts, name_localizations: name_localizations, description_localizations: description_localizations }.compact.to_json,
Authorization: token,
content_type: :json
)
Expand Down Expand Up @@ -84,12 +84,12 @@ def bulk_overwrite_global_commands(token, application_id, commands)

# Get a guild's commands for an application.
# https://discord.com/developers/docs/interactions/slash-commands#get-guild-application-commands
def get_guild_commands(token, application_id, guild_id)
def get_guild_commands(token, application_id, guild_id, with_localizations: nil)
Discordrb::API.request(
:applications_aid_guilds_gid_commands,
guild_id,
:get,
"#{Discordrb::API.api_base}/applications/#{application_id}/guilds/#{guild_id}/commands",
"#{Discordrb::API.api_base}/applications/#{application_id}/guilds/#{guild_id}/commands#{"?with_localizations=#{!!with_localizations}" unless with_localizations.nil?}",
Authorization: token
)
end
Expand All @@ -108,27 +108,27 @@ def get_guild_command(token, application_id, guild_id, command_id)

# Create an application command for a guild.
# https://discord.com/developers/docs/interactions/slash-commands#create-guild-application-command
def create_guild_command(token, application_id, guild_id, name, description, options = nil, default_permission = nil, type = 1, default_member_permissions = nil, contexts = nil)
def create_guild_command(token, application_id, guild_id, name, description, options = nil, default_permission = nil, type = 1, default_member_permissions = nil, contexts = nil, name_localizations = {}, description_localizations = {})
Discordrb::API.request(
:applications_aid_guilds_gid_commands,
guild_id,
:post,
"#{Discordrb::API.api_base}/applications/#{application_id}/guilds/#{guild_id}/commands",
{ name: name, description: description, options: options, default_permission: default_permission, type: type, default_member_permissions: default_member_permissions, contexts: contexts }.to_json,
{ name: name, description: description, options: options, default_permission: default_permission, type: type, default_member_permissions: default_member_permissions, contexts: contexts, name_localizations: name_localizations, description_localizations: description_localizations }.to_json,
Authorization: token,
content_type: :json
)
end

# Edit an application command for a guild.
# https://discord.com/developers/docs/interactions/slash-commands#edit-guild-application-command
def edit_guild_command(token, application_id, guild_id, command_id, name = nil, description = nil, options = nil, default_permission = nil, type = 1, default_member_permissions = nil, contexts = nil)
def edit_guild_command(token, application_id, guild_id, command_id, name = nil, description = nil, options = nil, default_permission = nil, type = 1, default_member_permissions = nil, contexts = nil, name_localizations = {}, description_localizations = {})
Discordrb::API.request(
:applications_aid_guilds_gid_commands_cid,
guild_id,
:patch,
"#{Discordrb::API.api_base}/applications/#{application_id}/guilds/#{guild_id}/commands/#{command_id}",
{ name: name, description: description, options: options, default_permission: default_permission, type: type, default_member_permissions: default_member_permissions, contexts: contexts }.compact.to_json,
{ name: name, description: description, options: options, default_permission: default_permission, type: type, default_member_permissions: default_member_permissions, contexts: contexts, name_localizations: name_localizations, description_localizations: description_localizations }.compact.to_json,
Authorization: token,
content_type: :json
)
Expand Down
19 changes: 10 additions & 9 deletions lib/discordrb/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -792,12 +792,13 @@ def prune_empty_groups

# Get all application commands.
# @param server_id [String, Integer, nil] The ID of the server to get the commands from. Global if `nil`.
# @param with_localizations [true, false, nil] Whether the full localizations of commands should be included in the result. `nil` requests the Discord API default.
# @return [Array<ApplicationCommand>]
def get_application_commands(server_id: nil)
def get_application_commands(server_id: nil, with_localizations: nil)
resp = if server_id
API::Application.get_guild_commands(@token, profile.id, server_id)
API::Application.get_guild_commands(@token, profile.id, server_id, with_localizations: with_localizations)
else
API::Application.get_global_commands(@token, profile.id)
API::Application.get_global_commands(@token, profile.id, with_localizations: with_localizations)
end

JSON.parse(resp).map do |command_data|
Expand Down Expand Up @@ -831,17 +832,17 @@ def get_application_command(command_id, server_id: nil)
# end
# end
# end
def register_application_command(name, description, server_id: nil, default_permission: nil, type: :chat_input, default_member_permissions: nil, contexts: nil)
def register_application_command(name, description, server_id: nil, default_permission: nil, type: :chat_input, default_member_permissions: nil, contexts: nil, name_localizations: nil, description_localizations: nil)
type = ApplicationCommand::TYPES[type] || type

builder = Interactions::OptionBuilder.new
permission_builder = Interactions::PermissionBuilder.new
yield(builder, permission_builder) if block_given?

resp = if server_id
API::Application.create_guild_command(@token, profile.id, server_id, name, description, builder.to_a, default_permission, type, default_member_permissions, contexts)
API::Application.create_guild_command(@token, profile.id, server_id, name, description, builder.to_a, default_permission, type, default_member_permissions, contexts, name_localizations, description_localizations)
else
API::Application.create_global_command(@token, profile.id, name, description, builder.to_a, default_permission, type, default_member_permissions, contexts)
API::Application.create_global_command(@token, profile.id, name, description, builder.to_a, default_permission, type, default_member_permissions, contexts, name_localizations, description_localizations)
end
cmd = ApplicationCommand.new(JSON.parse(resp), self, server_id)

Expand All @@ -856,7 +857,7 @@ def register_application_command(name, description, server_id: nil, default_perm

# @yieldparam [OptionBuilder]
# @yieldparam [PermissionBuilder]
def edit_application_command(command_id, server_id: nil, name: nil, description: nil, default_permission: nil, type: :chat_input, default_member_permissions: nil, contexts: nil)
def edit_application_command(command_id, server_id: nil, name: nil, description: nil, default_permission: nil, type: :chat_input, default_member_permissions: nil, contexts: nil, name_localizations: nil, description_localizations: nil)
type = ApplicationCommand::TYPES[type] || type

builder = Interactions::OptionBuilder.new
Expand All @@ -865,9 +866,9 @@ def edit_application_command(command_id, server_id: nil, name: nil, description:
yield(builder, permission_builder) if block_given?

resp = if server_id
API::Application.edit_guild_command(@token, profile.id, server_id, command_id, name, description, builder.to_a, default_permission, type, default_member_permissions, contexts)
API::Application.edit_guild_command(@token, profile.id, server_id, command_id, name, description, builder.to_a, default_permission, type, default_member_permissions, contexts, name_localizations, description_localizations)
else
API::Application.edit_global_command(@token, profile.id, command_id, name, description, builder.to_a, default_permission, type, default_member_permissions, contexts)
API::Application.edit_global_command(@token, profile.id, command_id, name, description, builder.to_a, default_permission, type, default_member_permissions, contexts, name_localizations, description_localizations)
end
cmd = ApplicationCommand.new(JSON.parse(resp), self, server_id)

Expand Down
Loading