From f35ae6761d9656ec51bd9176a83320c5dcf44278 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 14 Apr 2014 02:06:53 -0700 Subject: Add support for modules. --- dinobot.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'dinobot.rb') 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 -- cgit v1.2.3-70-g09d2