Module: OnyxCord::Bot::ApplicationCommands

Included in:
OnyxCord::Bot
Defined in:
lib/onyxcord/core/bot/application_commands.rb

Instance Method Summary collapse

Instance Method Details

#application_command_permissions(server_id:) ⇒ Array<ApplicationCommand::Permission>

Get the permissions for all of the application commands in a specific server.

Parameters:

  • server_id (Integer, String, nil)

    The ID of the server to fetch application command permissions for.

Returns:



168
169
170
171
# File 'lib/onyxcord/core/bot/application_commands.rb', line 168

def application_command_permissions(server_id:)
  response = REST::Application.get_guild_application_command_permissions(@token, profile.id, server_id.resolve_id)
  OnyxCord::Internal::JSON.parse(response).flat_map { |data| data['permissions'].map { |inner| ApplicationCommand::Permission.new(inner, data, self) } }
end

#application_command_registryObject



38
39
40
# File 'lib/onyxcord/core/bot/application_commands.rb', line 38

def application_command_registry
  @application_command_registry ||= ::OnyxCord::ApplicationCommands::Registry.new(self)
end

#application_emoji(emoji_id) ⇒ Emoji

Fetches a single application emoji from its ID.

Parameters:

Returns:

  • (Emoji)

    The application emoji.



183
184
185
186
# File 'lib/onyxcord/core/bot/application_commands.rb', line 183

def application_emoji(emoji_id)
  response = REST::Application.get_application_emoji(@token, profile.id, emoji_id.resolve_id)
  Emoji.new(OnyxCord::Internal::JSON.parse(response), self)
end

#application_emojisArray<Emoji>

Fetches all the application emojis that the bot can use.

Returns:

  • (Array<Emoji>)

    Returns an array of emoji objects.



175
176
177
178
# File 'lib/onyxcord/core/bot/application_commands.rb', line 175

def application_emojis
  response = REST::Application.list_application_emojis(@token, profile.id)
  OnyxCord::Internal::JSON.parse(response)['items'].map { |emoji| Emoji.new(emoji, self) }
end

#bulk_overwrite_global_application_commands(commands) ⇒ Object



58
59
60
61
# File 'lib/onyxcord/core/bot/application_commands.rb', line 58

def bulk_overwrite_global_application_commands(commands)
  response = REST::Application.bulk_overwrite_global_commands(@token, profile.id, commands)
  OnyxCord::Internal::JSON.parse(response).map { |data| ApplicationCommand.new(data, self) }
end

#bulk_overwrite_guild_application_commands(server_id, commands) ⇒ Object



63
64
65
66
# File 'lib/onyxcord/core/bot/application_commands.rb', line 63

def bulk_overwrite_guild_application_commands(server_id, commands)
  response = REST::Application.bulk_overwrite_guild_commands(@token, profile.id, server_id.resolve_id, commands)
  OnyxCord::Internal::JSON.parse(response).map { |data| ApplicationCommand.new(data, self, server_id) }
end

#commandsOnyxCord::Interactions::Registry



34
35
36
# File 'lib/onyxcord/core/bot/application_commands.rb', line 34

def commands
  @commands ||= application_command_registry
end

#create_application_emoji(name:, image:) ⇒ Emoji

Creates a new custom emoji that can be used by this application.

