Module: OnyxCord::Commands::Bot::Permissions

Included in:
OnyxCord::Commands::Bot
Defined in:
lib/onyxcord/commands/bot/permissions.rb

Instance Method Summary collapse

Instance Method Details

#permission?(user, level, server) ⇒ true, false

Check if a user has permission to do something

Parameters:

  • user (User)

    The user to check

  • level (Integer)

    The minimum permission level the user should have (inclusive)

  • server (Server)

    The server on which to check

Returns:

  • (true, false)

    whether or not the user has the given permission



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/onyxcord/commands/bot/permissions.rb', line 26

def permission?(user, level, server)
  determined_level = if user.webhook? || server.nil?
                       0
                     else
                       user.roles.reduce(0) do |memo, role|
                         [@permissions[:roles][role.id] || 0, memo].max
                       end
                     end

  [@permissions[:users][user.id] || 0, determined_level].max >= level
end

#set_role_permission(id, level) ⇒ Object

Sets the permission level of a role - this applies to all users in the role

Parameters:

  • id (Integer)

    the ID of the role whose level to set

  • level (Integer)

    the level to set the permission to



16
17
18
# File 'lib/onyxcord/commands/bot/permissions.rb', line 16

def set_role_permission(id, level)
  @permissions[:roles][id] = level
end

#set_user_permission(id, level) ⇒ Object

Sets the permission level of a user

Parameters:

  • id (Integer)

    the ID of the user whose level to set

  • level (Integer)

    the level to set the permission to



8
9
10
# File 'lib/onyxcord/commands/bot/permissions.rb', line 8

def set_user_permission(id, level)
  @permissions[:users][id] = level
end