Class: Rubot::Core::Command

Inherits:
Object show all
Defined in:
lib/core/command.rb

Overview

Base class that handles the dirty work for IRC commands.

Direct Known Subclasses

Auth, Help, Join, Msg, Part, Quit, Reload, UpTime

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher) ⇒ Command

Takes an instance of Rubot::Core::Dispatcher. Any child class that needs a constructor should override this method.

Parameters

dispatcher<Rubot::Core::Dispatcher>

The dispatcher that was used to create

the instance of the command.



17
18
19
# File 'lib/core/command.rb', line 17

def initialize(dispatcher)
  @dispatcher = dispatcher
end

Instance Method Details

#protected?Boolean

Whether the command is marked as protected or not (using Command::acts_as_protected).

Returns:

  • (Boolean)


47
48
49
# File 'lib/core/command.rb', line 47

def protected?
  false
end

#run(server, message) ⇒ Object

Runs the command with the given server and message.

Parameters

server<Rubot::Irc::Server>

Server instance the command should use for

messaging and information.

message<Rubot::Irc::Message>

The message that invoked the command.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/core/command.rb', line 27

def run(server, message)
  if protected? && !message.authenticated
    server.msg(message.destination, "unauthorized")
    return
  end
  
  args = Shellwords.shellwords(message.body.gsub(/(')/n, "\\\\\'"))
  options = parse(args)
    
  if options.help
    # this to_s.lines.to_a business is a hack to not display the -h, --help line
    # there's got to be an easier/better way to do this. but it'll work for now
    server.msg(message.destination, options.help.to_s.lines.to_a[0..-2])
  else
    message.body = args.join(" ")
    execute(server, message, options)
  end
end