Class: Discorb::Role

Inherits:
DiscordModel show all
Includes:
Comparable
Defined in:
lib/discorb/role.rb

Overview

Represents a role in the guild.

Defined Under Namespace

Classes: Tag

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DiscordModel

#==, #eql?, #hash

Instance Attribute Details

#color -> Discorb::Color (readonly)

Returns The color of the role.

Returns:



13
14
15
# File 'lib/discorb/role.rb', line 13

def color
  @color
end

#custom_icon -> Discorb::Asset? (readonly)

Returns The icon of the role.

Returns:



30
31
32
# File 'lib/discorb/role.rb', line 30

def custom_icon
  @custom_icon
end

#emoji -> Discorb::Emoji? (readonly)

Returns The emoji of the role.

Returns:



32
33
34
# File 'lib/discorb/role.rb', line 32

def emoji
  @emoji
end

#guild -> Discorb::Guild (readonly)

Returns The guild this role belongs to.

Returns:



19
20
21
# File 'lib/discorb/role.rb', line 19

def guild
  @guild
end

#hoist -> Boolean (readonly) Also known as: hoist?

Returns Whether the role is hoisted.

Returns:

  • (Boolean)

    Whether the role is hoisted.



21
22
23
# File 'lib/discorb/role.rb', line 21

def hoist
  @hoist
end

#id -> Discorb::Snowflake (readonly)

Returns The ID of the role.

Returns:



9
10
11
# File 'lib/discorb/role.rb', line 9

def id
  @id
end

#managed -> Boolean (readonly) Also known as: managed?

Returns Whether the role is managed.

Returns:

  • (Boolean)

    Whether the role is managed.



24
25
26
# File 'lib/discorb/role.rb', line 24

def managed
  @managed
end

#mentionable -> Boolean (readonly) Also known as: mentionable?

Returns Whether the role is a default role.

Returns:

  • (Boolean)

    Whether the role is a default role.



27
28
29
# File 'lib/discorb/role.rb', line 27

def mentionable
  @mentionable
end

#name -> String (readonly)

Returns The name of the role.

Returns:

  • (String)

    The name of the role.



11
12
13
# File 'lib/discorb/role.rb', line 11

def name
  @name
end

#permissions -> Discorb::Permission (readonly)

Returns The permissions of the role.

Returns:



15
16
17
# File 'lib/discorb/role.rb', line 15

def permissions
  @permissions
end

#position -> Integer (readonly)

Returns The position of the role.

Returns:

  • (Integer)

    The position of the role.



17
18
19
# File 'lib/discorb/role.rb', line 17

def position
  @position
end

Instance Method Details

#<=>(other) -> Integer

Compares two roles by their position.

Parameters:

Returns:

  • (Integer)

    -1 if the other role is higher, 0 if they are equal, 1 if the other role is lower.



64
65
66
67
68
# File 'lib/discorb/role.rb', line 64

def <=>(other)
  return false unless other.is_a?(Role)

  @position <=> other.position
end

#color? -> Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/discorb/role.rb', line 83

def color?
  @color != 0
end

#delete!(reason: nil) -> Object Also known as: destroy!

Deletes the role.

Parameters:

  • reason (String) (defaults to: nil)

    The reason for deleting the role.



145
146
147
148
149
# File 'lib/discorb/role.rb', line 145

def delete!(reason: nil)
  Async do
    @client.http.delete("/guilds/#{@guild.id}/roles/#{@id}", audit_log_reason: reason).wait
  end
end

#edit(name: :unset, position: :unset, color: :unset, hoist: :unset, mentionable: :unset, icon: :unset, reason: nil) -> Object Also known as: modify

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Note:

The arguments of this method are defaultly set to :unset. Specify value to set the value, if not don't specify or specify :unset.

Edits the role.

Parameters:

  • name (String) (defaults to: :unset)

    The new name of the role.

  • position (Integer) (defaults to: :unset)

    The new position of the role.

  • color (Discorb::Color) (defaults to: :unset)

    The new color of the role.

  • hoist (Boolean) (defaults to: :unset)

    Whether the role should be hoisted.

  • mentionable (Boolean) (defaults to: :unset)

    Whether the role should be mentionable.

  • icon (Discorb::Image, Discorb::UnicodeEmoji) (defaults to: :unset)

    The new icon or emoji of the role.

  • reason (String) (defaults to: nil)

    The reason for editing the role.

Raises:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/discorb/role.rb', line 119

def edit(name: :unset, position: :unset, color: :unset, hoist: :unset, mentionable: :unset, icon: :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
    if icon != :unset
      if icon.is_a?(Discorb::Image)
        payload[:icon] = icon.to_s
      else
        payload[:unicode_emoji] = icon.to_s
      end
    end
    @client.http.patch("/guilds/#{@guild.id}/roles/#{@id}", payload, audit_log_reason: reason).wait
  end
end

#icon -> Object



53
54
55
# File 'lib/discorb/role.rb', line 53

def icon
  @custom_icon || @emoji
end

#inspect -> Object



87
88
89
# File 'lib/discorb/role.rb', line 87

def inspect
  "#<#{self.class} @#{@name} id=#{@id}>"
end

#mention -> Object



79
80
81
# File 'lib/discorb/role.rb', line 79

def mention
  "<@&#{@id}>"
end

#move(position, reason: nil) -> Object

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Moves the role to a new position.

Parameters:

  • position (Integer)

    The new position.

  • reason (String) (defaults to: nil)

    The reason for moving the role.

Raises:



99
100
101
102
103
# File 'lib/discorb/role.rb', line 99

def move(position, reason: nil)
  Async do
    @client.http.patch("/guilds/#{@guild.id}/roles", { id: @id, position: position }, audit_log_reason: reason).wait
  end
end

#tag -> Object Also known as: tags



153
154
155
# File 'lib/discorb/role.rb', line 153

def tag
  Tag.new(@tags)
end

#to_s -> String

Formats the role as a string.

Returns:

  • (String)

    The formatted string.



75
76
77
# File 'lib/discorb/role.rb', line 75

def to_s
  "@#{@name}"
end