class Discordrb::Permissions

List of permissions Discord uses

Constants

FLAGS

This hash maps bit positions to logical permissions. I'm not sure what the unlabeled bits are reserved for.

Attributes

bits[R]

Public Class Methods

bits(list) click to toggle source

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. @example Get the bits for permissions that could allow/deny read messages, connect, and speak

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

@param list [Array<Symbol>] @return [Integer] the computed permissions integer

# 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
new(bits = 0, writer = nil) click to toggle source

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.

@example 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

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

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

@param bits [Integer, Array<Symbol>] The permission bits that should be set from the beginning, or an array of permission flag symbols @param writer [RoleWriter] The writer that should be used to update data when a permission is set.

# 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

Public Instance Methods

==(other) click to toggle source

Comparison based on permission bits

# File lib/discordrb/permissions.rb, line 119
def ==(other)
  false unless other.is_a? Discordrb::Permissions
  bits == other.bits
end
bits=(bits) click to toggle source

Set the raw bitset of this permission object @param bits [Integer] A number whose binary representation is the desired bitset.

# File lib/discordrb/permissions.rb, line 65
def bits=(bits)
  @bits = bits
  init_vars
end
init_vars() click to toggle source

Initialize the instance variables based on the bitset.

# 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