Class: DiscordRDA::Permission
- Inherits:
-
Object
- Object
- DiscordRDA::Permission
- Defined in:
- lib/discord_rda/entity/value_objects.rb
Overview
Represents Discord permissions as a bitfield. Provides easy checking and manipulation of individual permissions.
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
-
#value ⇒ Integer
readonly
The permission bitfield value.
Class Method Summary collapse
-
.bit(permission) ⇒ Integer
Get the bit value for a permission.
-
.from_array(permissions) ⇒ Permission
Create from an array of permission names.
-
.names ⇒ Array<Symbol>
Get all available permission names.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check equality.
-
#add(permission) ⇒ Permission
Add a permission.
-
#administrator? ⇒ Boolean
Check for administrator permission.
-
#difference(other) ⇒ Permission
(also: #-)
Subtract another permission set.
-
#empty? ⇒ Boolean
Check if no permissions are set.
-
#granted ⇒ Array<Symbol>
Get all granted permissions as array of symbols.
-
#has?(permission) ⇒ Boolean
(also: #include?)
Check if a specific permission is granted.
-
#has_all?(*permissions) ⇒ Boolean
Check for multiple permissions.
-
#has_any?(*permissions) ⇒ Boolean
Check for any of multiple permissions.
-
#hash ⇒ Integer
Hash code.
-
#initialize(value = 0) ⇒ Permission
constructor
Initialize with a permission value.
-
#inspect ⇒ String
Inspect.
-
#intersection(other) ⇒ Permission
(also: #&)
Intersect with another permission set.
-
#missing(*permissions) ⇒ Array<Symbol>
Missing permissions from a list.
-
#remove(permission) ⇒ Permission
Remove a permission.
-
#subset?(other) ⇒ Boolean
Check if this permission set is a subset of another.
-
#superset?(other) ⇒ Boolean
Check if this permission set includes all of another.
-
#to_i ⇒ Integer
Convert to integer.
-
#to_s ⇒ String
Convert to string representation.
-
#toggle(permission) ⇒ Permission
Toggle a permission.
-
#union(other) ⇒ Permission
(also: #|)
Combine with another permission set (union).
Constructor Details
#initialize(value = 0) ⇒ Permission
Initialize with a permission value
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
#value ⇒ Integer (readonly)
Returns 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
93 94 95 |
# File 'lib/discord_rda/entity/value_objects.rb', line 93 def bit() BITS[.to_sym] || 0 end |
.from_array(permissions) ⇒ Permission
Create from an array of permission names
100 101 102 |
# File 'lib/discord_rda/entity/value_objects.rb', line 100 def from_array() new(.sum { |p| bit(p) }) end |
.names ⇒ Array<Symbol>
Get all available 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
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
125 126 127 128 129 |
# File 'lib/discord_rda/entity/value_objects.rb', line 125 def add() bit = BITS[.to_sym] @value |= bit if bit self end |
#administrator? ⇒ Boolean
Check for administrator permission
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
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
220 221 222 |
# File 'lib/discord_rda/entity/value_objects.rb', line 220 def empty? @value == 0 end |
#granted ⇒ Array<Symbol>
Get all granted permissions as array of symbols
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
114 115 116 117 118 119 |
# File 'lib/discord_rda/entity/value_objects.rb', line 114 def has?() bit = BITS[.to_sym] return false unless bit administrator? || (@value & bit) == bit end |
#has_all?(*permissions) ⇒ Boolean
Check for multiple permissions
162 163 164 |
# File 'lib/discord_rda/entity/value_objects.rb', line 162 def has_all?(*) .all? { |p| has?(p) } end |
#has_any?(*permissions) ⇒ Boolean
Check for any of multiple permissions
169 170 171 |
# File 'lib/discord_rda/entity/value_objects.rb', line 169 def has_any?(*) .any? { |p| has?(p) } end |
#hash ⇒ Integer
Hash code
245 246 247 |
# File 'lib/discord_rda/entity/value_objects.rb', line 245 def hash @value.hash end |
#inspect ⇒ String
Inspect
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
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
176 177 178 |
# File 'lib/discord_rda/entity/value_objects.rb', line 176 def missing(*) .reject { |p| has?(p) } end |
#remove(permission) ⇒ Permission
Remove a permission
134 135 136 137 138 |
# File 'lib/discord_rda/entity/value_objects.rb', line 134 def remove() bit = BITS[.to_sym] @value &= ~bit if bit self end |
#subset?(other) ⇒ Boolean
Check if this permission set is a subset of another
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
207 208 209 |
# File 'lib/discord_rda/entity/value_objects.rb', line 207 def superset?(other) (@value & other.value) == other.value end |
#to_i ⇒ Integer
Convert to integer
226 227 228 |
# File 'lib/discord_rda/entity/value_objects.rb', line 226 def to_i @value end |
#to_s ⇒ String
Convert to string representation
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
143 144 145 |
# File 'lib/discord_rda/entity/value_objects.rb', line 143 def toggle() has?() ? remove() : add() end |
#union(other) ⇒ Permission Also known as: |
Combine with another permission set (union)
183 184 185 |
# File 'lib/discord_rda/entity/value_objects.rb', line 183 def union(other) self.class.new(@value | other.value) end |