Class: Commandorobo::Bot
- Inherits:
-
Discordrb::Bot
- Object
- Discordrb::Bot
- Commandorobo::Bot
- Defined in:
- lib/commandorobo.rb
Overview
The main bot class. An extension of discordrb’s.
Instance Attribute Summary collapse
-
#commands ⇒ Array
readonly
The commands, in an array.
-
#config ⇒ Hash
readonly
The configuration for the bot.
-
#invokers ⇒ Array
The invokers for the bot.
-
#listeners ⇒ Array
readonly
Bound event listeners.
Instance Method Summary collapse
-
#cmd(sym, perms: [], desc: nil, invokers: [], &block) ⇒ nil
Adds a command.
-
#evt(name, &block) ⇒ nil
Registers a new event hook.
-
#get_command(name) ⇒ Commandorobo::Command
Gets a command based on its name.
-
#get_invoker(text) ⇒ nil
Grabs an invoker from text.
-
#idispatch(name, *args) ⇒ nil
Internal.
-
#initialize(config, token, **kwargs) ⇒ Bot
constructor
A new instance of Bot.
-
#invoke_by(value, type, **kwargs) ⇒ nil
Adds an invoker.
Constructor Details
#initialize(config, token, **kwargs) ⇒ Bot
Returns a new instance of Bot.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/commandorobo.rb', line 170 def initialize(config, token, **kwargs) @config = config @commands = [] @listeners = {} @invokers = config['invokers'].map {|i| Commandorobo::Invoker.new(i[1], i[0].to_sym)} @owners = @config['owner'] || kwargs[:owners] super(token: token, **kwargs) # begin command self. do |ev| meme = self.get_invoker(ev.text) if meme.nil? next end awau = meme.extrapolate(ev.text) if awau.is_a?(MatchData) awau = awau[1].split(/ /) else awau = awau.split(/ /) end cmd = awau.first acmd = self.get_command cmd.to_sym awau.shift sm = awau if !acmd.nil? pc = acmd.perm_check(ev) if pc.is_a?(Commandorobo::NoPermission) self.idispatch(:command_noperms, ev, acmd, pc) next end begin a = acmd.invoke(ev, Commandorobo::Arguments.new(sm)) ev.respond(a) rescue Exception => err self.idispatch(:command_error, ev, acmd, err) else self.idispatch(:command_notfound, ev, acmd) end end end end |
Instance Attribute Details
#commands ⇒ Array (readonly)
The commands, in an array. This isn’t a hash because I have a method for looking them up.
167 168 169 |
# File 'lib/commandorobo.rb', line 167 def commands @commands end |
#config ⇒ Hash (readonly)
The configuration for the bot.
167 168 169 |
# File 'lib/commandorobo.rb', line 167 def config @config end |
#invokers ⇒ Array
The invokers for the bot. Can be modified, but I’d rather you use #invoke_by.
167 168 169 |
# File 'lib/commandorobo.rb', line 167 def invokers @invokers end |
#listeners ⇒ Array (readonly)
Bound event listeners.
167 168 169 |
# File 'lib/commandorobo.rb', line 167 def listeners @listeners end |
Instance Method Details
#cmd(sym, perms: [], desc: nil, invokers: [], &block) ⇒ nil
Adds a command.
255 256 257 258 |
# File 'lib/commandorobo.rb', line 255 def cmd(sym, perms:[], desc:nil, invokers:[], &block) invokers << sym @commands << Commandorobo::Command.new(sym, block, perms, desc, invokers, self) end |
#evt(name, &block) ⇒ nil
Registers a new event hook.
222 223 224 225 226 227 228 229 230 |
# File 'lib/commandorobo.rb', line 222 def evt(name, &block) if name.is_a? String name = name.to_sym end if @listeners[name].nil? @listeners[name] = [] end @listeners[name] << block end |
#get_command(name) ⇒ Commandorobo::Command
Gets a command based on its name.
214 215 216 |
# File 'lib/commandorobo.rb', line 214 def get_command(name) @commands.select {|c| c.invokers.include? name}.compact[0] end |
#get_invoker(text) ⇒ nil
Grabs an invoker from text.
271 272 273 |
# File 'lib/commandorobo.rb', line 271 def get_invoker(text) @invokers.map {|a| a if a.check text}.reject(&:!).first # reject all false end |
#idispatch(name, *args) ⇒ nil
Internal.
235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/commandorobo.rb', line 235 def idispatch(name, *args) if name.is_a? String name = name.to_sym end thing = @listeners[name] if thing.nil? raise "No event hooks registered for #{name.to_s}" else thing.each do |func| func.call(*args) end end end |
#invoke_by(value, type, **kwargs) ⇒ nil
Adds an invoker.
264 265 266 |
# File 'lib/commandorobo.rb', line 264 def invoke_by(value, type, **kwargs) @invokers << Commandorobo::Invoker.new(value, type, kwargs) end |