Class: DiscordRDA::Command
- Inherits:
-
Object
- Object
- DiscordRDA::Command
- Defined in:
- lib/discord_rda/interactions/command_system.rb
Overview
Represents a registered command
Instance Attribute Summary collapse
-
#cooldown ⇒ Hash
readonly
Cooldown configuration.
-
#description ⇒ String
readonly
Command description.
-
#handler ⇒ Proc
readonly
Command handler.
-
#middleware ⇒ Array
readonly
Per-command middleware.
-
#name ⇒ String
readonly
Command name.
-
#options ⇒ Array
readonly
Command options.
-
#permissions ⇒ Array
readonly
Required permissions.
-
#subcommand_groups ⇒ Hash
readonly
Subcommand groups by name.
-
#subcommands ⇒ Hash
readonly
Subcommands by name.
-
#system ⇒ CommandSystem
readonly
Parent command system.
Instance Method Summary collapse
-
#check_permissions(context) ⇒ Boolean
Check if user has required permissions.
-
#execute(context) ⇒ Object
Execute the command.
-
#initialize(name:, description:, options: [], subcommands: {}, subcommand_groups: {}, permissions: [], cooldown: nil, middleware: [], handler:, system:) ⇒ Command
constructor
A new instance of Command.
-
#to_discord_format ⇒ Hash
Convert to Discord API format.
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 = @subcommands = subcommands @subcommand_groups = subcommand_groups @permissions = @cooldown = cooldown @middleware = middleware @handler = handler @system = system end |
Instance Attribute Details
#cooldown ⇒ Hash (readonly)
Returns Cooldown configuration.
213 214 215 |
# File 'lib/discord_rda/interactions/command_system.rb', line 213 def cooldown @cooldown end |
#description ⇒ String (readonly)
Returns Command description.
198 199 200 |
# File 'lib/discord_rda/interactions/command_system.rb', line 198 def description @description end |
#handler ⇒ Proc (readonly)
Returns Command handler.
219 220 221 |
# File 'lib/discord_rda/interactions/command_system.rb', line 219 def handler @handler end |
#middleware ⇒ Array (readonly)
Returns Per-command middleware.
216 217 218 |
# File 'lib/discord_rda/interactions/command_system.rb', line 216 def middleware @middleware end |
#name ⇒ String (readonly)
Returns Command name.
195 196 197 |
# File 'lib/discord_rda/interactions/command_system.rb', line 195 def name @name end |
#options ⇒ Array (readonly)
Returns Command options.
201 202 203 |
# File 'lib/discord_rda/interactions/command_system.rb', line 201 def @options end |
#permissions ⇒ Array (readonly)
Returns Required permissions.
210 211 212 |
# File 'lib/discord_rda/interactions/command_system.rb', line 210 def @permissions end |
#subcommand_groups ⇒ Hash (readonly)
Returns Subcommand groups by name.
207 208 209 |
# File 'lib/discord_rda/interactions/command_system.rb', line 207 def subcommand_groups @subcommand_groups end |
#subcommands ⇒ Hash (readonly)
Returns Subcommands by name.
204 205 206 |
# File 'lib/discord_rda/interactions/command_system.rb', line 204 def subcommands @subcommands end |
#system ⇒ CommandSystem (readonly)
Returns Parent command system.
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
291 292 293 294 295 296 |
# File 'lib/discord_rda/interactions/command_system.rb', line 291 def (context) return true if .empty? return true unless context.member .all? { |perm| context.member.&.send("#{perm}?") } end |
#execute(context) ⇒ Object
Execute the command
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 (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_format ⇒ Hash
Convert to Discord API format
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: } # 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 |