Class: Commandorobo::Command

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

Overview

Class that represents a command.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, code, permissions, description, invokers, bot) ⇒ Command



118
119
120
121
122
123
124
125
# File 'lib/commandorobo.rb', line 118

def initialize(name, code, permissions, description, invokers, bot)
    @name = name
    @code = code
    @permissions = permissions
    @description = description
    @invokers = invokers.nil? ? [name] : invokers
 @bot = bot
end

Instance Attribute Details

#codeBlock (readonly)

The code to execute when the command fires.



115
116
117
# File 'lib/commandorobo.rb', line 115

def code
  @code
end

#descriptionString (readonly)

The description for the command.



115
116
117
# File 'lib/commandorobo.rb', line 115

def description
  @description
end

#invokersArray

The invokers for the command.



115
116
117
# File 'lib/commandorobo.rb', line 115

def invokers
  @invokers
end

#nameSymbol (readonly)

The name of the command.



115
116
117
# File 'lib/commandorobo.rb', line 115

def name
  @name
end

#permissionsArray (readonly)

The permissions required to execute the command.



115
116
117
# File 'lib/commandorobo.rb', line 115

def permissions
  @permissions
end

Instance Method Details

#invoke(event, args) ⇒ nil

Invokes the command.



131
132
133
# File 'lib/commandorobo.rb', line 131

def invoke(event, args)
    @code.call(event, args)
end

#perm_check(event) ⇒ true, Commandorobo::NoPermission

Checks permissions for a command.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/commandorobo.rb', line 138

def perm_check(event)
 perms = {}
    @permissions.each do |p|
        if p == :bot_owner
            perms[p] = @bot.owners.include?(event.author.id)
        else
            perms[p] = event.author.permission?(p)
        end
    end
    if !perms.values.all?
        noperms = []
        perms.keys.each do |p|
            if !perms[p]
                noperms << p
            end
        end
        Commandorobo::NoPermission.new(noperms)
    else
        true
    end
end