Class: DiscordRDA::Permission

Inherits:
Object
  • Object
show all
Defined in:
lib/discord_rda/entity/value_objects.rb

Overview

Represents Discord permissions as a bitfield. Provides easy checking and manipulation of individual permissions.

Examples:

Checking permissions

perms = Permission.new(0x8) # Administrator
perms.administrator? # => true

Building permissions

perms = Permission.new
perms.add(:send_messages)
perms.add(:read_messages)
perms.value # => 0x400 | 0x800

Constant Summary collapse

BITS =

All permission bits with their names

{
  create_instant_invite: 1 << 0,
  kick_members: 1 << 1,
  ban_members: 1 << 2,
  administrator: 1 << 3,
  manage_channels: 1 << 4,
  manage_guild: 1 << 5,
  add_reactions: 1 << 6,
  view_audit_log: 1 << 7,
  priority_speaker: 1 << 8,
  stream: 1 << 9,
  view_channel: 1 << 10,
  send_messages: 1 << 11,
  send_tts_messages: 1 << 12,
  manage_messages: 1 << 13,
  embed_links: 1 << 14,
  attach_files: 1 << 15,
  read_message_history: 1 << 16,
  mention_everyone: 1 << 17,
  use_external_emojis: 1 << 18,
  view_guild_insights: 1 << 19,
  connect: 1 << 20,
  speak: 1 << 21,
  mute_members: 1 << 22,
  deafen_members: 1 << 23,
  move_members: 1 << 24,
  use_vad: 1 << 25,
  change_nickname: 1 << 26,
  manage_nicknames: 1 << 27,
  manage_roles: 1 << 28,
  manage_webhooks: 1 << 29,
  manage_emojis_and_stickers: 1 << 30,
  use_application_commands: 1 << 31,
  request_to_speak: 1 << 32,
  manage_events: 1 << 33,
  manage_threads: 1 << 34,
  create_public_threads: 1 << 35,
  create_private_threads: 1 << 36,
  use_external_stickers: 1 << 37,
  send_messages_in_threads: 1 << 38,
  use_embedded_activities: 1 << 39,
  moderate_members: 1 << 40,
  monetization_analytics: 1 << 41,
  use_soundboard: 1 << 42,
  create_expressions: 1 << 43,
  create_events: 1 << 44,
  use_external_sounds: 1 << 45,
  send_voice_messages: 1 << 46,
  send_polls: 1 << 49,
  use_external_apps: 1 << 50
}.freeze
NAMES =

All permission names

BITS.keys.freeze
ALL =

All permissions value

BITS.values.reduce(:|)
NONE =

None permissions value

0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = 0) ⇒ Permission

Initialize with a permission value

Parameters:

  • value (Integer, String) (defaults to: 0)

    Permission bitfield



107
108
109
# File 'lib/discord_rda/entity/value_objects.rb', line 107

def initialize(value = 0)
  @value = value.to_i
end

Instance Attribute Details

#valueInteger (readonly)

Returns The permission bitfield value.

Returns:

  • (Integer)

    The permission bitfield value



81
82
83
# File 'lib/discord_rda/entity/value_objects.rb', line 81

def value
  @value
end

Class Method Details

.bit(permission) ⇒ Integer

Get the bit value for a permission

Parameters:

  • permission (Symbol)

    Permission name

Returns:

  • (Integer)

    Bit value



93
94
95
# File 'lib/discord_rda/entity/value_objects.rb', line 93

def bit(permission)
  BITS[permission.to_sym] || 0
end

.from_array(permissions) ⇒ Permission

Create from an array of permission names

Parameters:

  • permissions (Array<Symbol>)

    Permission names

Returns:



100
101
102
# File 'lib/discord_rda/entity/value_objects.rb', line 100

def from_array(permissions)
  new(permissions.sum { |p| bit(p) })
end

.namesArray<Symbol>

Get all available permission names

Returns:

  • (Array<Symbol>)

    Permission names



86
87
88
# File 'lib/discord_rda/entity/value_objects.rb', line 86

def names
  NAMES
end

Instance Method Details

#==(other) ⇒ Boolean

Check equality

Parameters:

  • other (Object)

    Other object

Returns:

  • (Boolean)

    True if equal



239
240
241
# File 'lib/discord_rda/entity/value_objects.rb', line 239

def ==(other)
  other.is_a?(Permission) && @value == other.value
end

#add(permission) ⇒ Permission

Add a permission

Parameters:

  • permission (Symbol)

    Permission to add

Returns:



125
126
127
128
129
# File 'lib/discord_rda/entity/value_objects.rb', line 125

def add(permission)
  bit = BITS[permission.to_sym]
  @value |= bit if bit
  self
end

#administrator?Boolean

Check for administrator permission

Returns:

  • (Boolean)

    True if administrator



155
156
157
# File 'lib/discord_rda/entity/value_objects.rb', line 155

def administrator?
  (@value & BITS[:administrator]) == BITS[:administrator]
