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. I'm not sure what the unlabeled bits are reserved for.

{
  # 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                          # 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                         # 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
}.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 (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.


106
107
108
109
110
111
112
113
114
115
116
# File 'lib/discordrb/permissions.rb', line 106

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

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

  init_vars
end

Instance Attribute Details

#bitsObject

Returns the value of attribute bits


61
62
63
# File 'lib/discordrb/permissions.rb', line 61

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


85
86
87
88
89
90
91
92
93
# File 'lib/discordrb/permissions.rb', line 85

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


119
120
121
122
# File 'lib/discordrb/permissions.rb', line 119

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

#init_varsObject

Initialize the instance variables based on the bitset.


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

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