-
Notifications
You must be signed in to change notification settings - Fork 5
/
dispatcher.rb
66 lines (57 loc) · 1.8 KB
/
dispatcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'time'
class Dispatcher
include Cinch::Plugin
match /pazudora ([\w-]+) *(.+)*/i, method: :execute
match /stupidpuzzledragonbullshit ([\w-]+) *(.+)*/i, method: :execute
match /stupiddragonpuzzlebullshit ([\w-]+) *(.+)*/i, method: :execute
match /p&d ([\w-]+) *(.+)*/i, method: :execute
match /pad ([\w-]+) *(.+)*/i, method: :execute
match /puzzlemon ([\w-]+) *(.+)*/i, method: :execute
match /init/, method: :init_reactor
match /clockup/, method: :init_reactor
def initialize(*args)
super
@plugins = PazudoraPluginBase.descendants
@reactor_targets = @plugins.select {|p| p.instance.respond_to?(:tick)}
@@reactor_started ||= false
end
def init_reactor(m=nil, cmd=nil, args=nil)
if @reactor_started
m.reply "Event reactor already started." and return
end
@reactor_started = true
m.reply "Starting event reactor!"
p "Initializing reactor for: #{@reactor_targets.map(&:name).join(',')}"
while true do
p "Reactor firing: #{Time.now}"
@reactor_targets.each do |p|
begin
p.instance.tick(Time.now, self.bot.channels)
rescue Exception => e
print "Exception for #{p.name}: #{e}"
end
end
sleep(60)
end
end
def select_plugin(cmd)
@plugins.each do |plugin|
return plugin if plugin.aliases.include?(cmd)
end
nil
end
def execute(m, cmd, args)
plugin = select_plugin(cmd.downcase.chomp)
return if plugin.nil?
request_id = rand(4294967295)
log = File.open("asterbot.log", "a")
log.write("#{request_id} #{Time.now.iso8601} ##{m.channel} #{cmd}: #{args}\n")
plugin.set_dispatcher(self)
plugin.instance.respond(m, args)
log.write("#{request_id} TERM\n")
log.close
end
def exec_helper(method, *args)
self.send(method, *args)
end
end