From e8908d71464ab36937ed4a95e8a32c4b3cb4726c Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 14 Apr 2014 04:36:52 -0700 Subject: Minor cleanup and improvements; remove run loop. --- dinobot.rb | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'dinobot.rb') diff --git a/dinobot.rb b/dinobot.rb index f5ea364..01b9735 100644 --- a/dinobot.rb +++ b/dinobot.rb @@ -2,6 +2,9 @@ require 'socket' module Dinobot class Bot + attr_accessor :trigger + attr_reader :server, :port, :nick, :pass, :modules, :channels + def initialize(server, port, nick, pass=nil, &block) @server = server @port = port @@ -34,23 +37,21 @@ module Dinobot end def run - while true - puts "== Connecting to #{@server}:#{@port}." - connect - - while str = @socket.gets - str.chomp! - puts "<< " + str.inspect - - begin - parse_line(str) - rescue - end - end + puts "== Connecting to #{@server}:#{@port}." + connect + + while str = @socket.gets + str.chomp! + puts "<< #{str.inspect}" - puts "== Disconnected." - @socket.close + begin + parse_line(str) + rescue + end end + + puts '== Disconnected.' + @socket.close end def parse_line(str) @@ -59,14 +60,14 @@ module Dinobot if str =~ /(\S+) PRIVMSG (\S+) :(.*)/ user, channel, message = str.scan(/(\S+) PRIVMSG (\S+) :(.*)/).first - if message =~ /^#{Regexp.escape(@trigger)}/ - message.sub!(@trigger, '') + return unless message =~ /^#{Regexp.escape(@trigger)}/ + + message.sub!(@trigger, '') - mod = message.split.first.downcase.intern + mod = message.split.first.downcase.intern - if @modules.has_key?(mod) - exec_commands(@modules[mod].call(user, channel, message)) - end + if @modules.has_key?(mod) + exec_commands(@modules[mod].call(user, channel, message)) end end end @@ -85,7 +86,7 @@ module Dinobot def out(str) return unless connected? - puts ">> " + str.inspect + puts ">> #{str.inspect}" @socket.puts str end -- cgit v1.2.3-70-g09d2