Class: Discordrb::Role
- Inherits:
-
Object
- Object
- Discordrb::Role
- Includes:
- IDObject
- Defined in:
- lib/discordrb/data/role.rb
Overview
A Discord role that contains permissions and applies to certain users
Defined Under Namespace
Classes: RoleWriter, Tags
Instance Attribute Summary collapse
-
#colour ⇒ ColourRGB
(also: #color)
The primary colour of this role.
-
#flags ⇒ Integer
readonly
The flags for this role.
-
#hoist ⇒ true, false
(also: #hoist?)
Whether or not this role should be displayed separately from other users.
-
#icon ⇒ String?
The icon hash for this role.
-
#managed ⇒ true, false
(also: #managed?)
readonly
Whether or not this role is managed by an integration or a bot.
-
#mentionable ⇒ true, false
(also: #mentionable?)
Whether this role can be mentioned using a role mention.
-
#name ⇒ String
This role's name ("new role" if it hasn't been changed).
-
#permissions ⇒ Permissions
readonly
This role's permissions.
-
#position ⇒ Integer
readonly
The position of this role in the hierarchy.
-
#secondary_colour ⇒ ColourRGB?
(also: #secondary_color)
The secondary colour of this role.
-
#server ⇒ Server
readonly
The server this role belongs to.
-
#tags ⇒ Tags?
readonly
The role tags.
-
#tertiary_colour ⇒ ColourRGB?
(also: #tertiary_color)
The tertiary colour of this role.
-
#unicode_emoji ⇒ String?
The unicode emoji of this role, or nil.
Attributes included from IDObject
Instance Method Summary collapse
-
#delete(reason = nil) ⇒ Object
Deletes this role.
-
#display_icon ⇒ String?
Get the icon that a role has displayed.
-
#display_icon=(icon) ⇒ Object
Set the icon this role is displaying.
-
#gradient? ⇒ true, false
Whether or not the role has a two-point gradient.
-
#holographic=(holographic) ⇒ Object
Sets whether the role colour should be a holographic style.
-
#holographic? ⇒ true, false
Whether or not the role is of the holographic style.
-
#icon_url(format = 'webp') ⇒ String
URL to the icon on Discord's CDN.
-
#inspect ⇒ Object
The inspect method is overwritten to give more useful output.
-
#members ⇒ Array<Member>
(also: #users)
An array of members who have this role.
-
#mention ⇒ String
A string that will mention this role, if it is mentionable.
-
#modify(name: :undef, mentionable: :undef, hoist: :undef, unicode_emoji: :undef, icon: :undef, display_icon: :undef, color: :undef, colour: :undef, secondary_color: :undef, secondary_colour: :undef, tertiary_color: :undef, tertiary_colour: :undef, permissions: :undef, reason: nil) {|builder| ... } ⇒ nil
Modify the properties of the role.
-
#move(bottom: nil, above: nil, below: nil, offset: 0, reason: nil) ⇒ Integer
Move the position of this role in the roles list.
-
#packed=(permissions, _update_perms = true) ⇒ Object
Changes this role's permissions to a fixed bitfield.
-
#sort_above(other = nil) ⇒ Integer
(also: #move_above)
deprecated
Deprecated.
Please migrate to using #move with the
aboveorbelowKWARGS. -
#update_colours(primary: :undef, secondary: :undef, tertiary: :undef, holographic: :undef, reason: nil) ⇒ Object
(also: #update_colors)
A rich interface designed to make working with role colours simple.
Methods included from IDObject
#==, #creation_time, synthesise
Instance Attribute Details
#colour ⇒ ColourRGB Also known as: color
Returns the primary colour of this role.
30 31 32 |
# File 'lib/discordrb/data/role.rb', line 30 def colour @colour end |
#flags ⇒ Integer (readonly)
Returns The flags for this role.
43 44 45 |
# File 'lib/discordrb/data/role.rb', line 43 def flags @flags end |
#hoist ⇒ true, false Also known as: hoist?
Returns whether or not this role should be displayed separately from other users.
18 19 20 |
# File 'lib/discordrb/data/role.rb', line 18 def hoist @hoist end |
#icon ⇒ String?
Returns The icon hash for this role.
37 38 39 |
# File 'lib/discordrb/data/role.rb', line 37 def icon @icon end |
#managed ⇒ true, false (readonly) Also known as: managed?
Returns whether or not this role is managed by an integration or a bot.
22 23 24 |
# File 'lib/discordrb/data/role.rb', line 22 def managed @managed end |
#mentionable ⇒ true, false Also known as: mentionable?
Returns whether this role can be mentioned using a role mention.
26 27 28 |
# File 'lib/discordrb/data/role.rb', line 26 def mentionable @mentionable end |
#name ⇒ String
Returns this role's name ("new role" if it hasn't been changed).
12 13 14 |
# File 'lib/discordrb/data/role.rb', line 12 def name @name end |
#permissions ⇒ Permissions (readonly)
Returns this role's permissions.
9 10 11 |
# File 'lib/discordrb/data/role.rb', line 9 def @permissions end |
#position ⇒ Integer (readonly)
Returns the position of this role in the hierarchy.
34 35 36 |
# File 'lib/discordrb/data/role.rb', line 34 def position @position end |
#secondary_colour ⇒ ColourRGB? Also known as: secondary_color
Returns the secondary colour of this role.
49 50 51 |
# File 'lib/discordrb/data/role.rb', line 49 def secondary_colour @secondary_colour end |
#server ⇒ Server (readonly)
Returns the server this role belongs to.
15 16 17 |
# File 'lib/discordrb/data/role.rb', line 15 def server @server end |
#tags ⇒ Tags? (readonly)
Returns The role tags.
40 41 42 |
# File 'lib/discordrb/data/role.rb', line 40 def @tags end |
#tertiary_colour ⇒ ColourRGB? Also known as: tertiary_color
Returns the tertiary colour of this role.
53 54 55 |
# File 'lib/discordrb/data/role.rb', line 53 def tertiary_colour @tertiary_colour end |
#unicode_emoji ⇒ String?
Returns The unicode emoji of this role, or nil.
46 47 48 |
# File 'lib/discordrb/data/role.rb', line 46 def unicode_emoji @unicode_emoji end |
Instance Method Details
#delete(reason = nil) ⇒ Object
Deletes this role. This cannot be undone without recreating the role!
290 291 292 293 |
# File 'lib/discordrb/data/role.rb', line 290 def delete(reason = nil) API::Server.delete_role(@bot.token, @server.id, @id, reason) @server.delete_role(@id) end |
#display_icon ⇒ String?
A role can have a unicode emoji, and an icon, but only the icon will be shown in the UI.
Get the icon that a role has displayed.
236 237 238 |
# File 'lib/discordrb/data/role.rb', line 236 def display_icon icon_url || unicode_emoji end |
#display_icon=(icon) ⇒ Object
Setting the icon to nil will remove the unicode emoji and the custom icon.
Set the icon this role is displaying.
243 244 245 |
# File 'lib/discordrb/data/role.rb', line 243 def display_icon=(icon) modify(display_icon: icon) end |
#gradient? ⇒ true, false
Whether or not the role has a two-point gradient.
255 256 257 |
# File 'lib/discordrb/data/role.rb', line 255 def gradient? !@secondary_colour.nil? && @tertiary_colour.nil? end |
#holographic=(holographic) ⇒ Object
Sets whether the role colour should be a holographic style.
211 212 213 |
# File 'lib/discordrb/data/role.rb', line 211 def holographic=(holographic) update_colours(holographic: holographic) end |
#holographic? ⇒ true, false
Whether or not the role is of the holographic style.
249 250 251 |
# File 'lib/discordrb/data/role.rb', line 249 def holographic? !@tertiary_colour.nil? end |
#icon_url(format = 'webp') ⇒ String
Returns URL to the icon on Discord's CDN.
229 230 231 |
# File 'lib/discordrb/data/role.rb', line 229 def icon_url(format = 'webp') API.role_icon_url(@id, @icon, format) if @icon end |
#inspect ⇒ Object
The inspect method is overwritten to give more useful output
445 446 447 |
# File 'lib/discordrb/data/role.rb', line 445 def inspect "<Role name=#{@name} permissions=#{@permissions.inspect} hoist=#{@hoist} colour=#{@colour.inspect} server=#{@server.inspect} position=#{@position} mentionable=#{@mentionable} unicode_emoji=#{@unicode_emoji} flags=#{@flags}>" end |
#members ⇒ Array<Member> Also known as: users
This requests a member chunk if it hasn't for the server before, which may be slow initially
Returns an array of members who have this role.
147 148 149 |
# File 'lib/discordrb/data/role.rb', line 147 def members @server.members.select { |m| m.role? self } end |
#mention ⇒ String
Returns a string that will mention this role, if it is mentionable.
141 142 143 |
# File 'lib/discordrb/data/role.rb', line 141 def mention "<@&#{@id}>" end |
#modify(name: :undef, mentionable: :undef, hoist: :undef, unicode_emoji: :undef, icon: :undef, display_icon: :undef, color: :undef, colour: :undef, secondary_color: :undef, secondary_colour: :undef, tertiary_color: :undef, tertiary_colour: :undef, permissions: :undef, reason: nil) {|builder| ... } ⇒ nil
Modify the properties of the role.
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/discordrb/data/role.rb', line 390 def modify( name: :undef, mentionable: :undef, hoist: :undef, unicode_emoji: :undef, icon: :undef, display_icon: :undef, color: :undef, colour: :undef, secondary_color: :undef, secondary_colour: :undef, tertiary_color: :undef, tertiary_colour: :undef, permissions: :undef, reason: nil ) if display_icon != :undef if icon != :undef || unicode_emoji != :undef raise ArgumentError, "'display_icon' is mutually exclusive with 'icon' and 'unicode_emoji'" end if display_icon.nil? icon = nil unicode_emoji = nil elsif display_icon.is_a?(String) icon = nil unicode_emoji = display_icon elsif display_icon.respond_to?(:read) icon = display_icon unicode_emoji = nil end end if block_given? && == :undef yield((builder = Permissions.new(@permissions.bits))) = builder.bits end data = { name: name, mentionable: mentionable, hoist: hoist, unicode_emoji: unicode_emoji, icon: icon.respond_to?(:read) ? Discordrb.encode64(icon) : icon, permissions: == :undef ? : &.to_s, reason: reason } color = (colour == :undef ? color : colour) tertiary = (tertiary_colour == :undef ? tertiary_color : tertiary_colour) secondary = (secondary_colour == :undef ? secondary_color : secondary_colour) if color != :undef || secondary != :undef || tertiary != :undef data[:colors] = { primary_color: (color == :undef ? @colour : color)&.to_i, tertiary_color: (tertiary == :undef ? @tertiary_colour : tertiary)&.to_i, secondary_color: (secondary == :undef ? @secondary_colour : secondary)&.to_i } end update_data(JSON.parse(API::Server.update_role!(@bot.token, @server.id, @id, **data))) nil end |
#move(bottom: nil, above: nil, below: nil, offset: 0, reason: nil) ⇒ Integer
Move the position of this role in the roles list.
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/discordrb/data/role.rb', line 310 def move(bottom: nil, above: nil, below: nil, offset: 0, reason: nil) if [bottom, above, below].count(&:itself) > 1 raise ArgumentError, "'bottom', 'above', and 'below' are mutually exclusive" end if (above || below) && !(target = @server.role(above || below)) raise ArgumentError, "The given 'above' or 'below' options are not valid" end if (below && target&.id == @server.id) || (@id == target&.id) raise ArgumentError, 'The target role that was provded is not valid' end roles = @server.roles.sort_by { |role| [role.position, role.id] } # Make sure we remove the current role. myself = roles.rindex(@id).tap { |index| roles.delete_at(index) } index = if bottom 1 elsif below roles.rindex(target) elsif above roles.rindex(target) + 1 else myself end roles.insert([index + (offset || 0), 1].max, self) roles = roles.map.with_index do |role, new_position| { id: role.resolve_id, position: new_position } end @server.update_role_positions(roles, reason: reason) @position end |
#packed=(permissions, _update_perms = true) ⇒ Object
Changes this role's permissions to a fixed bitfield. This allows setting multiple permissions at once with just one API call.
Information on how this bitfield is structured can be found at https://discord.com/developers/docs/topics/permissions.
273 274 275 |
# File 'lib/discordrb/data/role.rb', line 273 def packed=(, _update_perms = true) modify(permissions: ) end |
#sort_above(other = nil) ⇒ Integer Also known as: move_above
Please migrate to using #move with the above or below KWARGS.
Moves this role above another role in the list.
282 283 284 |
# File 'lib/discordrb/data/role.rb', line 282 def sort_above(other = nil) other ? move(above: other) : move(bottom: true) end |
#update_colours(primary: :undef, secondary: :undef, tertiary: :undef, holographic: :undef, reason: nil) ⇒ Object Also known as: update_colors
A rich interface designed to make working with role colours simple.
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/discordrb/data/role.rb', line 355 def update_colours(primary: :undef, secondary: :undef, tertiary: :undef, holographic: :undef, reason: nil) colours = { color: (primary == :undef ? @colour : primary)&.to_i, tertiary_color: (tertiary == :undef ? @tertiary_colour : tertiary)&.to_i, secondary_color: (secondary == :undef ? @secondary_colour : secondary)&.to_i } holographic_colours = { color: 11_127_295, tertiary_color: 16_761_760, secondary_color: 16_759_788 } # Only set the tertiary_color to `nil` if holographic is explicitly set to false. (colours[:tertiary_color] = nil) if holographic.is_a?(FalseClass) modify(reason: reason, **(holographic ? holographic_colours : colours)) end |