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

Returns a new instance of 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.

Returns:

  • (Block)

    the current value of code



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

def code
  @code
end

#descriptionString (readonly)

The description for the command.

Returns:

  • (String)

    the current value of description



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

def description
  @description
end

#invokersArray

The invokers for the command.

Returns:

  • (Array)

    the current value of invokers



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

def invokers
  @invokers
end

#nameSymbol (readonly)

The name of the command.

Returns:

  • (Symbol)

    the current value of name



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

def name
  @name
end

#permissionsArray (readonly)

The permissions required to execute the command.

Returns:

  • (Array)

    the current value of permissions



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

def permissions
  @permissions
end

Instance Method Details

#invoke(event, args) ⇒ nil

Invokes the command.

Parameters:

  • event (Discordrb::Event)

    The Event object to pass to the command.

  • args (Array)

    The arguments to pass to the command.

Returns:

  • (nil)


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.

Parameters:

  • event (Discordrb::Event)

    The event to use to check.

Returns:



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