Class: Imouto::Bot
- Inherits:
-
Object
- Object
- Imouto::Bot
- Defined in:
- lib/imouto.rb
Instance Attribute Summary collapse
-
#irc ⇒ Object
readonly
Returns the value of attribute irc.
-
#matchers ⇒ Object
readonly
Returns the value of attribute matchers.
-
#reply_queue ⇒ Object
readonly
Returns the value of attribute reply_queue.
Instance Method Summary collapse
-
#dequeue_replies(irc_con = @irc) ⇒ Object
Write replies to the IRC-connection.
-
#initialize(irc, conf) ⇒ Bot
constructor
A new instance of Bot.
-
#log(msg) ⇒ Object
Calls every logger with a message.
-
#queue_reply(target, reply) ⇒ Object
Enqueues a reply, to be sent.
-
#read(irc_con = @irc) ⇒ Object
Read from the IRC-connection until it closes.
-
#register_matcher(regex, matcher) ⇒ Object
Registers a matcher to respond to PRIVMSGs that match a certain regex.
-
#reply(msg, matcher, matches) ⇒ Object
Reply to a PRIVMSG.
-
#start ⇒ Object
Starts the bot, spawning a read- and a write-thread.
-
#unregister_matcher(regex) ⇒ Object
Consumes a regex, and removes the matcher that was registered with it.
Constructor Details
#initialize(irc, conf) ⇒ Bot
Returns a new instance of Bot.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/imouto.rb', line 25 def initialize(irc, conf) imouto_config = ImoutoConfig.new conf.each { |k, v| imouto_config[k] = v; } @loggers = imouto_config.loggers || [->(msg) { p msg }] @matchers = {} @irc = irc @reply_queue = Imouto::RatelimitedQueue.new( imouto_config. || 3, imouto_config. || 4) end |
Instance Attribute Details
#irc ⇒ Object (readonly)
Returns the value of attribute irc.
23 24 25 |
# File 'lib/imouto.rb', line 23 def irc @irc end |
#matchers ⇒ Object (readonly)
Returns the value of attribute matchers.
23 24 25 |
# File 'lib/imouto.rb', line 23 def matchers @matchers end |
#reply_queue ⇒ Object (readonly)
Returns the value of attribute reply_queue.
23 24 25 |
# File 'lib/imouto.rb', line 23 def reply_queue @reply_queue end |
Instance Method Details
#dequeue_replies(irc_con = @irc) ⇒ Object
Write replies to the IRC-connection
67 68 69 70 71 72 |
# File 'lib/imouto.rb', line 67 def dequeue_replies(irc_con = @irc) @reply_queue.dequeue { |reply, irc = irc_con| log("[->] #{reply['target']}: #{reply['reply']}") irc.privmsg(reply['target'], reply['reply']) } end |
#log(msg) ⇒ Object
Calls every logger with a message.
82 83 84 |
# File 'lib/imouto.rb', line 82 def log(msg) @loggers.each { |l| l.call(msg) } end |
#queue_reply(target, reply) ⇒ Object
Enqueues a reply, to be sent. You can also directly send messages by using @irc.privmsg this however bypasses rate limiting and might get you kicked.
77 78 79 |
# File 'lib/imouto.rb', line 77 def queue_reply(target, reply) @reply_queue.enqueue('target' => target, 'reply' => reply) end |
#read(irc_con = @irc) ⇒ Object
Read from the IRC-connection until it closes.
45 46 47 48 49 50 51 |
# File 'lib/imouto.rb', line 45 def read(irc_con = @irc) irc_con.start.read { |msg, matchers = @matchers| matchers.keys.each { |regex| msg[:message] =~ regex && reply(msg, matchers[regex], regex.match(msg[:message])) } } end |
#register_matcher(regex, matcher) ⇒ Object
Registers a matcher to respond to PRIVMSGs that match a certain regex.
87 88 89 90 91 |
# File 'lib/imouto.rb', line 87 def register_matcher(regex, matcher) return false unless matcher.respond_to? 'call' @matchers[regex] = matcher log("[Registered Matcher] #{regex}") end |
#reply(msg, matcher, matches) ⇒ Object
Reply to a PRIVMSG
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/imouto.rb', line 54 def reply(msg, matcher, matches) log("[Executing Matcher] #{msg[:message]}") m = Message.new(msg[:message], matches, msg) begin reply = Thread.new { matcher.call(m) }.value rescue StandardError => e log("[Matcher Exception] #{e}") end reply_to = msg[:target].start_with?('#') ? msg[:target] : msg[:nick] queue_reply(reply_to, reply) end |
#start ⇒ Object
Starts the bot, spawning a read- and a write-thread
37 38 39 40 41 42 |
# File 'lib/imouto.rb', line 37 def start read_thread = Thread.new { read(@irc) } write_thread = Thread.new { dequeue_replies } read_thread.join write_thread.join end |
#unregister_matcher(regex) ⇒ Object
Consumes a regex, and removes the matcher that was registered with it.
94 95 96 97 |
# File 'lib/imouto.rb', line 94 def unregister_matcher(regex) @matchers.delete(regex) log("[Removed Matcher] #{regex}") end |