Skip to content

Commit

Permalink
Merge pull request #212 from shardlab/revert-190-localization
Browse files Browse the repository at this point in the history
Revert "Add Localization Support"
  • Loading branch information
PixeLInc authored Mar 27, 2023
2 parents 2a2d660 + 7294294 commit 2011ac6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 97 deletions.
16 changes: 8 additions & 8 deletions lib/discordrb/api/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, name_localizations = {}, description_localizations = {})
def create_global_command(token, application_id, name, description, options = [], default_permission = nil, type = 1)
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, description_localizations: description_localizations, name_localizations: name_localizations }.to_json,
{ name: name, description: description, options: options, default_permission: default_permission, type: type }.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, name_localizations = {}, description_localizations = {})
def edit_global_command(token, application_id, command_id, name = nil, description = nil, options = nil, default_permission = nil, type = 1)
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, description_localizations: description_localizations, name_localizations: name_localizations }.compact.to_json,
{ name: name, description: description, options: options, default_permission: default_permission, type: type }.compact.to_json,
Authorization: token,
content_type: :json
)
Expand Down Expand Up @@ -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, name_localizations = {}, description_localizations = {})
def create_guild_command(token, application_id, guild_id, name, description, options = nil, default_permission = nil, type = 1)
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, description_localizations: description_localizations, name_localizations: name_localizations }.to_json,
{ name: name, description: description, options: options, default_permission: default_permission, type: type }.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, name_localizations = {}, description_localizations = {})
def edit_guild_command(token, application_id, guild_id, command_id, name = nil, description = nil, options = nil, default_permission = nil, type = 1)
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, description_localizations: description_localizations, name_localizations: name_localizations }.compact.to_json,
{ name: name, description: description, options: options, default_permission: default_permission, type: type }.compact.to_json,
Authorization: token,
content_type: :json
)
Expand Down
16 changes: 7 additions & 9 deletions lib/discordrb/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -828,14 +828,12 @@ def register_application_command(name, description, server_id: nil, default_perm

builder = Interactions::OptionBuilder.new
permission_builder = Interactions::PermissionBuilder.new
localization_builder = Interactions::LocalizationBuilder.new

yield(builder, permission_builder, localization_builder) if block_given?
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, localization_builder.to_a[0], localization_builder.to_a[1])
API::Application.create_guild_command(@token, profile.id, server_id, name, description, builder.to_a, default_permission, type)
else
API::Application.create_global_command(@token, profile.id, name, description, builder.to_a, default_permission, type, localization_builder.to_a[0], localization_builder.to_a[1])
API::Application.create_global_command(@token, profile.id, name, description, builder.to_a, default_permission, type)
end
cmd = ApplicationCommand.new(JSON.parse(resp), self, server_id)

Expand All @@ -852,16 +850,16 @@ def register_application_command(name, description, server_id: nil, default_perm
# @yieldparam [PermissionBuilder]
def edit_application_command(command_id, server_id: nil, name: nil, description: nil, default_permission: nil, type: :chat_input)
type = ApplicationCommand::TYPES[type] || type

builder = Interactions::OptionBuilder.new
permission_builder = Interactions::PermissionBuilder.new
localization_builder = Interactions::LocalizationBuilder.new

yield(builder, permission_builder, name_localization_builder, description_localization_builder) if block_given?
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, localization_builder.to_a[0], localization_builder.to_a[1])
API::Application.edit_guild_command(@token, profile.id, server_id, command_id, name, description, builder.to_a, default_permission, type)
else
API::Application.edit_guild_command(@token, profile.id, command_id, name, description, builder.to_a, default_permission.type, localization_builder.to_a[0], localization_builder.to_a[1])
API::Application.edit_guild_command(@token, profile.id, command_id, name, description, builder.to_a, default_permission.type)
end
cmd = ApplicationCommand.new(JSON.parse(resp), self, server_id)

Expand Down
95 changes: 15 additions & 80 deletions lib/discordrb/data/interaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ class Interaction
# @return [Array<ActionRow>]
attr_reader :components

