Class: Tkellem::TkellemBot::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/tkellem/tkellem_bot.rb

Defined Under Namespace

Classes: ArgumentError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(responder) ⇒ Command

Returns a new instance of Command.



85
86
87
88
# File 'lib/tkellem/tkellem_bot.rb', line 85

def initialize(responder)
  @responder = responder
  @opts = {}
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



24
25
26
# File 'lib/tkellem/tkellem_bot.rb', line 24

def args
  @args
end

#bouncerObject

Returns the value of attribute bouncer.



24
25
26
# File 'lib/tkellem/tkellem_bot.rb', line 24

def bouncer
  @bouncer
end

#connObject

Returns the value of attribute conn.



24
25
26
# File 'lib/tkellem/tkellem_bot.rb', line 24

def conn
  @conn
end

#optionsObject

Returns the value of attribute options.



24
25
26
# File 'lib/tkellem/tkellem_bot.rb', line 24

def options
  @options
end

#optsObject

Returns the value of attribute opts.



24
25
26
# File 'lib/tkellem/tkellem_bot.rb', line 24

def opts
  @opts
end

Class Method Details

.admin_only?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/tkellem/tkellem_bot.rb', line 50

def self.admin_only?
  true
end

.admin_option(name, *args) ⇒ Object



31
32
33
34
35
# File 'lib/tkellem/tkellem_bot.rb', line 31

def self.admin_option(name, *args)
  option(name, *args)
  @admin_onlies ||= []
  @admin_onlies << name
end

.admin_user?(user) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/tkellem/tkellem_bot.rb', line 103

def self.admin_user?(user)
  !user || user.admin?
end

.build_options(user, cmd = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tkellem/tkellem_bot.rb', line 54

def self.build_options(user, cmd = nil)
  OptionParser.new.tap do |options|
    @options.try(:each) { |opt_name,args|
      next if !admin_user?(user) && @admin_onlies.include?(opt_name)
      options.on(*args) { |v| cmd.opts[opt_name] = v }
    }
    resources = self.resources(name)
    options.banner = resources['banner'] if resources['banner']
    options.separator(resources['help']) if resources['help']
  end
end

.option(name, *args) ⇒ Object



26
27
28
29
# File 'lib/tkellem/tkellem_bot.rb', line 26

def self.option(name, *args)
  @options ||= {}
  @options[name] = args
end

.register(cmd_name) ⇒ Object



37
38
39
40
41
# File 'lib/tkellem/tkellem_bot.rb', line 37

def self.register(cmd_name)
  cattr_accessor :name
  self.name = cmd_name
  TkellemBot.commands[name.upcase] = self
end

.resources(name) ⇒ Object



43
44
45
46
# File 'lib/tkellem/tkellem_bot.rb', line 43

def self.resources(name)
  @resources ||= YAML.load_file(File.expand_path("../../../resources/bot_command_descriptions.yml", __FILE__))
  @resources[name.upcase] || {}
end

.run(args_arr, bouncer, conn, block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tkellem/tkellem_bot.rb', line 66

def self.run(args_arr, bouncer, conn, block)
  if admin_only? && !admin_user?(bouncer.try(:user))
    block.call "You can only run #{name} as an admin."
    return
  end
  cmd = self.new(block)

  cmd.args = args_arr
  cmd.bouncer = bouncer
  cmd.conn = conn

  cmd.options = build_options(bouncer.try(:user), cmd)
  cmd.options.parse!(args_arr)

  cmd.execute
rescue ArgumentError, OptionParser::InvalidOption => e
  cmd.respond e.to_s
end

Instance Method Details

#respond(text) ⇒ Object Also known as: r



98
99
100
# File 'lib/tkellem/tkellem_bot.rb', line 98

def respond(text)
  text.to_s.each_line { |l| @responder.call(l.chomp) }
end

#show_helpObject



94
95
96
# File 'lib/tkellem/tkellem_bot.rb', line 94

def show_help
  respond(options)
end

#userObject



90
91
92
# File 'lib/tkellem/tkellem_bot.rb', line 90

def user
  bouncer.try(:user)
end