Add command help

This commit is contained in:
Matthias Schiffer 2013-01-02 01:13:29 +01:00
parent 09e925d695
commit dceabdbb7e
6 changed files with 68 additions and 11 deletions

28
bot.rb
View file

@ -22,6 +22,8 @@ module Lain
@modules[mod] = Modules.const_get(mod).new(self, cfg) @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...' $stderr.puts 'Connecting...'
@cl = Jabber::Client.new(Jabber::JID.new(Config::JID)) @cl = Jabber::Client.new(Jabber::JID.new(Config::JID))
@ -36,23 +38,33 @@ module Lain
@mucs[r] = muc @mucs[r] = muc
muc.add_message_callback { |msg| muc.add_message_callback { |msg|
@modules.each { | _, mod | unless msg.from == r
begin @modules.each { | _, mod |
mod.on_message muc, msg begin
rescue mod.on_message muc, msg
end rescue
} end
}
end
} }
$stderr.puts "Joining room `#{r}'..." $stderr.puts "Joining room `#{r}'..."
muc.join("#{r}/#{Config::Nick}") muc.join(r)
muc.configure()
begin
muc.configure
rescue
end
} }
$stderr.puts 'Startup finished.' $stderr.puts 'Startup finished.'
end end
def commands
@commands
end
def run def run
@mainthread = Thread.current @mainthread = Thread.current
Thread.stop Thread.stop

View file

@ -2,11 +2,16 @@ module Lain
module Modules module Modules
class Base class Base
def initialize(lain, config) def initialize(lain, config)
@lain = lain
@config = config @config = config
end end
def on_message(muc, message) def on_message(muc, message)
end end
def commands
{}
end
end end
end end
end end

View file

@ -10,7 +10,13 @@ module Lain
return unless message.type == :groupchat return unless message.type == :groupchat
return unless /!credits\b/ =~ message.body 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 end
end end

View file

@ -9,7 +9,13 @@ module Lain
return unless message.type == :groupchat return unless message.type == :groupchat
return unless /!ddate\b/ =~ message.body 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 end
end end

View file

@ -9,7 +9,13 @@ module Lain
return unless message.type == :groupchat return unless message.type == :groupchat
return unless /!fortune\b/ =~ message.body 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 end
end end

22
modules/Help.rb Normal file
View file

@ -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