class Discordrb::Permissions
List of permissions Discord uses
Constants
- FLAGS
This hash maps bit positions to logical permissions.
Attributes
Public Class Methods
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 95 def self.bits(list) value = 0 FLAGS.each do |position, flag| value += 2**position if list.include? flag end value end
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 [String, 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 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
Public Instance Methods
Comparison based on permission bits
# File lib/discordrb/permissions.rb, line 138 def ==(other) false unless other.is_a? Discordrb::Permissions bits == other.bits end
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 75 def bits=(bits) @bits = bits init_vars end
Return an array of permission flag symbols for this class’s permissions @example Get the permissions for the bits “9”
permissions = Permissions.new(9) permissions.defined_permissions #=> [:create_instant_invite, :administrator]
@return [Array<Symbol>] the permissions
# File lib/discordrb/permissions.rb, line 133 def defined_permissions FLAGS.filter_map { |value, name| (@bits & (1 << value)).positive? ? name : nil } end
Initialize the instance variables based on the bitset.
# 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