diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-01-02 01:13:29 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-01-02 01:13:29 +0100 |
commit | dceabdbb7ede39694ecac9e71b36124e4123e741 (patch) | |
tree | 9fbab4f2f8971baa741521cf6e7ad70b267f0ba4 | |
parent | 09e925d6956cd7431a3466d8b659a7207e029617 (diff) | |
download | lain-dceabdbb7ede39694ecac9e71b36124e4123e741.tar lain-dceabdbb7ede39694ecac9e71b36124e4123e741.zip |
Add command help
-rw-r--r-- | bot.rb | 28 | ||||
-rw-r--r-- | module_base.rb | 5 | ||||
-rw-r--r-- | modules/Credits.rb | 8 | ||||
-rw-r--r-- | modules/DDate.rb | 8 | ||||
-rw-r--r-- | modules/Fortune.rb | 8 | ||||
-rw-r--r-- | modules/Help.rb | 22 |
6 files changed, 68 insertions, 11 deletions
@@ -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 |