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.
-
#owners ⇒ Array
readonly
List of user IDs to consider as bot owners.
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.
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 210 |
# File 'lib/commandorobo.rb', line 171 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) # 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.
168 169 170 |
# File 'lib/commandorobo.rb', line 168 def commands @commands end |
#config ⇒ Hash (readonly)
The configuration for the bot.
168 169 170 |
# File 'lib/commandorobo.rb', line 168 def config @config end |
#invokers ⇒ Array
The invokers for the bot. Can be modified, but I’d rather you use #invoke_by.
168 169 170 |
# File 'lib/commandorobo.rb', line 168 def invokers @invokers end |
#listeners ⇒ Array (readonly)
Bound event listeners.
168 169 170 |
# File 'lib/commandorobo.rb', line 168 def listeners @listeners end |
#owners ⇒ Array (readonly)
List of user IDs to consider as bot owners.
168 169 170 |
# File 'lib/commandorobo.rb', line 168 def owners @owners end |
Instance Method Details
#cmd(sym, perms: [], desc: nil, invokers: [], &block) ⇒ nil
Adds a command.
256 257 258 259 |
# File 'lib/commandorobo.rb', line 256 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.
223 224 225 226 227 228 229 230 231 |
# File 'lib/commandorobo.rb', line 223 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.
215 216 217 |
# File 'lib/commandorobo.rb', line 215 def get_command(name) @commands.select {|c| c.invokers.include? name}.compact[0] end |
#get_invoker(text) ⇒ nil
Grabs an invoker from text.
272 273 274 |
# File 'lib/commandorobo.rb', line 272 def get_invoker(text) @invokers.map {|a| a if a.check text}.reject(&:!).first # reject all false end |
#idispatch(name, *args) ⇒ nil
Internal.
236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/commandorobo.rb', line 236 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.
265 266 267 |
# File 'lib/commandorobo.rb', line 265 def invoke_by(value, type, **kwargs) @invokers << Commandorobo::Invoker.new(value, type, kwargs) end |