From dceabdbb7ede39694ecac9e71b36124e4123e741 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 2 Jan 2013 01:13:29 +0100 Subject: Add command help --- bot.rb | 28 ++++++++++++++++++++-------- module_base.rb | 5 +++++ modules/Credits.rb | 8 +++++++- modules/DDate.rb | 8 +++++++- modules/Fortune.rb | 8 +++++++- modules/Help.rb | 22 ++++++++++++++++++++++ 6 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 modules/Help.rb diff --git a/bot.rb b/bot.rb index 6040029..524bb02 100644 --- a/bot.rb +++ b/bot.rb @@ -22,6 +22,8 @@ module Lain @modules[mod] = Modules.const_get(mod).new(self, cfg) } + @commands = @modules.values.reduce({}) { |c, mod| c.merge mod.commands }.to_a.sort + $stderr.puts 'Connecting...' @cl = Jabber::Client.new(Jabber::JID.new(Config::JID)) @@ -36,23 +38,33 @@ module Lain @mucs[r] = muc muc.add_message_callback { |msg| - @modules.each { | _, mod | - begin - mod.on_message muc, msg - rescue - end - } + unless msg.from == r + @modules.each { | _, mod | + begin + mod.on_message muc, msg + rescue + end + } + end } $stderr.puts "Joining room `#{r}'..." - muc.join("#{r}/#{Config::Nick}") - muc.configure() + muc.join(r) + + begin + muc.configure + rescue + end } $stderr.puts 'Startup finished.' end + def commands + @commands + end + def run @mainthread = Thread.current Thread.stop diff --git a/module_base.rb b/module_base.rb index 01d3f74..b209e7b 100644 --- a/module_base.rb +++ b/module_base.rb @@ -2,11 +2,16 @@ module Lain module Modules class Base def initialize(lain, config) + @lain = lain @config = config end def on_message(muc, message) end + + def commands + {} + end end end end diff --git a/modules/Credits.rb b/modules/Credits.rb index e0289e4..190c284 100644 --- a/modules/Credits.rb +++ b/modules/Credits.rb @@ -10,7 +10,13 @@ module Lain return unless message.type == :groupchat return unless /!credits\b/ =~ message.body - muc.send(Jabber::Message.new(message.to, "Lain (レイン) #{Version}\n\n -- hacked by NeoRaider")) + muc.send(Jabber::Message.new(message.to, "\nLain (レイン) #{Version}\n\n -- hacked by NeoRaider")) + end + + def commands + { + '!credits' => 'show credits' + } end end end diff --git a/modules/DDate.rb b/modules/DDate.rb index af9912e..3c9f3e1 100644 --- a/modules/DDate.rb +++ b/modules/DDate.rb @@ -9,7 +9,13 @@ module Lain return unless message.type == :groupchat return unless /!ddate\b/ =~ message.body - muc.send(Jabber::Message.new(message.to, IO.popen("ddate").gets(sep=nil).chomp)) + muc.send(Jabber::Message.new(message.to, IO.popen("ddate").read.chomp)) + end + + def commands + { + '!ddate' => 'show Discordian date' + } end end end diff --git a/modules/Fortune.rb b/modules/Fortune.rb index 2b9d63b..f8b237a 100644 --- a/modules/Fortune.rb +++ b/modules/Fortune.rb @@ -9,7 +9,13 @@ module Lain return unless message.type == :groupchat return unless /!fortune\b/ =~ message.body - muc.send(Jabber::Message.new(message.to, IO.popen(@config['command']).gets(sep=nil).chomp)) + muc.send(Jabber::Message.new(message.to, "\n" + IO.popen(@config['command']).read.chomp)) + end + + def commands + { + '!fortune' => 'fortune cookies' + } end end end diff --git a/modules/Help.rb b/modules/Help.rb new file mode 100644 index 0000000..84d9a9e --- /dev/null +++ b/modules/Help.rb @@ -0,0 +1,22 @@ +require 'xmpp4r/message' + +require_relative '../module_base' + +module Lain + module Modules + class Help < Base + def on_message(muc, message) + return unless message.type == :groupchat + return unless /!help\b/ =~ message.body + + muc.send(Jabber::Message.new(message.to, "Commands:" + @lain.commands.reduce('') { |s, cmd| "#{s}\n#{cmd[0]}: #{cmd[1]}" })) + end + + def commands + { + '!help' => 'show this help' + } + end + end + end +end -- cgit v1.2.3