aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2014-04-14 02:06:53 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2014-04-14 02:06:53 -0700
commitf35ae6761d9656ec51bd9176a83320c5dcf44278 (patch)
treec0ae3eccc5dacbcddc91c0152ff5e99783299e7a
parent3446d5f2ec4263fc097d4d5c7996d88daf220693 (diff)
downloaddinobot-f35ae6761d9656ec51bd9176a83320c5dcf44278.tar.gz
dinobot-f35ae6761d9656ec51bd9176a83320c5dcf44278.tar.xz
Add support for modules.
-rw-r--r--dinobot.rb32
-rw-r--r--echo.rb15
-rw-r--r--module.rb22
3 files changed, 69 insertions, 0 deletions
diff --git a/dinobot.rb b/dinobot.rb
index f88b81b..464f299 100644
--- a/dinobot.rb
+++ b/dinobot.rb
@@ -55,6 +55,27 @@ module Dinobot
def parse_line(str)
out str.sub('PING', 'PONG') if str =~ /^PING /
+
+ if str =~ /(\S+) PRIVMSG (\S+) :(.*)/
+ user, channel, message = str.scan(/(\S+) PRIVMSG (\S+) :(.*)/).first
+
+ if @modules.has_key?(message.split.first.sub(/^#{Regexp.escape(@trigger)}/, '').downcase.intern)
+ ret = @modules[message.split.first.sub(/^#{Regexp.escape(@trigger)}/, '').downcase.intern].call(user, channel, message)
+
+ exec_commands(ret)
+ end
+ end
+ end
+
+ def exec_commands(commands)
+ commands.each do |command|
+ case command.first
+ when :say
+ send(*command) if command.length == 3
+ when :join, :part
+ send(*command) if command.length == 2
+ end
+ end
end
def out(str)
@@ -80,5 +101,16 @@ module Dinobot
out "PART #{channel}"
end
+
+ def load_module(mod)
+ file = Dir.entries(File.dirname(__FILE__)).find { |x| x == mod.to_s.downcase + ".rb" }
+
+ if file
+ puts "== Loading #{mod}."
+
+ load file
+ @modules[mod.downcase] = eval("Dinobot::#{mod}").new
+ end
+ end
end
end
diff --git a/echo.rb b/echo.rb
new file mode 100644
index 0000000..4911ad0
--- /dev/null
+++ b/echo.rb
@@ -0,0 +1,15 @@
+require_relative 'module'
+
+module Dinobot
+ class Echo < Module
+ def initialize
+ super
+
+ @commands << :echo
+ end
+
+ def echo(user, channel, message)
+ [[:say, channel, message]]
+ end
+ end
+end
diff --git a/module.rb b/module.rb
new file mode 100644
index 0000000..f0c415a
--- /dev/null
+++ b/module.rb
@@ -0,0 +1,22 @@
+module Dinobot
+ class Module
+ attr_accessor :commands
+
+ def initialize
+ @commands = [:commands]
+ end
+
+ def call(user, channel, message)
+ message = message.split(' ', 2).last
+ command = message.split.first
+
+ if @commands.include?(command.intern)
+ send(command, user, channel, message)
+ end
+ end
+
+ def commands(user, channel, message)
+ [[:say, channel, "Commands: #{@commands.sort.join(' ')}"]]
+ end
+ end
+end