Class: DiscordRDA::ApplicationCommand

Inherits:
Entity
  • Object
show all
Defined in:
lib/discord_rda/interactions/application_command.rb

Overview

Application Command (Slash Command) for Discord. Supports Chat Input Commands (slash), User Commands, and Message Commands.

Constant Summary collapse

EMPTY_EXECUTION_POLICY =
{}.freeze
TYPES =

Command types

{
  chat_input: 1,      # Slash commands
  user: 2,            # User context menu commands
  message: 3          # Message context menu commands
}.freeze
OPTION_TYPES =

Option types

{
  sub_command: 1,
  sub_command_group: 2,
  string: 3,
  integer: 4,
  boolean: 5,
  user: 6,
  channel: 7,
  role: 8,
  mentionable: 9,
  number: 10,
  attachment: 11
}.freeze
PERMISSIONS =

Permission constants

{
  create_instant_invite: 1 << 0,
  kick_members: 1 << 1,
  ban_members: 1 << 2,
  administrator: 1 << 3,
  manage_channels: 1 << 4,
  manage_guild: 1 << 5,
  add_reactions: 1 << 6,
  view_audit_log: 1 << 7,
  priority_speaker: 1 << 8,
  stream: 1 << 9,
  view_channel: 1 << 10,
  send_messages: 1 << 11,
  send_tts_messages: 1 << 12,
  manage_messages: 1 << 13,
  embed_links: 1 << 14,
  attach_files: 1 << 15,
  read_message_history: 1 << 16,
  mention_everyone: 1 << 17,
  use_external_emojis: 1 << 18,
  view_guild_insights: 1 << 19,
  connect: 1 << 20,
  speak: 1 << 21,
  mute_members: 1 << 22,
  deafen_members: 1 << 23,
  move_members: 1 << 24,
  use_vad: 1 << 25,
  change_nickname: 1 << 26,
  manage_nicknames: 1 << 27,
  manage_roles: 1 << 28,
  manage_webhooks: 1 << 29,
  manage_emojis_and_stickers: 1 << 30,
  use_application_commands: 1 << 31,
  request_to_speak: 1 << 32,
  manage_events: 1 << 33,
  manage_threads: 1 << 34,
  create_public_threads: 1 << 35,
  create_private_threads: 1 << 36,
  use_external_stickers: 1 << 37,
  send_messages_in_threads: 1 << 38,
  use_embedded_activities: 1 << 39,
  moderate_members: 1 << 40
}.freeze

Instance Attribute Summary

Attributes inherited from Entity

#id

Instance Method Summary collapse

Methods inherited from Entity

#==, attribute, #created_at, from_hash, #hash, #initialize, #inspect, #to_h, #to_json

Constructor Details

This class inherits a constructor from DiscordRDA::Entity

Instance Method Details

#chat_input?Boolean

Check if this is a chat input (slash) command

Returns:

  • (Boolean)

    True if slash command



99
100
101
# File 'lib/discord_rda/interactions/application_command.rb', line 99

def chat_input?
  type == 1
end

#command_typeSymbol

Get command type as symbol

Returns:

  • (Symbol)

    Command type



93
94
95
# File 'lib/discord_rda/interactions/application_command.rb', line 93

def command_type
  TYPES.key(type) || :chat_input
end

#create_global(bot) ⇒ ApplicationCommand

Create a global application command via REST API

Parameters:

  • bot (Bot)

    Bot instance

Returns:



150
151
152
153
# File 'lib/discord_rda/interactions/application_command.rb', line 150

def create_global(bot)
  data = bot.rest.post("/applications/#{application_id}/commands", body: to_api_hash)
  ApplicationCommand.new(data)
end

#create_guild(bot, guild_id) ⇒ ApplicationCommand

Create a guild-specific application command via REST API

Parameters:

  • bot (Bot)

    Bot instance

  • guild_id (String, Snowflake)

    Guild ID

Returns:



159
160
161
162
163
# File 'lib/discord_rda/interactions/application_command.rb', line 159

