Add Topic module
This commit is contained in:
parent
dceabdbb7e
commit
58649c9843
6 changed files with 86 additions and 14 deletions
4
bot.rb
4
bot.rb
|
@ -1,5 +1,5 @@
|
|||
require 'xmpp4r'
|
||||
require 'xmpp4r/muc/helper/mucclient'
|
||||
require 'xmpp4r/muc/helper/simplemucclient'
|
||||
|
||||
require_relative 'sasl'
|
||||
|
||||
|
@ -34,7 +34,7 @@ module Lain
|
|||
@mucs = {}
|
||||
|
||||
Config::Rooms.each { |r|
|
||||
muc = Jabber::MUC::MUCClient.new(@cl)
|
||||
muc = Jabber::MUC::SimpleMUCClient.new(@cl)
|
||||
@mucs[r] = muc
|
||||
|
||||
muc.add_message_callback { |msg|
|
||||
|
|
|
@ -7,10 +7,9 @@ module Lain
|
|||
module Modules
|
||||
class Credits < Base
|
||||
def on_message(muc, message)
|
||||
return unless message.type == :groupchat
|
||||
return unless /!credits\b/ =~ message.body
|
||||
return unless /\A!credits\b/ =~ message.body
|
||||
|
||||
muc.send(Jabber::Message.new(message.to, "\nLain (レイン) #{Version}\n\n -- hacked by NeoRaider"))
|
||||
muc.send(Jabber::Message.new(muc.room, "\nLain (レイン) #{Version}\n\n -- coded by NeoRaider"))
|
||||
end
|
||||
|
||||
def commands
|
||||
|
|
|
@ -6,10 +6,9 @@ module Lain
|
|||
module Modules
|
||||
class DDate < Base
|
||||
def on_message(muc, message)
|
||||
return unless message.type == :groupchat
|
||||
return unless /!ddate\b/ =~ message.body
|
||||
return unless /\A!ddate\b/ =~ message.body
|
||||
|
||||
muc.send(Jabber::Message.new(message.to, IO.popen("ddate").read.chomp))
|
||||
muc.send(Jabber::Message.new(muc.room, IO.popen("ddate").read.chomp))
|
||||
end
|
||||
|
||||
def commands
|
||||
|
|
|
@ -6,10 +6,9 @@ module Lain
|
|||
module Modules
|
||||
class Fortune < Base
|
||||
def on_message(muc, message)
|
||||
return unless message.type == :groupchat
|
||||
return unless /!fortune\b/ =~ message.body
|
||||
return unless /\A!fortune\b/ =~ message.body
|
||||
|
||||
muc.send(Jabber::Message.new(message.to, "\n" + IO.popen(@config['command']).read.chomp))
|
||||
muc.send(Jabber::Message.new(muc.room, "\n" + IO.popen(@config['command']).read.chomp))
|
||||
end
|
||||
|
||||
def commands
|
||||
|
|
|
@ -6,10 +6,9 @@ module Lain
|
|||
module Modules
|
||||
class Help < Base
|
||||
def on_message(muc, message)
|
||||
return unless message.type == :groupchat
|
||||
return unless /!help\b/ =~ message.body
|
||||
return unless /\A!help\b/ =~ message.body
|
||||
|
||||
muc.send(Jabber::Message.new(message.to, "Commands:" + @lain.commands.reduce('') { |s, cmd| "#{s}\n#{cmd[0]}: #{cmd[1]}" }))
|
||||
muc.send(Jabber::Message.new(muc.room, "Commands:" + @lain.commands.reduce('') { |s, cmd| "#{s}\n#{cmd[0]}: #{cmd[1]}" }))
|
||||
end
|
||||
|
||||
def commands
|
||||
|
|
76
modules/Topic.rb
Normal file
76
modules/Topic.rb
Normal file
|
@ -0,0 +1,76 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
require 'xmpp4r/message'
|
||||
|
||||
require_relative '../module_base'
|
||||
|
||||
module Lain
|
||||
module Modules
|
||||
class Topic < Base
|
||||
def on_message(muc, message)
|
||||
return unless /\A!topic\b/ =~ message.body
|
||||
|
||||
if /\A!topic\s*(?:\ss(?:how)?.*)?\Z/ =~ message.body
|
||||
show_topic(muc, message)
|
||||
elsif /\A!topic\s+a(?:dd)?\s+(.+)\Z/ =~ message.body
|
||||
add_topic(muc, message, $~[1].strip)
|
||||
elsif /\A!topic\s+d(?:el(?:ete)?)?\s+(\d+)\Z/ =~ message.body
|
||||
del_topic(muc, message, $~[1].to_i)
|
||||
end
|
||||
end
|
||||
|
||||
def commands
|
||||
{
|
||||
'!topic [s[how]]' => 'show topics (with IDs)',
|
||||
'!topic a[dd] <topic>' => 'add a topic to the topic line',
|
||||
'!topic d[el[ete]] <ID>' => 'remove topic <ID>'
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def current_topic(muc)
|
||||
topic = muc.subject
|
||||
return [] if topic.nil?
|
||||
|
||||
topic.split('|').map { |s| s.strip }
|
||||
end
|
||||
|
||||
def set_topic(muc, topic)
|
||||
muc.subject = topic.join(" | ")
|
||||
end
|
||||
|
||||
def topic_list(topic)
|
||||
topic.each_with_index.map { |t, i| "[#{i}] #{t}" }.join("\n")
|
||||
end
|
||||
|
||||
def show_topic(muc, message)
|
||||
topic = current_topic muc
|
||||
if topic.empty?
|
||||
muc.send(Jabber::Message.new(muc.room, "No topic set."))
|
||||
return
|
||||
end
|
||||
|
||||
muc.send(Jabber::Message.new(muc.room, "Current topics:\n" + topic_list(topic)))
|
||||
end
|
||||
|
||||
def add_topic(muc, message, text)
|
||||
topic = current_topic muc
|
||||
topic.unshift text
|
||||
set_topic(muc, topic)
|
||||
|
||||
muc.send(Jabber::Message.new(muc.room, "New topics:\n" + topic_list(topic)))
|
||||
end
|
||||
|
||||
def del_topic(muc, message, id)
|
||||
topic = current_topic muc
|
||||
begin
|
||||
topic.delete_at id
|
||||
rescue
|
||||
end
|
||||
set_topic(muc, topic)
|
||||
|
||||
muc.send(Jabber::Message.new(muc.room, "New topics:\n" + topic_list(topic)))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in a new issue