From 03a29033194c7740ea4717cbfe2fbaf32325611d Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 14 Apr 2014 18:08:07 -0700 Subject: Clean up command code. --- dinobot.rb | 47 ++++++++++++++++++++++------------------------- test.rb | 6 +++++- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/dinobot.rb b/dinobot.rb index 16b8573..09964ef 100644 --- a/dinobot.rb +++ b/dinobot.rb @@ -52,6 +52,7 @@ module Dinobot end rescue => e puts "!! Error parsing line. (#{e})" + puts e.backtrace end end end @@ -67,50 +68,46 @@ module Dinobot user, channel, message = str.scan(/(\S+) PRIVMSG (\S+) :(.*)/).first return unless message =~ /^#{Regexp.escape(@trigger)}/ - message.sub!(@trigger, '') - commands = parse_command(user, channel, message) + methods = parse_command(user, channel, message) - exec_commands(commands) if commands.is_a?(Array) + run_methods(methods) if methods.is_a?(Array) end end - def parse_command(user, channel, message, pc=nil) - mod = message.split.first.downcase.intern - - m = message.split(' | ', 2) + def parse_command(user, channel, command, prev=nil) + command, remainder = command.split(' | ', 2) + mod = command.scan(/\A\S+/).first.downcase.intern - if pc.nil? - commands = @modules[mod].call(user, channel, m.first) + if prev.nil? + methods = @modules[mod].call(user, channel, command) else - commands = [] + methods = [] + + prev.each do |p| + if p.first == :say + m = @modules[mod].call(user, p[1], "#{command} #{p[2]}") - pc.each do |c| - if c.first == :say - commands.concat(@modules[mod].call(user, c[1], "#{m.first} #{c[2]}")) + methods.concat(m) if m.is_a?(Array) else - commands << c + methods << p end end end - if m.length == 2 - parse_command(user, channel, m.last, commands) - else - commands - end + remainder ? parse_command(user, channel, remainder, methods) : methods end - def exec_commands(commands) - commands.each do |command| - puts "== Executing command: #{command.inspect}" + def run_methods(methods) + methods.each do |method| + puts "== Executing method: #{method.inspect}" - case command.first + case method.first when :say - send(*command) if command.length == 3 + send(*method) if method.length == 3 when :join, :part, :load_module, :unload_module - send(*command) if command.length == 2 + send(*method) if method.length == 2 end end end diff --git a/test.rb b/test.rb index 665c21e..735af2c 100644 --- a/test.rb +++ b/test.rb @@ -5,7 +5,7 @@ module Dinobot def initialize super - @commands << :echo << :error << :timeout << :x3 << :wrongreturn + @commands << :echo << :error << :timeout << :x3 << :wrongreturn << :fooify end def echo(user, channel, argument) @@ -27,5 +27,9 @@ module Dinobot def wrongreturn(user, channel, argument) 0 end + + def fooify(user, channel, argument) + [[:say, channel, 'foo' + argument]] + end end end -- cgit v1.2.3-70-g09d2