Class: Discordrb::Commands::Command
- Inherits:
-
Object
- Object
- Discordrb::Commands::Command
- Defined in:
- lib/discordrb/commands/parser.rb
Overview
Command that can be called in a chain
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
readonly
The attributes the command was initialized with.
-
#name ⇒ Symbol
readonly
The name of this command.
Instance Method Summary collapse
-
#call(event, arguments, chained = false, check_permissions = true) ⇒ String
Calls this command and executes the code inside.
Instance Attribute Details
#attributes ⇒ Hash (readonly)
Returns the attributes the command was initialized with.
7 8 9 |
# File 'lib/discordrb/commands/parser.rb', line 7 def attributes @attributes end |
#name ⇒ Symbol (readonly)
Returns the name of this command.
10 11 12 |
# File 'lib/discordrb/commands/parser.rb', line 10 def name @name end |
Instance Method Details
#call(event, arguments, chained = false, check_permissions = true) ⇒ String
Calls this command and executes the code inside.
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/discordrb/commands/parser.rb', line 82 def call(event, arguments, chained = false, = true) if arguments.length < @attributes[:min_args] response = "Too few arguments for command `#{name}`!" response += "\nUsage: `#{@attributes[:usage]}`" if @attributes[:usage] event.respond(response) return end if @attributes[:max_args] >= 0 && arguments.length > @attributes[:max_args] response = "Too many arguments for command `#{name}`!" response += "\nUsage: `#{@attributes[:usage]}`" if @attributes[:usage] event.respond(response) return end unless @attributes[:chain_usable] && !chained event.respond "Command `#{name}` cannot be used in a command chain!" return end if rate_limited = event.bot.rate_limited?(@attributes[:bucket], event.) if @attributes[:bucket] && rate_limited event.respond @attributes[:rate_limit_message].gsub('%time%', rate_limited.round(2).to_s) if @attributes[:rate_limit_message] return end end result = @block.call(event, *arguments) event.drain_into(result) rescue LocalJumpError => e # occurs when breaking result = e.exit_value event.drain_into(result) rescue StandardError => e # Something went wrong inside our @block! rescue_value = @attributes[:rescue] || event.bot.attributes[:rescue] if rescue_value event.respond(rescue_value.gsub('%exception%', e.)) if rescue_value.is_a?(String) rescue_value.call(event, e) if rescue_value.respond_to?(:call) end raise e end |