class Discorb::Role

Represents a role in the guild.

Attributes

color[R]

@return [Discorb::Color] The color of the role.

guild[R]

@return [Discorb::Guild] The guild this role belongs to.

hoist[R]

@return [Boolean] Whether the role is hoisted.

hoist?[R]

@return [Boolean] Whether the role is hoisted.

id[R]

@return [Discorb::Snowflake] The ID of the role.

managed[R]

@return [Boolean] Whether the role is managed.

managed?[R]

@return [Boolean] Whether the role is managed.

mentionable[R]

@return [Boolean] Whether the role is a default role.

mentionable?[R]

@return [Boolean] Whether the role is a default role.

name[R]

@return [String] The name of the role.

permissions[R]

@return [Discorb::Permission] The permissions of the role.

position[R]

@return [Integer] The position of the role.

Public Class Methods

new(client, guild, data) click to toggle source

@!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

<=>(other) click to toggle source

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
color?() click to toggle source
# File lib/discorb/role.rb, line 73
def color?
  @color != 0
end
delete!(reason: nil) click to toggle source

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
Also aliased as: destroy!
destroy!(reason: nil)
Alias for: delete!
edit(name: :unset, position: :unset, color: :unset, hoist: :unset, mentionable: :unset, reason: nil) click to toggle source

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
Also aliased as: modify
inspect() click to toggle source
# File lib/discorb/role.rb, line 77
def inspect
  "#<#{self.class} @#{@name} id=#{@id}>"
end
mention() click to toggle source
# File lib/discorb/role.rb, line 69
def mention
  "<@&#{@id}>"
end
modify(name: :unset, position: :unset, color: :unset, hoist: :unset, mentionable: :unset, reason: nil)
Alias for: edit
move(position, reason: nil) click to toggle source

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
tag() click to toggle source
# File lib/discorb/role.rb, line 135
def tag
  Tag.new(@tags)
end
Also aliased as: tags
tags()
Alias for: tag
to_s() click to toggle source

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

_set_data(data) click to toggle source
# 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