Class: OnyxCord::Interactions::Command
- Inherits:
-
Object
- Object
- OnyxCord::Interactions::Command
- Defined in:
- lib/onyxcord/interactions/command.rb
Constant Summary collapse
- TYPES =
{ chat_input: 1, user: 2, message: 3, primary_entry_point: 4 }.freeze
- DESCRIPTION_TYPES =
%i[chat_input primary_entry_point].freeze
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .chat_input(name, description:, **attributes, &block) ⇒ Object
- .message(name, **attributes, &block) ⇒ Object
- .primary_entry_point(name, description:, **attributes, &block) ⇒ Object
- .user(name, **attributes, &block) ⇒ Object
Instance Method Summary collapse
- #call(context) ⇒ Object
- #copy_for(receiver = nil) ⇒ Object
- #execute(&block) ⇒ Object
-
#initialize(name, description: '', type: :chat_input, **attributes, &block) ⇒ Command
constructor
A new instance of Command.
- #method_missing(method_name, *args, **kwargs, &block) ⇒ Object
- #parse(&block) ⇒ Object
- #respond_to_missing?(_method_name, _include_private = false) ⇒ Boolean
- #subcommand(name, description, &block) ⇒ Object
- #subcommand_group(name, description, &block) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(name, description: '', type: :chat_input, **attributes, &block) ⇒ Command
Returns a new instance of Command.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/onyxcord/interactions/command.rb', line 33 def initialize(name, description: '', type: :chat_input, **attributes, &block) @name = name.to_s @description = description @type = type @attributes = attributes @options = [] @block = block @executor = nil @default_member_permissions = attributes[:default_member_permissions] @nsfw = attributes[:nsfw] @contexts = attributes[:contexts] @dm_permission = attributes.fetch(:dm_permission, true) @name_localizations = attributes[:name_localizations] @description_localizations = attributes[:description_localizations] @integration_types = attributes[:integration_types] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, **kwargs, &block) ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/onyxcord/interactions/command.rb', line 132 def method_missing(method_name, *args, **kwargs, &block) if @block && @block.arity.positive? @block.call(Context::Proxy.new(self, method_name, args, kwargs, block)) else super end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/onyxcord/interactions/command.rb', line 6 def attributes @attributes end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
6 7 8 |
# File 'lib/onyxcord/interactions/command.rb', line 6 def block @block end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
6 7 8 |
# File 'lib/onyxcord/interactions/command.rb', line 6 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/onyxcord/interactions/command.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/onyxcord/interactions/command.rb', line 6 def @options end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/onyxcord/interactions/command.rb', line 6 def type @type end |
Class Method Details
.chat_input(name, description:, **attributes, &block) ⇒ Object
17 18 19 |
# File 'lib/onyxcord/interactions/command.rb', line 17 def self.chat_input(name, description:, **attributes, &block) new(name, description: description, type: :chat_input, **attributes, &block) end |
.message(name, **attributes, &block) ⇒ Object
25 26 27 |
# File 'lib/onyxcord/interactions/command.rb', line 25 def self.(name, **attributes, &block) new(name, description: '', type: :message, **attributes, &block) end |
.primary_entry_point(name, description:, **attributes, &block) ⇒ Object
29 30 31 |
# File 'lib/onyxcord/interactions/command.rb', line 29 def self.primary_entry_point(name, description:, **attributes, &block) new(name, description: description, type: :primary_entry_point, **attributes, &block) end |
.user(name, **attributes, &block) ⇒ Object
21 22 23 |
# File 'lib/onyxcord/interactions/command.rb', line 21 def self.user(name, **attributes, &block) new(name, description: '', type: :user, **attributes, &block) end |
Instance Method Details
#call(context) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/onyxcord/interactions/command.rb', line 59 def call(context) return unless @executor return unless @check.nil? || @check.call(context, self) @cog_before_invoke&.call(context, self) result = @executor.call(context) @cog_after_invoke&.call(context, result, self) result rescue StandardError => e return if @error_handler&.call(context, e, self) raise e end |
#copy_for(receiver = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/onyxcord/interactions/command.rb', line 73 def copy_for(receiver = nil) copied = self.class.new(@name, description: @description, type: @type, **@attributes, &@block) copied.instance_variable_set(:@receiver, receiver) copied.instance_variable_set(:@check, proc { |context, command| receiver.__send__(:passed_cog_application_command_check?, context, command) }) if receiver copied.instance_variable_set(:@error_handler, proc { |context, error, command| receiver.__send__(:handled_cog_application_command_error?, context, error, command) }) if receiver copied.instance_variable_set(:@cog_before_invoke, proc { |context, command| receiver.__send__(:invoke_cog_application_before_invoke, context, command) }) if receiver copied.instance_variable_set(:@cog_after_invoke, proc { |context, result, command| receiver.__send__(:invoke_cog_application_after_invoke, context, result, command) }) if receiver copied.instance_variable_set(:@options, @options.map(&:dup)) if @executor executor = @executor copied.execute { |context| receiver ? receiver.instance_exec(context, &executor) : executor.call(context) } end copied end |
#execute(&block) ⇒ Object
55 56 57 |
# File 'lib/onyxcord/interactions/command.rb', line 55 def execute(&block) @executor = @receiver ? proc { |context| @receiver.instance_exec(context, &block) } : block end |
#parse(&block) ⇒ Object
50 51 52 53 |
# File 'lib/onyxcord/interactions/command.rb', line 50 def parse(&block) instance_eval(&block) if block self end |
#respond_to_missing?(_method_name, _include_private = false) ⇒ Boolean
140 141 142 |
# File 'lib/onyxcord/interactions/command.rb', line 140 def respond_to_missing?(_method_name, _include_private = false) true end |
#subcommand(name, description, &block) ⇒ Object
120 121 122 123 124 |
# File 'lib/onyxcord/interactions/command.rb', line 120 def subcommand(name, description, &block) sub = Option.new(name, description, :subcommand, &block) @options << sub sub end |
#subcommand_group(name, description, &block) ⇒ Object
126 127 128 129 130 |
# File 'lib/onyxcord/interactions/command.rb', line 126 def subcommand_group(name, description, &block) group = Option.new(name, description, :subcommand_group, &block) @options << group group end |
#to_h ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/onyxcord/interactions/command.rb', line 90 def to_h data = { name: @name, type: TYPES[@type] || @type } if DESCRIPTION_TYPES.include?(@type) data[:description] = @description data[:description_localizations] = @description_localizations if @description_localizations end data[:name_localizations] = @name_localizations if @name_localizations data[:options] = @options.map(&:to_h) unless @options.empty? data[:default_member_permissions] = @default_member_permissions.to_s if @default_member_permissions data[:dm_permission] = @dm_permission data[:nsfw] = @nsfw if @nsfw data[:contexts] = @contexts if @contexts data[:integration_types] = @integration_types if @integration_types data end |