Sha256: 475b5a9544991a826cfd2084c4f91a0d07a35fd6f5228ed4479f7796f07370ea
Contents?: true
Size: 1.48 KB
Versions: 4
Compression:
Stored size: 1.48 KB
Contents
require "cognition/message" require "cognition/matcher" require "cognition/responder" require "cognition/plugins/base" require "cognition/plugins/default" module Cognition class Bot attr_accessor :plugins, :matchers def initialize # Default plugin, responds to "ping" with "PONG" and provides help text register(Cognition::Plugins::Default) end def process(msg, metadata = {}) if msg.respond_to? :command process_msg(msg) else process_string(msg.to_s, metadata) end end def register(klass) return false if plugin_names.include? klass.to_s plugins << klass.new(self) end def reset @matchers = [] @plugins = [] register(Cognition::Plugins::Default) end def plugin_names plugins.map { |p| p.class.name } end def help matchers.flat_map(&:help) end private def process_msg(msg) response = false matchers.each do |matcher| if matcher.attempt(msg) response = matcher.response break end end response ? response : not_found(msg.command) end def process_string(message, metadata = {}) process_msg(Cognition::Message.new(message.strip, metadata)) end def matchers plugins.flat_map(&:matchers).compact end def plugins @plugins ||= [] end def not_found(message) "No such command: #{message}\nUse 'help' for available commands!" end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
cognition-2.0.8 | lib/cognition/bot.rb |
cognition-2.0.7 | lib/cognition/bot.rb |
cognition-2.0.6 | lib/cognition/bot.rb |
cognition-2.0.5 | lib/cognition/bot.rb |