Module: Cinch::Plugin::ClassMethods
- Defined in:
- lib/cinch/plugin.rb
Defined Under Namespace
Instance Method Summary collapse
- #__plugin_name ⇒ String private
- #__register_with_bot(bot, instance) ⇒ void private
- #ctcp(command) ⇒ Object
-
#help(message) ⇒ void
Define a help message which will be returned on "
help ". -
#listen_to(*types, options = {}) ⇒ void
Events to listen to.
-
#match(pattern, options = {}) ⇒ void
Set a match pattern.
-
#plugin(name) ⇒ void
Define the plugin name.
-
#prefix(prefix) ⇒ void
Set the plugin prefix.
-
#react_on(target) ⇒ void
Set which kind of messages to react on (i.e. call #execute).
Instance Method Details
#__plugin_name ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
92 93 94 |
# File 'lib/cinch/plugin.rb', line 92 def __plugin_name @__cinch_name || self.name.split("::").last.downcase end |
#__register_with_bot(bot, instance) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/cinch/plugin.rb', line 98 def __register_with_bot(bot, instance) plugin_name = __plugin_name (@__cinch_listeners || []).each do |listener| bot.debug "[plugin] #{plugin_name}: Registering listener for type `#{listener.event}`" bot.on(listener.event, [], instance) do |, plugin| plugin.__send__(listener.method, ) if plugin.respond_to?(listener.method) end end if (@__cinch_patterns ||= []).empty? @__cinch_patterns << Pattern.new(plugin_name, true, nil) end prefix = @__cinch_prefix || bot.config.plugins.prefix if prefix.is_a?(String) prefix = Regexp.escape(prefix) end @__cinch_patterns.each do |pattern| pattern_to_register = nil if pattern.use_prefix && prefix case pattern.pattern when Regexp pattern_to_register = /^#{prefix}#{pattern.pattern}/ when String pattern_to_register = prefix + pattern.pattern end else pattern_to_register = pattern.pattern end react_on = @__cinch_react_on || :message bot.debug "[plugin] #{plugin_name}: Registering executor with pattern `#{pattern_to_register}`, reacting on `#{react_on}`" bot.on(react_on, pattern_to_register, instance, pattern) do |, plugin, pattern, *args| if plugin.respond_to?(pattern.method) method = plugin.method(pattern.method) arity = method.arity - 1 if arity > 0 args = args[0..arity - 1] elsif arity == 0 args = [] end method.call(, *args) end end end (@__cinch_ctcps || []).each do |ctcp| bot.debug "[plugin] #{plugin_name}: Registering CTCP `#{ctcp}`" bot.on(:ctcp, ctcp, instance, ctcp) do |, plugin, ctcp, *args| plugin.__send__("ctcp_#{ctcp.downcase}", , *args) end end if bot.debug "[plugin] #{plugin_name}: Registering help message" bot.on(:message, /#{prefix}help #{Regexp.escape(plugin_name)}/, ) do |, | .reply() end end end |
#ctcp(command) ⇒ Object
52 53 54 |
# File 'lib/cinch/plugin.rb', line 52 def ctcp(command) (@__cinch_ctcps ||= []) << command.to_s.upcase end |
#help(message) ⇒ void
This method returns an undefined value.
Define a help message which will be returned on "
61 62 63 |
# File 'lib/cinch/plugin.rb', line 61 def help() = end |
#listen_to(*types, options = {}) ⇒ void
Events to listen to.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cinch/plugin.rb', line 39 def listen_to(*types) = {:method => :listen} if types.last.is_a?(Hash) .merge!(types.pop) end @__cinch_listeners ||= [] types.each do |type| @__cinch_listeners << Listener.new(type, [:method]) end end |
#match(pattern, options = {}) ⇒ void
This method returns an undefined value.
Set a match pattern.
17 18 19 20 21 |
# File 'lib/cinch/plugin.rb', line 17 def match(pattern, = {}) = {:use_prefix => true, :method => :execute}.merge() @__cinch_patterns ||= [] @__cinch_patterns << Pattern.new(pattern, [:use_prefix], [:method]) end |
#plugin(name) ⇒ void
This method returns an undefined value.
Define the plugin name.
86 87 88 |
# File 'lib/cinch/plugin.rb', line 86 def plugin(name) @__cinch_name = name end |
#prefix(prefix) ⇒ void
This method returns an undefined value.
Set the plugin prefix.
69 70 71 |
# File 'lib/cinch/plugin.rb', line 69 def prefix(prefix) @__cinch_prefix = prefix end |
#react_on(target) ⇒ void
This method returns an undefined value.
Set which kind of messages to react on (i.e. call Cinch::Plugin#execute)
78 79 80 |
# File 'lib/cinch/plugin.rb', line 78 def react_on(target) @__cinch_react_on = target end |