Parameters:

  • name (String)

    The name of emoji to create.

  • image (String, #read)

    Base64 string with the image data, or an object that responds to #read.

Returns:

  • (Emoji)

    The emoji that has been created.



192
193
194
195
196
# File 'lib/onyxcord/core/bot/application_commands.rb', line 192

def create_application_emoji(name:, image:)
  image = image.respond_to?(:read) ? OnyxCord.encode64(image) : image
  response = REST::Application.create_application_emoji(@token, profile.id, name, image)
  Emoji.new(OnyxCord::Internal::JSON.parse(response), self)
end

#delete_application_command(command_id, server_id: nil) ⇒ Object

Remove an application command from the commands registered with discord.

Parameters:

  • command_id (String, Integer)

    The ID of the command to remove.

  • server_id (String, Integer) (defaults to: nil)

    The ID of the server to delete this command from, global if nil.



142
143
144
145
146
147
148
# File 'lib/onyxcord/core/bot/application_commands.rb', line 142

def delete_application_command(command_id, server_id: nil)
  if server_id
    REST::Application.delete_guild_command(@token, profile.id, server_id, command_id)
  else
    REST::Application.delete_global_command(@token, profile.id, command_id)
  end
end

#delete_application_emoji(emoji_id) ⇒ Object

Deletes an existing application emoji.

Parameters:



209
210
211
# File 'lib/onyxcord/core/bot/application_commands.rb', line 209

def delete_application_emoji(emoji_id)
  REST::Application.delete_application_emoji(@token, profile.id, emoji_id.resolve_id)
end

#edit_application_command(command_id, server_id: nil, name: nil, description: nil, default_permission: nil, type: :chat_input, default_member_permissions: nil, contexts: nil, nsfw: nil, integration_types: nil) {|, | ... } ⇒ Object

Yield Parameters:

  • (OptionBuilder)
  • (PermissionBuilder)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/onyxcord/core/bot/application_commands.rb', line 111

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, nsfw: nil, integration_types: nil)
  type = ApplicationCommand::TYPES[type] || type

  contexts = contexts&.map { |context| Interaction::CONTEXTS[context] || context }
  integration_types = integration_types&.map { |type| Interaction::INTEGRATION_TYPES[type] || type }
  default_member_permissions = Permissions.bits(default_member_permissions) if default_member_permissions.is_a?(Array)

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

  yield(builder, permission_builder) if block_given?

  resp = if server_id
           REST::Application.edit_guild_command(@token, profile.id, server_id, command_id, name, description, builder.to_a, default_permission, type, default_member_permissions&.to_s, contexts, nsfw)
         else
           REST::Application.edit_global_command(@token, profile.id, command_id, name, description, builder.to_a, default_permission, type, default_member_permissions&.to_s, contexts, nsfw, integration_types)
         end
  cmd = ApplicationCommand.new(OnyxCord::Internal::JSON.parse(resp), self, server_id)

  if permission_builder.to_a.any?
    raise ArgumentError, 'Permissions can only be set for guild commands' unless server_id

    edit_application_command_permissions(cmd.id, server_id, permission_builder.to_a)
  end

  cmd
end

#edit_application_command_permissions(command_id, server_id, permissions = [], bearer_token = nil) {|builder| ... } ⇒ Object

Parameters:

  • command_id (Integer, String)
  • server_id (Integer, String)
  • permissions (Array<Hash>) (defaults to: [])

    An array of objects formatted as { id: ENTITY_ID, type: 1 or 2, permission: true or false }

  • bearer_token (String) (defaults to: nil)

    A valid bearer token that has permission to manage the server and its roles.

Yields:

  • (builder)

Raises:

  • (ArgumentError)


154
155
156
157
158
159
160
161
162
163
# File 'lib/onyxcord/core/bot/application_commands.rb', line 154

def edit_application_command_permissions(command_id, server_id, permissions = [], bearer_token = nil)
  builder = Interactions::PermissionBuilder.new
  yield builder if block_given?

  raise ArgumentError, 'This method requires a valid bearer token to be provided' unless bearer_token

  permissions += builder.to_a
  bearer_token = "Bearer #{bearer_token.delete_prefix('Bearer ')}"
  REST::Application.edit_guild_command_permissions(bearer_token, profile.id, server_id, command_id, permissions)
end

#edit_application_emoji(emoji_id, name:) ⇒ Emoji

Edits an existing application emoji.

Parameters:

  • emoji_id (Integer, String, Emoji)

    ID of the application emoji to edit.

  • name (String)

    The new name of the emoji.

Returns:

  • (Emoji)

    Returns the updated emoji object on success.



202
203
204
205
# File 'lib/onyxcord/core/bot/application_commands.rb', line 202

def edit_application_emoji(emoji_id, name:)
  response = REST::Application.edit_application_emoji(@token, profile.id, emoji_id.resolve_id, name)
  Emoji.new(OnyxCord::Internal::JSON.parse(response), self)
end

#get_application_command(command_id, server_id: nil) ⇒ Object

Get an application command by ID.

Parameters:

  • command_id (String, Integer)
  • server_id (String, Integer, nil) (defaults to: nil)

    The ID of the server to get the command from. Global if nil.



24
25
26
27
28
29
30
31
# File 'lib/onyxcord/core/bot/application_commands.rb', line 24

