Class: DiscordRDA::Command

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

Overview

Represents a registered command

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, description:, options: [], subcommands: {}, subcommand_groups: {}, permissions: [], cooldown: nil, middleware: [], handler:, system:) ⇒ Command

Returns a new instance of Command.



224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/discord_rda/interactions/command_system.rb', line 224

def initialize(name:, description:, options: [], subcommands: {}, subcommand_groups: {},
               permissions: [], cooldown: nil, middleware: [], handler:, system:)
  @name = name.to_s
  @description = description
  @options = options
  @subcommands = subcommands
  @subcommand_groups = subcommand_groups
  @permissions = permissions
  @cooldown = cooldown
  @middleware = middleware
  @handler = handler
  @system = system
end

Instance Attribute Details

#cooldownHash (readonly)

Returns Cooldown configuration.

Returns:

  • (Hash)

    Cooldown configuration



213
214
215
# File 'lib/discord_rda/interactions/command_system.rb', line 213

def cooldown
  @cooldown
end

#descriptionString (readonly)

Returns Command description.

Returns:

  • (String)

    Command description



198
199
200
# File 'lib/discord_rda/interactions/command_system.rb', line 198

def description
  @description
end

#handlerProc (readonly)

Returns Command handler.

Returns:

  • (Proc)

    Command handler



219
220
221
# File 'lib/discord_rda/interactions/command_system.rb', line 219

def handler
  @handler
end

#middlewareArray (readonly)

Returns Per-command middleware.

Returns:

  • (Array)

    Per-command middleware



216
217
218
# File 'lib/discord_rda/interactions/command_system.rb', line 216

def middleware
  @middleware
end

#nameString (readonly)

Returns Command name.

Returns:

  • (String)

    Command name



195
196
197
# File 'lib/discord_rda/interactions/command_system.rb', line 195

def name
  @name
end

#optionsArray (readonly)

Returns Command options.

Returns:

  • (Array)

    Command options



201
202
203
# File 'lib/discord_rda/interactions/command_system.rb', line 201

def options
  @options
end

#permissionsArray (readonly)

Returns Required permissions.

Returns:

  • (Array)

    Required permissions



210
211
212
# File 'lib/discord_rda/interactions/command_system.rb', line 210

def permissions
  @permissions
end

#subcommand_groupsHash (readonly)

Returns Subcommand groups by name.

Returns:

  • (Hash)

    Subcommand groups by name



207
208
209
# File 'lib/discord_rda/interactions/command_system.rb', line 207

def subcommand_groups
  @subcommand_groups
end

#subcommandsHash (readonly)

Returns Subcommands by name.

Returns:

  • (Hash)

    Subcommands by name



204
205
206
# File 'lib/discord_rda/interactions/command_system.rb', line 204

def subcommands
  @subcommands
end

#systemCommandSystem (readonly)

Returns Parent command system.

Returns:



222
223
224
# File 'lib/discord_rda/interactions/command_system.rb', line 222

def system
  @system
end

Instance Method Details

#check_permissions(context) ⇒ Boolean

Check if user has required permissions

Parameters:

Returns:

  • (Boolean)

    True if has permissions



291
292
293
294
295
296
# File 'lib/discord_rda/interactions/command_system.rb', line 291

def check_permissions(context)
  return true if permissions.empty?
  return true unless context.member

  permissions.all? { |perm| context.member.permissions&.send("#{perm}?") }
end

#execute(context) ⇒ Object

Execute the command

Parameters:

Returns:

  • (Object)

    Handler result



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/discord_rda/interactions/command_system.rb', line 241

def execute(context)
  # Check permissions
  unless check_permissions(context)
    return context.respond(content: "You don't have permission to use this command.", ephemeral: true)
  end

  # Check cooldown
  if @cooldown
    remaining = system.cooldown_remaining(name, user_id: context.user.id.to_s, guild_id: context.guild_id)
    if remaining
      return context.respond(
        content: "This command is on cooldown. Try again in #{remaining.round} seconds.",
        ephemeral: true
      )
    end

    system.apply_cooldown(
      name,
      @cooldown[:duration] || 3,
      user_id: context.user.id.to_s,
      guild_id: context.guild_id,
      scope: @cooldown[:scope] || :user
    )
  end

  # Run command middleware
  @middleware.each do |mw|
    result = mw.call(context)
    return result if result == :halt
  end

  # Handle subcommands or groups
  if context.subcommand_group
    group = subcommand_groups[context.subcommand_group.to_s]
    return handler.call(context) unless group

    subcommand = group.subcommands[context.subcommand.to_s]
    return subcommand&.execute(context) || handler.call(context)
  elsif context.subcommand
    subcommand = subcommands[context.subcommand.to_s]
    return subcommand&.execute(context) || handler.call(context)
  end

  # Execute main handler
  handler.call(context)
end

#to_discord_formatHash

Convert to Discord API format

Returns:

  • (Hash)

    Discord command JSON



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/discord_rda/interactions/command_system.rb', line 300

def to_discord_format
  cmd = {
    name: name,
    description: description,
    options: options
  }

  # Add subcommands and groups as options
  subcommand_groups.each do |name, group|
    cmd[:options] << {
      type: 2, # SUB_COMMAND_GROUP
      name: name,
      description: group.description,
      options: group.subcommands.values.map(&:to_option_format)
    }
  end

  subcommands.each do |name, sub|
    cmd[:options] << sub.to_option_format
  end

  cmd
end