Module: Discordrb::PermissionCalculator

Included in:
Member
Defined in:
lib/discordrb/permissions.rb

Overview

Mixin to calculate resulting permissions from overrides etc.

Instance Method Summary collapse

Instance Method Details

#defined_permission?(action, channel = nil) ⇒ true, false

Checks whether this user has a particular permission defined (i.e. not implicit, through for example Manage Roles)

Examples:

Check if a member has the Manage Channels permission defined in the server.

has_manage_channels = member.defined_permission?(:manage_channels)

Parameters:

  • action (Symbol)

    The permission that should be checked. See also Discordrb::Permissions::FLAGS for a list.

  • channel (Channel, nil) (defaults to: nil)

    If channel overrides should be checked too, this channel specifies where the overrides should be checked.

Returns:

  • (true, false)

    whether or not this user has the permission defined.



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/discordrb/permissions.rb', line 156

def defined_permission?(action, channel = nil)
  # Get the permission the user's roles have
  role_permission = defined_role_permission?(action, channel)

  # Once we have checked the role permission, we have to check the channel overrides for the
  # specific user
  user_specific_override = permission_overwrite(action, channel, id) # Use the ID reader as members have no ID instance variable

  # Merge the two permissions - if an override is defined, it has to be allow, otherwise we only care about the role
  return role_permission unless user_specific_override

  user_specific_override == :allow
end

#permission?(action, channel = nil) ⇒ true, false

Checks whether this user can do the particular action, regardless of whether it has the permission defined, through for example being the server owner or having the Manage Roles permission

Examples:

Check if the bot can send messages to a specific channel in a server.

bot_profile = bot.profile.on(event.server)
can_send_messages = bot_profile.permission?(:send_messages, channel)

Parameters:

  • action (Symbol)

    The permission that should be checked. See also Discordrb::Permissions::FLAGS for a list.

  • channel (Channel, nil) (defaults to: nil)

    If channel overrides should be checked too, this channel specifies where the overrides should be checked.

Returns:

  • (true, false)

    whether or not this user has the permission.



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/discordrb/permissions.rb', line 135

def permission?(action, channel = nil)
  # If the member is the server owner, it irrevocably has all permissions.
  return true if owner?

  # First, check whether the user has Manage Roles defined.
  # (Coincidentally, Manage Permissions is the same permission as Manage Roles, and a
  # Manage Permissions deny overwrite will override Manage Roles, so we can just check for
  # Manage Roles once and call it a day.)
  return true if defined_permission?(:administrator, channel)

  # Otherwise, defer to defined_permission
  defined_permission?(action, channel)
end