def get_application_command(command_id, server_id: nil)
  resp = if server_id
           REST::Application.get_guild_command(@token, profile.id, server_id, command_id)
         else
           REST::Application.get_global_command(@token, profile.id, command_id)
         end
  ApplicationCommand.new(OnyxCord::Internal::JSON.parse(resp), self, server_id)
end

#get_application_commands(server_id: nil) ⇒ Array<ApplicationCommand>

Get all application commands.

Parameters:

  • server_id (String, Integer, nil) (defaults to: nil)

    The ID of the server to get the commands from. Global if nil.

Returns:



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/onyxcord/core/bot/application_commands.rb', line 9

def get_application_commands(server_id: nil)
  resp = if server_id
           REST::Application.get_guild_commands(@token, profile.id, server_id)
         else
           REST::Application.get_global_commands(@token, profile.id)
         end

  OnyxCord::Internal::JSON.parse(resp).map do |command_data|
    ApplicationCommand.new(command_data, self, server_id)
  end
end

#message_command(name, **attributes, &block) ⇒ Object



50
51
52
# File 'lib/onyxcord/core/bot/application_commands.rb', line 50

def message_command(name, **attributes, &block)
  application_command_registry.message(name, **attributes, &block)
end

#register_application_command(name, description, server_id: nil, default_permission: nil, type: :chat_input, default_member_permissions: nil, contexts: nil, nsfw: false, integration_types: nil) {|, | ... } ⇒ Object

Examples:

bot.register_application_command(:reddit, 'Reddit Commands') do |cmd|
  cmd.subcommand_group(:subreddit, 'Subreddit Commands') do |group|
    group.subcommand(:hot, "What's trending") do |sub|
      sub.string(:subreddit, 'Subreddit to search')
    end
    group.subcommand(:new, "What's new") do |sub|
      sub.string(:since, 'How long ago', choices: ['this hour', 'today', 'this week', 'this month', 'this year', 'all time'])
      sub.string(:subreddit, 'Subreddit to search')
    end
  end
end

Yield Parameters:

  • (OptionBuilder)
  • (PermissionBuilder)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/onyxcord/core/bot/application_commands.rb', line 82

def register_application_command(name, description, server_id: nil, default_permission: nil, type: :chat_input, default_member_permissions: nil, contexts: nil, nsfw: false, integration_types: nil)
  type = ApplicationCommand::TYPES[type] || type

  contexts = contexts&.map { |context| Interaction::CONTEXTS[context] || context }
  integration_types = integration_types&.map { |type| Interaction::INTEGRATION_TYPES[type] || type }
  default_member_permissions = Permissions.bits(default_member_permissions) if default_member_permissions.is_a?(Array)

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

  resp = if server_id
           REST::Application.create_guild_command(@token, profile.id, server_id, name, description, builder.to_a, default_permission, type, default_member_permissions&.to_s, contexts, nsfw)
         else
           REST::Application.create_global_command(@token, profile.id, name, description, builder.to_a, default_permission, type, default_member_permissions&.to_s, contexts, nsfw, integration_types)
         end
  cmd = ApplicationCommand.new(OnyxCord::Internal::JSON.parse(resp), self, server_id)

  if permission_builder.to_a.any?
    raise ArgumentError, 'Permissions can only be set for guild commands' unless server_id

    edit_application_command_permissions(cmd.id, server_id, permission_builder.to_a)
  end

  cmd
end

#slash(name, description: nil, **attributes, &block) ⇒ Object



42
43
44
# File 'lib/onyxcord/core/bot/application_commands.rb', line 42

def slash(name, description: nil, **attributes, &block)
  application_command_registry.slash(name, description: description, **attributes, &block)
end

#sync_application_commands!(server_id: nil, delete_unknown: false) ⇒ Object



54
55
56
# File 'lib/onyxcord/core/bot/application_commands.rb', line 54

def sync_application_commands!(server_id: nil, delete_unknown: false)
  application_command_registry.sync!(server_id: server_id, delete_unknown: delete_unknown)
end

#user_command(name, **attributes, &block) ⇒ Object



46
47
48
# File 'lib/onyxcord/core/bot/application_commands.rb', line 46

def user_command(name, **attributes, &block)
  application_command_registry.user(name, **attributes, &block)
end