Class: OnyxCord::Cog

Inherits:
Object
  • Object
show all
Includes:
CogMeta
Defined in:
lib/onyxcord/cogs/cog.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CogMeta

#description, included, #qualified_name

Constructor Details

#initialize(bot) ⇒ Cog

Returns a new instance of Cog.



19
20
21
22
# File 'lib/onyxcord/cogs/cog.rb', line 19

def initialize(bot)
  @bot = bot
  _update_copy
end

Instance Attribute Details

#application_commandsObject (readonly)

Returns the value of attribute application_commands.



12
13
14
# File 'lib/onyxcord/cogs/cog.rb', line 12

def application_commands
  @application_commands
end

#botObject (readonly)

Returns the value of attribute bot.



12
13
14
# File 'lib/onyxcord/cogs/cog.rb', line 12

def bot
  @bot
end

#commandsObject (readonly)

Returns the value of attribute commands.



12
13
14
# File 'lib/onyxcord/cogs/cog.rb', line 12

def commands
  @commands
end

Class Method Details

.inherited(child) ⇒ Object



14
15
16
17
# File 'lib/onyxcord/cogs/cog.rb', line 14

def self.inherited(child)
  child.include CogMeta
  super
end

Instance Method Details

#_eject(bot) ⇒ Object



89
90
91
92
93
94
# File 'lib/onyxcord/cogs/cog.rb', line 89

def _eject(bot)
  eject_commands(bot)
  eject_listeners(bot)
  cog_unload
  self
end

#_inject(bot) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/onyxcord/cogs/cog.rb', line 68

def _inject(bot)
  @bot = bot
  old_commands = (bot.instance_variable_get(:@commands) || {}).dup
  old_application_commands = (bot.application_command_registry.commands || {}).dup
  old_application_handlers = (bot.instance_variable_get(:@application_commands) || {}).dup
  old_event_handlers = clone_event_handlers(bot.instance_variable_get(:@event_handlers))
  old_raw_handlers = (bot.instance_variable_get(:@raw_handlers) || []).dup

  inject_commands(bot)
  inject_listeners(bot)
  cog_load
  self
rescue StandardError
  bot.instance_variable_set(:@commands, old_commands)
  bot.application_command_registry.commands.replace(old_application_commands)
  bot.instance_variable_set(:@application_commands, old_application_handlers)
  bot.instance_variable_set(:@event_handlers, old_event_handlers)
  bot.instance_variable_set(:@raw_handlers, old_raw_handlers)
  raise
end

#_update_copyObject



96
97
98
99
100
101
102
103
104
# File 'lib/onyxcord/cogs/cog.rb', line 96

def _update_copy
  @commands = copy_prefix_commands
  @application_commands = self.class.application_commands.transform_values do |command|
    copy = command.copy_for(self)
    copy.parse(&copy.block) if copy.block && copy.options.empty?
    copy
  end
  @listeners = self.class.listeners.map { |listener| listener.merge(names: listener[:names].dup) }
end

#cog_after_invoke(_event, _result) ⇒ Object



54
# File 'lib/onyxcord/cogs/cog.rb', line 54

def cog_after_invoke(_event, _result); end

#cog_before_invoke(_event) ⇒ Object



52
# File 'lib/onyxcord/cogs/cog.rb', line 52

def cog_before_invoke(_event); end

#cog_check(_event) ⇒ Object



36
37
38
# File 'lib/onyxcord/cogs/cog.rb', line 36

def cog_check(_event)
  true
end

#cog_command_error(_event, _error) ⇒ Object



28
# File 'lib/onyxcord/cogs/cog.rb', line 28

def cog_command_error(_event, _error); end

#cog_loadObject



24
# File 'lib/onyxcord/cogs/cog.rb', line 24

def cog_load; end

#cog_message_after_invoke(_context, _result) ⇒ Object



66
# File 'lib/onyxcord/cogs/cog.rb', line 66

def cog_message_after_invoke(_context, _result); end

#cog_message_before_invoke(_context) ⇒ Object



64
# File 'lib/onyxcord/cogs/cog.rb', line 64

def cog_message_before_invoke(_context); end

#cog_message_command_check(_context) ⇒ Object



48
49
50
# File 'lib/onyxcord/cogs/cog.rb', line 48

def cog_message_command_check(_context)
  true
end

#cog_message_command_error(_context, _error) ⇒ Object



34
# File 'lib/onyxcord/cogs/cog.rb', line 34

def cog_message_command_error(_context, _error); end

#cog_slash_after_invoke(_context, _result) ⇒ Object



58
# File 'lib/onyxcord/cogs/cog.rb', line 58

def cog_slash_after_invoke(_context, _result); end

#cog_slash_before_invoke(_context) ⇒ Object



56
# File 'lib/onyxcord/cogs/cog.rb', line 56

def cog_slash_before_invoke(_context); end

#cog_slash_command_check(_context) ⇒ Object



