Class: Commandorobo::Bot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#commandsObject (readonly)

what the hell ruby



128
129
130
# File 'lib/commandorobo.rb', line 128

def commands
  @commands
end

#configObject (readonly)

what the hell ruby



128
129
130
# File 'lib/commandorobo.rb', line 128

def config
  @config
end

#invokersObject

Returns the value of attribute invokers.



127
128
129
# File 'lib/commandorobo.rb', line 127

def invokers
  @invokers
end

#listenersObject (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