end

#difference(other) ⇒ Permission Also known as: -

Subtract another permission set

Parameters:

Returns:



199
200
201
# File 'lib/discord_rda/entity/value_objects.rb', line 199

def difference(other)
  self.class.new(@value & ~other.value)
end

#empty?Boolean

Check if no permissions are set

Returns:

  • (Boolean)

    True if empty



220
221
222
# File 'lib/discord_rda/entity/value_objects.rb', line 220

def empty?
  @value == 0
end

#grantedArray<Symbol>

Get all granted permissions as array of symbols

Returns:

  • (Array<Symbol>)

    Granted permissions



149
150
151
# File 'lib/discord_rda/entity/value_objects.rb', line 149

def granted
  BITS.keys.select { |name| has?(name) }
end

#has?(permission) ⇒ Boolean Also known as: include?

Check if a specific permission is granted

Parameters:

  • permission (Symbol)

    Permission to check

Returns:

  • (Boolean)

    True if permission is granted



114
115
116
117
118
119
# File 'lib/discord_rda/entity/value_objects.rb', line 114

def has?(permission)
  bit = BITS[permission.to_sym]
  return false unless bit

  administrator? || (@value & bit) == bit
end

#has_all?(*permissions) ⇒ Boolean

Check for multiple permissions

Parameters:

  • permissions (Array<Symbol>)

    Permissions to check

Returns:

  • (Boolean)

    True if all granted



162
163
164
# File 'lib/discord_rda/entity/value_objects.rb', line 162

def has_all?(*permissions)
  permissions.all? { |p| has?(p) }
end

#has_any?(*permissions) ⇒ Boolean

Check for any of multiple permissions

Parameters:

  • permissions (Array<Symbol>)

    Permissions to check

Returns:

  • (Boolean)

    True if any granted



169
170
171
# File 'lib/discord_rda/entity/value_objects.rb', line 169

def has_any?(*permissions)
  permissions.any? { |p| has?(p) }
end

#hashInteger

Hash code

Returns:

  • (Integer)

    Hash code



245
246
247
# File 'lib/discord_rda/entity/value_objects.rb', line 245

def hash
  @value.hash
end

#inspectString

Inspect

Returns:

  • (String)

    Inspect string



251
252
253
254
# File 'lib/discord_rda/entity/value_objects.rb', line 251

def inspect
  perms = granted.map(&:to_s).join(', ')
  "#<Permission #{perms}>"
end

#intersection(other) ⇒ Permission Also known as: &

Intersect with another permission set

Parameters:

Returns:



191
192
193
# File 'lib/discord_rda/entity/value_objects.rb', line 191

def intersection(other)
  self.class.new(@value & other.value)
end

#missing(*permissions) ⇒ Array<Symbol>

Missing permissions from a list

Parameters:

  • permissions (Array<Symbol>)

    Required permissions

Returns:

  • (Array<Symbol>)

    Missing permissions



176
177
178
# File 'lib/discord_rda/entity/value_objects.rb', line 176

def missing(*permissions)
  permissions.reject { |p| has?(p) }
end

#remove(permission) ⇒ Permission

Remove a permission

Parameters:

  • permission (Symbol)

    Permission to remove

Returns:



134
135
136
137
138
# File 'lib/discord_rda/entity/value_objects.rb', line 134

def remove(permission)
  bit = BITS[permission.to_sym]
  @value &= ~bit if bit
  self
end

#subset?(other) ⇒ Boolean

Check if this permission set is a subset of another

Parameters:

Returns:

  • (Boolean)

    True if this is subset of other



214
215
216
# File 'lib/discord_rda/entity/value_objects.rb', line 214

def subset?(other)
  other.superset?(self)
end

#superset?(other) ⇒ Boolean

Check if this permission set includes all of another

Parameters:

Returns:

  • (Boolean)

    True if this includes all of other



207
208
209
# File 'lib/discord_rda/entity/value_objects.rb', line 207

def superset?(other)
  (@value & other.value) == other.value
end

#to_iInteger

Convert to integer

Returns:

  • (Integer)

    Bitfield value



226
227
228
# File 'lib/discord_rda/entity/value_objects.rb', line 226

def to_i
  @value
end

#to_sString

Convert to string representation

Returns:

  • (String)

    String value



232
233
234
# File 'lib/discord_rda/entity/value_objects.rb', line 232

def to_s
  @value.to_s
end

#toggle(permission) ⇒ Permission

Toggle a permission

Parameters:

  • permission (Symbol)

    Permission to toggle

Returns:



143
144
145
# File 'lib/discord_rda/entity/value_objects.rb', line 143

def toggle(permission)
  has?(permission) ? remove(permission) : add(permission)
end

#union(other) ⇒ Permission Also known as: |

Combine with another permission set (union)

Parameters:

Returns:



183
184
185
# File 'lib/discord_rda/entity/value_objects.rb', line 183

def union(other)
  self.class.new(@value | other.value)
end