Class: Commandorobo::Bot

Inherits:
Discordrb::Bot
  • Object
show all
Defined in:
lib/commandorobo.rb

Overview

The main bot class. An extension of discordrb’s.

Instance Attribute Summary collapse

Instance Method Summary collapse

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.message 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

#commandsArray (readonly)

The commands, in an array. This isn’t a hash because I have a method for looking them up.

Returns:

  • (Array)

    the current value of commands



168
169
170
# File 'lib/commandorobo.rb', line 168

def commands
  @commands
end

#configHash (readonly)

The configuration for the bot.

Returns:

  • (Hash)

    the current value of config



168
169
170
# File 'lib/commandorobo.rb', line 168

def config
  @config
end

#invokersArray

The invokers for the bot. Can be modified, but I’d rather you use #invoke_by.

Returns:

  • (Array)

    the current value of invokers



168
169
170
# File 'lib/commandorobo.rb', line 168

def invokers
  @invokers
end

#listenersArray (readonly)

Bound event listeners.

Returns:

  • (Array)

    the current value of listeners



168
169
170
# File 'lib/commandorobo.rb', line 168

def listeners
  @listeners
end

#ownersArray (readonly)

List of user IDs to consider as bot owners.

Returns:

  • (Array)

    the current value of 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.

Parameters:

  • sym (Symbol)

    The command name.

  • perms (Array) (defaults to: [])

    The permissions required by the command. Can be any valid discordrb permission, or :bot_owner, that will restrict to owners.

  • desc (String, nil) (defaults to: nil)

    The description for the command.

  • block (Block)

    The block to run when the command is invoked.

Returns:

  • (nil)


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.

Parameters:

  • name (Symbol)

    The event hook.

  • block (Block)

    The code to run when the event fires.

Returns:

  • (nil)


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.

Parameters:

  • name (Symbol)

    The command to look for.

Returns:



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.

Parameters:

  • text (String)

    The text to parse.

Returns:

  • (nil)


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.

Returns:

  • (nil)

Raises:

  • (RuntimeError)

    No event hooks registered for the event.



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.

Parameters:

  • value (String)

    The value of the invoker.

  • type (Symbol)

    The type of the invoker.

Returns:

  • (nil)


265
266
267
# File 'lib/commandorobo.rb', line 265

def invoke_by(value, type, **kwargs)
    @invokers << Commandorobo::Invoker.new(value, type, kwargs)
end