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_threads,       # 8589934592
  34 => :use_public_threads,   # 17179869184
  35 => :use_private_threads,  # 34359738368
  36 => :use_external_stickers # 68719476736
}.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.



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/discordrb/permissions.rb', line 112

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.



67
68
69
# File 'lib/discordrb/permissions.rb', line 67

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



91
92
93
94
95
96
97
98
99
# File 'lib/discordrb/permissions.rb', line 91

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



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

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



129
130
131
# File 'lib/discordrb/permissions.rb', line 129

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

#init_varsObject

Initialize the instance variables based on the bitset.



77
78
79
80
81
82
# File 'lib/discordrb/permissions.rb', line 77

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