def create_guild(bot, guild_id)
  gid = guild_id.to_s
  data = bot.rest.post("/applications/#{application_id}/guilds/#{gid}/commands", body: to_api_hash)
  ApplicationCommand.new(data)
end

#delete(bot) ⇒ void

This method returns an undefined value.

Delete this command

Parameters:

  • bot (Bot)

    Bot instance



182
183
184
185
186
187
188
# File 'lib/discord_rda/interactions/application_command.rb', line 182

def delete(bot)
  if guild_id
    bot.rest.delete("/applications/#{application_id}/guilds/#{guild_id}/commands/#{id}")
  else
    bot.rest.delete("/applications/#{application_id}/commands/#{id}")
  end
end

#edit(bot, **changes) ⇒ ApplicationCommand

Edit this command

Parameters:

  • bot (Bot)

    Bot instance

  • changes (Hash)

    Changes to apply

Returns:



169
170
171
172
173
174
175
176
177
# File 'lib/discord_rda/interactions/application_command.rb', line 169

def edit(bot, **changes)
  payload = to_api_hash.merge(changes)
  if guild_id
    data = bot.rest.patch("/applications/#{application_id}/guilds/#{guild_id}/commands/#{id}", body: payload)
  else
    data = bot.rest.patch("/applications/#{application_id}/commands/#{id}", body: payload)
  end
  ApplicationCommand.new(data)
end

#edit_permissions(bot, permissions) ⇒ Hash

Edit command permissions

Parameters:

  • bot (Bot)

    Bot instance

  • permissions (Array<Hash>)

    Permission overwrites

Returns:

  • (Hash)

    Updated permissions



203
204
205
206
207
208
# File 'lib/discord_rda/interactions/application_command.rb', line 203

def edit_permissions(bot, permissions)
  return nil unless guild_id

  payload = { permissions: permissions }
  bot.rest.put("/applications/#{application_id}/guilds/#{guild_id}/commands/#{id}/permissions", body: payload)
end

#execution_policyObject



121
122
123
# File 'lib/discord_rda/interactions/application_command.rb', line 121

def execution_policy
  @execution_policy || EMPTY_EXECUTION_POLICY
end

#handlerProc?

Get the handler block for this command

Returns:

  • (Proc, nil)

    Handler block



117
118
119
# File 'lib/discord_rda/interactions/application_command.rb', line 117

def handler
  @handler
end

#handler=(block) ⇒ Object

Set the handler block

Parameters:

  • block (Proc)

    Handler block



127
128
129
# File 'lib/discord_rda/interactions/application_command.rb', line 127

def handler=(block)
  @handler = block
end

#message_command?Boolean

Check if this is a message context menu command

Returns:

  • (Boolean)

    True if message command



111
112
113
# File 'lib/discord_rda/interactions/application_command.rb', line 111

def message_command?
  type == 3
end

#permissions(bot) ⇒ Hash

Get command permissions for this guild command

Parameters:

  • bot (Bot)

    Bot instance

Returns:

  • (Hash)

    Command permissions



193
194
195
196
197
# File 'lib/discord_rda/interactions/application_command.rb', line 193

def permissions(bot)
  return nil unless guild_id

  bot.rest.get("/applications/#{application_id}/guilds/#{guild_id}/commands/#{id}/permissions")
end

#to_api_hashHash

Convert to API hash for creating/updating

Returns:

  • (Hash)

    API payload



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/discord_rda/interactions/application_command.rb', line 133

def to_api_hash
  {
    name: name,
    name_localizations: name_localizations,
    description: description,
    description_localizations: description_localizations,
    options: options,
    default_member_permissions: default_member_permissions,
    dm_permission: dm_permission,
    type: type,
    nsfw: nsfw
  }.compact
end

#user_command?Boolean

Check if this is a user context menu command

Returns:

  • (Boolean)

    True if user command



105
106
107
# File 'lib/discord_rda/interactions/application_command.rb', line 105

def user_command?
  type == 2
end