# @return [String] The selected locale of the invoking user
attr_reader :locale
alias_method :language, :locale

# @return [String, nil] The preferred locale of the server the interaction was invoked in
attr_reader :guild_locale
alias_method :server_locale, :guild_locale
alias_method :server_language, :guild_locale
alias_method :guild_language, :guild_locale

# @!visibility private
def initialize(data, bot)
@bot = bot
Expand All @@ -87,8 +77,6 @@ def initialize(data, bot)
@token = data['token']
@version = data['version']
@components = @data['components']&.map { |component| Components.from_data(component, @bot) }&.compact || []
@guild_locale = data['guild_locale']
@locale = data['locale']
end

# Respond to the creation of this interaction. An interaction must be responded to or deferred,
Expand Down Expand Up @@ -450,10 +438,9 @@ def initialize
# end
def subcommand(name, description)
builder = OptionBuilder.new
localization_builder = LocalizationBuilder.new
yield(builder, localization_builder) if block_given?
yield builder if block_given?

option(TYPES[:subcommand], name, description, options: builder.to_a, name_localizations: localization_builder.to_a[0], description_localizations: localization_builder.to_a[1])
option(TYPES[:subcommand], name, description, options: builder.to_a)
end

# @param name [String, Symbol] The name of the subcommand group.
Expand All @@ -470,10 +457,9 @@ def subcommand(name, description)
# end
def subcommand_group(name, description)
builder = OptionBuilder.new
localization_builder = LocalizationBuilder.new
yield(builder, localization_builder) if block_given?
yield builder

option(TYPES[:subcommand_group], name, description, options: builder.to_a, name_localizations: localization_builder.to_a[0], description_localizations: localization_builder.to_a[1])
option(TYPES[:subcommand_group], name, description, options: builder.to_a)
end

# @param name [String, Symbol] The name of the argument.
Expand All @@ -482,10 +468,7 @@ def subcommand_group(name, description)
# @param choices [Hash, nil] Available choices, mapped as `Name => Value`.
# @return (see #option)
def string(name, description, required: nil, choices: nil)
localization_builder = LocalizationBuilder.new
yield(localization_builder) if block_given?

option(TYPES[:string], name, description, required: required, choices: choices, name_localizations: localization_builder.to_a[0], description_localizations: localization_builder.to_a[1])
option(TYPES[:string], name, description, required: required, choices: choices)
end

# @param name [String, Symbol] The name of the argument.
Expand All @@ -494,32 +477,23 @@ def string(name, description, required: nil, choices: nil)
# @param choices [Hash, nil] Available choices, mapped as `Name => Value`.
# @return (see #option)
def integer(name, description, required: nil, choices: nil)
localization_builder = LocalizationBuilder.new
yield(localization_builder) if block_given?

option(TYPES[:integer], name, description, required: required, choices: choices, name_localizations: localization_builder.to_a[0], description_localizations: localization_builder.to_a[1])
option(TYPES[:integer], name, description, required: required, choices: choices)
end

# @param name [String, Symbol] The name of the argument.
# @param description [String] A description of the argument.
# @param required [true, false] Whether this option must be provided.
# @return (see #option)
def boolean(name, description, required: nil)
localization_builder = LocalizationBuilder.new
yield(localization_builder) if block_given?

option(TYPES[:boolean], name, description, required: required, name_localizations: localization_builder.to_a[0], description_localizations: localization_builder.to_a[1])
option(TYPES[:boolean], name, description, required: required)
end

# @param name [String, Symbol] The name of the argument.
# @param description [String] A description of the argument.
# @param required [true, false] Whether this option must be provided.
# @return (see #option)
def user(name, description, required: nil)
localization_builder = LocalizationBuilder.new
yield(localization_builder) if block_given?

option(TYPES[:user], name, description, required: required, name_localizations: localization_builder.to_a[0], description_localizations: description_localization_builder.to_a[1])
option(TYPES[:user], name, description, required: required)
end

