class Discorb::Role
Represents a role in the guild.
Attributes
@return [Discorb::Color] The color of the role.
@return [Discorb::Guild] The guild this role belongs to.
@return [Boolean] Whether the role is hoisted.
@return [Boolean] Whether the role is hoisted.
@return [Discorb::Snowflake] The ID of the role.
@return [Boolean] Whether the role is managed.
@return [Boolean] Whether the role is managed.
@return [Boolean] Whether the role is a default role.
@return [Boolean] Whether the role is a default role.
@return [String] The name of the role.
@return [Discorb::Permission] The permissions of the role.
@return [Integer] The position of the role.
Public Class Methods
@!visibility private
# File lib/discorb/role.rb, line 40 def initialize(client, guild, data) @client = client @guild = guild @data = {} _set_data(data) end
Public Instance Methods
Compares two roles by their position.
@param [Discorb::Role] other The role to compare to.
@return [Integer] -1 if the other role is higher, 0 if they are equal, 1 if the other role is lower.
# File lib/discorb/role.rb, line 54 def <=>(other) return false unless other.is_a?(Role) @position <=> other.position end
# File lib/discorb/role.rb, line 73 def color? @color != 0 end
Deletes the role.
@param [String] reason The reason for deleting the role.
# File lib/discorb/role.rb, line 127 def delete!(reason: nil) Async do @client.http.delete("/guilds/#{@guild_id}/roles/#{@id}", reason: reason).wait end end
Edits the role. @macro async @macro http @macro edit
@param [String] name The new name of the role. @param [Integer] position The new position of the role. @param [Discorb::Color] color The new color of the role. @param [Boolean] hoist Whether the role should be hoisted. @param [Boolean] mentionable Whether the role should be mentionable. @param [String] reason The reason for editing the role.
# File lib/discorb/role.rb, line 108 def edit(name: :unset, position: :unset, color: :unset, hoist: :unset, mentionable: :unset, reason: nil) Async do payload = {} payload[:name] = name if name != :unset payload[:position] = position if position != :unset payload[:color] = color.to_i if color != :unset payload[:hoist] = hoist if hoist != :unset payload[:mentionable] = mentionable if mentionable != :unset @client.http.patch("/guilds/#{@guild_id}/roles/#{@id}", payload, reason: reason).wait end end
# File lib/discorb/role.rb, line 77 def inspect "#<#{self.class} @#{@name} id=#{@id}>" end
# File lib/discorb/role.rb, line 69 def mention "<@&#{@id}>" end
Moves the role to a new position. @macro async @macro http
@param [Integer] position The new position. @param [String] reason The reason for moving the role.
# File lib/discorb/role.rb, line 89 def move(position, reason: nil) Async do @client.http.patch("/guilds/#{@guild_id}/roles", { id: @id, position: position }, reason: reason).wait end end
# File lib/discorb/role.rb, line 135 def tag Tag.new(@tags) end
Formats the role as a string.
@return [String] The formatted string.
# File lib/discorb/role.rb, line 65 def to_s "@#{@name}" end
Private Instance Methods
# File lib/discorb/role.rb, line 175 def _set_data(data) @id = Snowflake.new(data[:id]) @name = data[:name] @color = Color.new(data[:color]) @hoist = data[:hoist] @position = data[:position] @permissions = Permission.new(data[:permissions].to_i) @managed = data[:managed] @mentionable = data[:mentionable] @tags = data[:tags] || {} @guild.roles[@id] = self unless data[:no_cache] @data.update(data) end