Class: Blur::Script::Commands::Command
- Inherits:
-
Object
- Object
- Blur::Script::Commands::Command
- Defined in:
- library/blur/script/commands.rb
Constant Summary collapse
- DefaultOptions =
{ prefix: '.', hostmask: nil }
Instance Method Summary collapse
-
#initialize(triggers, options = {}, &block) ⇒ Command
constructor
A new instance of Command.
- #matches_trigger?(command) ⇒ Boolean protected
-
#received_message(user, channel, message) ⇒ Object
Called by the Commands module.
- #split_message(message) ⇒ Object protected
Constructor Details
#initialize(triggers, options = {}, &block) ⇒ Command
Returns a new instance of Command.
11 12 13 14 15 |
# File 'library/blur/script/commands.rb', line 11 def initialize triggers, = {}, &block @triggers = Array === triggers ? triggers : [triggers] @options = DefaultOptions.merge @block = block end |
Instance Method Details
#matches_trigger?(command) ⇒ Boolean (protected)
51 52 53 |
# File 'library/blur/script/commands.rb', line 51 def matches_trigger? command return @triggers.find{|trigger| trigger.to_s == command } end |
#received_message(user, channel, message) ⇒ Object
Called by the Commands module.
Calls the command block if the trigger matches the criteria.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'library/blur/script/commands.rb', line 20 def user, channel, prefix = @options[:prefix] # Return if the prefix don't match. return unless .start_with? prefix # Return if the hostmask don't match. # FIXME: Maybe use globbing instead of regular expressions? unless @options[:hostmask].nil? hostmask = "#{user.nick}!#{user.name}@#{user.host}" return unless hostmask =~ @options[:hostmask] end command, args = # Strip the prefix and compare the trigger name. if self.matches_trigger? command @block.call user, channel, args end end |
#split_message(message) ⇒ Object (protected)
44 45 46 47 48 49 |
# File 'library/blur/script/commands.rb', line 44 def prefix = @options[:prefix] command, args = [prefix.length..-1].split $;, 2 return command, args end |