class Lita::Handlers::Help

Provides online help about Lita commands for users.

Public Instance Methods

help(response) click to toggle source

Outputs help information about Lita commands. @param response [Lita::Response] The response object. @return [void]

# File lib/lita/handlers/help.rb, line 14
def help(response)
  output = build_help(response)
  output = filter_help(output, response)
  response.reply_privately output.join("\n")
end

Private Instance Methods

authorized?(user, required_groups) click to toggle source

Checks if the user is authorized to at least one of the given groups.

# File lib/lita/handlers/help.rb, line 23
def authorized?(user, required_groups)
  required_groups.nil? || required_groups.any? do |group|
    robot.auth.user_in_group?(user, group)
  end
end
build_help(response) click to toggle source

Creates an array of help info for all registered routes.

# File lib/lita/handlers/help.rb, line 30
def build_help(response)
  robot.handlers.map do |handler|
    next unless handler.respond_to?(:routes)

    handler.routes.map do |route|
      route.help.map do |command, description|
        if authorized?(response.user, route.required_groups)
          help_command(route, command, description)
        end
      end
    end
  end.flatten.compact
end
filter_help(output, response) click to toggle source

Filters the help output by an optional command.

# File lib/lita/handlers/help.rb, line 45
def filter_help(output, response)
  filter = response.matches[0][0]

  if filter
    output.select { |line| /(?:@?#{name}[:,]?)?#{filter}/i === line }
  else
    output
  end
end
help_command(route, command, description) click to toggle source

Formats an individual command's help message.

# File lib/lita/handlers/help.rb, line 56
def help_command(route, command, description)
  command = "#{name}: #{command}" if route.command?
  "#{command} - #{description}"
end
name() click to toggle source

The way the bot should be addressed in order to trigger a command.

# File lib/lita/handlers/help.rb, line 62
def name
  robot.config.robot.mention_name || robot.config.robot.name
end