# @param name [String, Symbol] The name of the argument.
Expand All @@ -529,43 +503,32 @@ def user(name, description, required: nil)
# @return (see #option)
def channel(name, description, required: nil, types: nil)
types = types&.collect { |type| type.is_a?(Numeric) ? type : CHANNEL_TYPES[type] }
localization_builder = LocalizationBuilder.new
yield(localization_builder) if block_given?

option(TYPES[:channel], name, description, required: required, channel_types: types, name_localizations: localization_builder.to_a[0], description_localizations: localization_builder.to_a[1])
option(TYPES[:channel], name, description, required: required, channel_types: types)
end

# @param name [String, Symbol] The name of the argument.
# @param description [String] A description of the argument.
# @param required [true, false] Whether this option must be provided.
# @return (see #option)
def role(name, description, required: nil)
localization_builder = LocalizationBuilder.new
yield(localization_builder) if block_given?

option(TYPES[:user], name, description, required: required, name_localizations: localization_builder.to_a[0], description_localizations: description_localization_builder.to_a[1])
option(TYPES[:role], name, description, required: required)
end

# @param name [String, Symbol] The name of the argument.
# @param description [String] A description of the argument.
# @param required [true, false] Whether this option must be provided.
# @return (see #option)
def mentionable(name, description, required: nil)
localization_builder = LocalizationBuilder.new
yield(localization_builder) if block_given?

option(TYPES[:user], name, description, required: required, name_localizations: localization_builder.to_a[0], description_localizations: description_localization_builder.to_a[1])
option(TYPES[:mentionable], name, description, required: required)
end

# @param name [String, Symbol] The name of the argument.
# @param description [String] A description of the argument.
# @param required [true, false] Whether this option must be provided.
# @return (see #option)
def number(name, description, required: nil, min_value: nil, max_value: nil, choices: nil)
localization_builder = LocalizationBuilder.new
yield(localization_builder) if block_given?

option(TYPES[:user], name, description, required: required, name_localizations: localization_builder.to_a[0], description_localizations: description_localization_builder.to_a[1])
option(TYPES[:number], name, description,
required: required, min_value: min_value, max_value: max_value, choices: choices)
end

# @!visibility private
Expand All @@ -576,16 +539,14 @@ def number(name, description, required: nil, min_value: nil, max_value: nil, cho
# @param min_value [Integer, Float] A minimum value for integer and number options.
# @param max_value [Integer, Float] A maximum value for integer and number options.
# @param channel_types [Array<Integer>] Channel types that can be provides for channel options.
# @param name_localizations [Hash] Name localizations for this option
# @param description_localizations [Hash] Description localizations for this option
# @return Hash
def option(type, name, description, required: nil, choices: nil, options: nil, min_value: nil, max_value: nil,
channel_types: nil, name_localizations: nil, description_localizations: nil)
channel_types: nil)
opt = { type: type, name: name, description: description }
choices = choices.map { |option_name, value| { name: option_name, value: value } } if choices

opt.merge!({ required: required, choices: choices, options: options, min_value: min_value,
max_value: max_value, channel_types: channel_types, name_localizations: name_localizations, description_localizations: description_localizations }.compact)
max_value: max_value, channel_types: channel_types }.compact)

@options << opt
opt
Expand All @@ -597,32 +558,6 @@ def to_a
end
end

# Builder for setting localizations
class LocalizationBuilder
# @!visibility private
def initialize
@description_localizations = {}
@name_localizations = {}
end

# @param locale [String] The locale to create a localization for
# @param name [String] The localization name
# @param description [String] The localization description
# @return [Array<Hash>]
def make_localization(locale, name: '', description: '')
raise ArgumentError 'You must give at least one field to add a localization to.' unless name || description

@description_localizations[locale] = description
@name_localizations[locale] = name
[@name_localizations[locale], @description_localizations[locale]]
end

# @return [Hash]
def to_a
[@name_localizations, @description_localizations]
end
end

# Builder for creating server application command permissions.
# @deprecated This system is being replaced in the near future.
class PermissionBuilder
Expand Down

0 comments on commit 2011ac6

Please sign in to comment.