40
41
42
# File 'lib/onyxcord/cogs/cog.rb', line 40

def cog_slash_command_check(_context)
  true
end

#cog_slash_command_error(_context, _error) ⇒ Object



30
# File 'lib/onyxcord/cogs/cog.rb', line 30

def cog_slash_command_error(_context, _error); end

#cog_unloadObject



26
# File 'lib/onyxcord/cogs/cog.rb', line 26

def cog_unload; end

#cog_user_after_invoke(_context, _result) ⇒ Object



62
# File 'lib/onyxcord/cogs/cog.rb', line 62

def cog_user_after_invoke(_context, _result); end

#cog_user_before_invoke(_context) ⇒ Object



60
# File 'lib/onyxcord/cogs/cog.rb', line 60

def cog_user_before_invoke(_context); end

#cog_user_command_check(_context) ⇒ Object



44
45
46
# File 'lib/onyxcord/cogs/cog.rb', line 44

def cog_user_command_check(_context)
  true
end

#cog_user_command_error(_context, _error) ⇒ Object



32
# File 'lib/onyxcord/cogs/cog.rb', line 32

def cog_user_command_error(_context, _error); end

#get_application_commandsObject



111
112
113
# File 'lib/onyxcord/cogs/cog.rb', line 111

def get_application_commands
  @application_commands.values
end

#get_commandsObject

rubocop:disable Naming/AccessorMethodName



107
108
109
# File 'lib/onyxcord/cogs/cog.rb', line 107

def get_commands
  @commands.values.grep(Commands::Command)
end

#get_listenersObject



115
116
117
# File 'lib/onyxcord/cogs/cog.rb', line 115

def get_listeners
  @listeners
end

#handled_cog_application_command_error?(context, error, command) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
141
# File 'lib/onyxcord/cogs/cog.rb', line 131

def handled_cog_application_command_error?(context, error, command)
  method_name = case command.type
                when :chat_input, :primary_entry_point then :cog_slash_command_error
                when :user then :cog_user_command_error
                when :message then :cog_message_command_error
                end
  return false unless method_name && overrides?(method_name)

  public_send(method_name, context, error)
  true
end

#handled_cog_command_error?(event, error) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
# File 'lib/onyxcord/cogs/cog.rb', line 124

def handled_cog_command_error?(event, error)
  return false unless overrides?(:cog_command_error)

  cog_command_error(event, error)
  true
end

#invoke_cog_after_invoke(event, result) ⇒ Object



166
167
168
169
170
# File 'lib/onyxcord/cogs/cog.rb', line 166

def invoke_cog_after_invoke(event, result)
  return unless overrides?(:cog_after_invoke)

  cog_after_invoke(event, result)
end

#invoke_cog_application_after_invoke(context, result, command) ⇒ Object



183
184
185
186
187
188
189
190
191
192
# File 'lib/onyxcord/cogs/cog.rb', line 183

def invoke_cog_application_after_invoke(context, result, command)
  method_name = case command.type
                when :chat_input, :primary_entry_point then :cog_slash_after_invoke
                when :user then :cog_user_after_invoke
                when :message then :cog_message_after_invoke
                end
  return unless method_name && overrides?(method_name)

  public_send(method_name, context, result)
end

#invoke_cog_application_before_invoke(context, command) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/onyxcord/cogs/cog.rb', line 172

def invoke_cog_application_before_invoke(context, command)
  method_name = case command.type
                when :chat_input, :primary_entry_point then :cog_slash_before_invoke
                when :user then :cog_user_before_invoke
                when :message then :cog_message_before_invoke
                end
  return unless method_name && overrides?(method_name)

  public_send(method_name, context)
end

#invoke_cog_before_invoke(event) ⇒ Object



160
161
162
163
164
# File 'lib/onyxcord/cogs/cog.rb', line 160

def invoke_cog_before_invoke(event)
  return unless overrides?(:cog_before_invoke)

  cog_before_invoke(event)
end

#passed_cog_application_command_check?(context, command) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
158
# File 'lib/onyxcord/cogs/cog.rb', line 149

def passed_cog_application_command_check?(context, command)
  method_name = case command.type
                when :chat_input, :primary_entry_point then :cog_slash_command_check
                when :user then :cog_user_command_check
                when :message then :cog_message_command_check
                end
  return true unless method_name && overrides?(method_name)

  public_send(method_name, context)
end

#passed_cog_command_check?(event) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
146
147
# File 'lib/onyxcord/cogs/cog.rb', line 143

def passed_cog_command_check?(event)
  return true unless overrides?(:cog_check)

  cog_check(event)
end

#walk_commandsObject

rubocop:enable Naming/AccessorMethodName



120
121
122
# File 'lib/onyxcord/cogs/cog.rb', line 120

def walk_commands
  get_commands + get_application_commands.flat_map { |command| [command, *walk_options(command.options)] }
end