Class: Discordrb::Permissions

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

Overview

List of permissions Discord uses

Constant Summary collapse

FLAGS =

This hash maps bit positions to logical permissions.

{
  # Bit => Permission # Value
  0 => :create_instant_invite,     # 1
  1 => :kick_members,              # 2
  2 => :ban_members,               # 4
  3 => :administrator,             # 8
  4 => :manage_channels,           # 16
  5 => :manage_server,             # 32
  6 => :add_reactions,             # 64
  7 => :view_audit_log,            # 128
  8 => :priority_speaker,          # 256
  9 => :stream,                    # 512
  10 => :read_messages,            # 1024
  11 => :send_messages,            # 2048
  12 => :send_tts_messages,        # 4096
  13 => :manage_messages,          # 8192
  14 => :embed_links,              # 16384
  15 => :attach_files,             # 32768
  16 => :read_message_history,     # 65536
  17 => :mention_everyone,         # 131072
  18 => :use_external_emoji,       # 262144
  19 => :view_server_insights,     # 524288
  20 => :connect,                  # 1048576
  21 => :speak,                    # 2097152
  22 => :mute_members,             # 4194304
  23 => :deafen_members,           # 8388608
  24 => :move_members,             # 16777216
  25 => :use_voice_activity,       # 33554432
  26 => :change_nickname,          # 67108864
  27 => :manage_nicknames,         # 134217728
  28 => :manage_roles,             # 268435456, also Manage Permissions
  29 => :manage_webhooks,          # 536870912
  30 => :manage_emojis,            # 1073741824, also Manage Stickers
  31 => :use_slash_commands,       # 2147483648
  32 => :request_to_speak,         # 4294967296
  33 => :manage_events,            # 8589934592
  34 => :manage_threads,           # 17179869184
  35 => :use_public_threads,       # 34359738368
  36 => :use_private_threads,      # 68719476736
  37 => :use_external_stickers,    # 137438953472
  38 => :send_messages_in_threads, # 274877906944
  39 => :use_embedded_activities,  # 549755813888
  40 => :moderate_members          # 1099511627776
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bits = 0, writer = nil) ⇒ Permissions

Create a new Permissions object either as a blank slate to add permissions to (for example for Channel#define_overwrite) or from existing bit data to read out.

Examples:

Create a permissions object that could allow/deny read messages, connect, and speak by setting flags

permission = Permissions.new
permission.can_read_messages = true
permission.can_connect = true
permission.can_speak = true

Create a permissions object that could allow/deny read messages, connect, and speak by an array of symbols

Permissions.new [:read_messages, :connect, :speak]

Parameters:

  • bits (String, Integer, Array<Symbol>) (defaults to: 0)

    The permission bits that should be set from the beginning, or an array of permission flag symbols

  • writer (RoleWriter) (defaults to: nil)

    The writer that should be used to update data when a permission is set.



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/discordrb/permissions.rb', line 116

def initialize(bits = 0, writer = nil)
  @writer = writer

  @bits = if bits.is_a? Array
            self.class.bits(bits)
          else
            bits.to_i
          end

  init_vars
end

Instance Attribute Details

#bitsObject

Returns the value of attribute bits.



71
72
73
# File 'lib/discordrb/permissions.rb', line 71

def bits
  @bits
end

Class Method Details

.bits(list) ⇒ Integer

Return the corresponding bits for an array of permission flag symbols. This is a class method that can be used to calculate bits instead of instancing a new Permissions object.

Examples:

Get the bits for permissions that could allow/deny read messages, connect, and speak

Permissions.bits [:read_messages, :connect, :speak] #=> 3146752

Parameters:

  • list (Array<Symbol>)

Returns:

  • (Integer)

    the computed permissions integer



95
96
97
98
99
100
101
102
103
# File 'lib/discordrb/permissions.rb', line 95

def self.bits(list)
  value = 0

  FLAGS.each do |position, flag|
    value += 2**position if list.include? flag
  end

  value
end

Instance Method Details

#==(other) ⇒ Object

Comparison based on permission bits



138
139
140
141
# File 'lib/discordrb/permissions.rb', line 138

def ==(other)
  false unless other.is_a? Discordrb::Permissions
  bits == other.bits
end

#defined_permissionsArray<Symbol>

Return an array of permission flag symbols for this class’s permissions

Examples:

Get the permissions for the bits “9”

permissions = Permissions.new(9)
permissions.defined_permissions #=> [:create_instant_invite, :administrator]

Returns:

  • (Array<Symbol>)

    the permissions



133
134
135
# File 'lib/discordrb/permissions.rb', line 133

def defined_permissions
  FLAGS.filter_map { |value, name| (@bits & (1 << value)).positive? ? name : nil }
end

#init_varsObject

Initialize the instance variables based on the bitset.



81
82
83
84
85
86
# File 'lib/discordrb/permissions.rb', line 81

def init_vars
  FLAGS.each do |position, flag|
    flag_set = ((@bits >> position) & 0x1) == 1
    instance_variable_set "@#{flag}", flag_set
  end
end