Class: Commandorobo::Bot
- Inherits:
-
Discordrb::Bot
- Object
- Discordrb::Bot
- Commandorobo::Bot
- Defined in:
- lib/commandorobo.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
what the hell ruby.
-
#config ⇒ Object
readonly
what the hell ruby.
-
#invokers ⇒ Object
Returns the value of attribute invokers.
-
#listeners ⇒ Object
readonly
what the hell ruby.
Instance Method Summary collapse
- #cmd(sym, perms: [], desc: nil, invokers: [], &block) ⇒ Object
- #evt(name, &block) ⇒ Object
- #get_command(name) ⇒ Object
- #get_invoker(text) ⇒ Object
- #idispatch(name, *args) ⇒ Object
-
#initialize(config, token, **kwargs) ⇒ Bot
constructor
A new instance of Bot.
- #invoke_by(thing, type, **kwargs) ⇒ Object
Constructor Details
#initialize(config, token, **kwargs) ⇒ Bot
Returns a new instance of Bot.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/commandorobo.rb', line 129 def initialize(config, token, **kwargs) @config = config @commands = [] @listeners = {} @invokers = config['invokers'].map {|i| Commandorobo::Invoker.new(i[1], i[0].to_sym)} 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 ⇒ Object (readonly)
what the hell ruby
128 129 130 |
# File 'lib/commandorobo.rb', line 128 def commands @commands end |
#config ⇒ Object (readonly)
what the hell ruby
128 129 130 |
# File 'lib/commandorobo.rb', line 128 def config @config end |
#invokers ⇒ Object
Returns the value of attribute invokers.
127 128 129 |
# File 'lib/commandorobo.rb', line 127 def invokers @invokers end |
#listeners ⇒ Object (readonly)
what the hell ruby
128 129 130 |
# File 'lib/commandorobo.rb', line 128 def listeners @listeners end |
Instance Method Details
#cmd(sym, perms: [], desc: nil, invokers: [], &block) ⇒ Object
197 198 199 200 |
# File 'lib/commandorobo.rb', line 197 def cmd(sym, perms:[], desc:nil, invokers:[], &block) invokers << sym @commands << Commandorobo::Command.new(sym, block, perms, desc, invokers, self) end |
#evt(name, &block) ⇒ Object
173 174 175 176 177 178 179 180 181 |
# File 'lib/commandorobo.rb', line 173 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) ⇒ Object
169 170 171 |
# File 'lib/commandorobo.rb', line 169 def get_command(name) @commands.select {|c| c.invokers.include? name}.compact[0] end |
#get_invoker(text) ⇒ Object
206 207 208 |
# File 'lib/commandorobo.rb', line 206 def get_invoker(text) @invokers.map {|a| a if a.check text}.reject(&:!).first # reject all false end |
#idispatch(name, *args) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/commandorobo.rb', line 183 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(thing, type, **kwargs) ⇒ Object
202 203 204 |
# File 'lib/commandorobo.rb', line 202 def invoke_by(thing, type, **kwargs) @invokers << Commandorobo::Invoker.new(thing, type, kwargs) end |