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?

Instance Attribute Details

#colorDiscorb::Color (readonly)

Returns The color of the role.

Returns:



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

def color
  @color
end

#custom_iconDiscorb::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

#emojiDiscorb::Emoji? (readonly)

Returns The emoji of the role.

Returns:



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

def emoji
  @emoji
end

#guildDiscorb::Guild (readonly)

Returns The guild this role belongs to.

Returns:



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

def guild
  @guild
end

#hoistBoolean (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

#idDiscorb::Snowflake (readonly)

Returns The ID of the role.

Returns:



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

def id
  @id
end

#managedBoolean (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

#mentionableBoolean (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

#nameString (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

#permissionsDiscorb::Permission (readonly)

Returns The permissions of the role.

Returns:



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

def permissions
  @permissions
end

#positionInteger (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.



71
72
73
74
75
# File 'lib/discorb/role.rb', line 71

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

  @position <=> other.position
end

#color?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/discorb/role.rb', line 90

def color?
  @color != 0
end

#delete(reason: nil) ⇒ Async::Task<void> Also known as: destroy

Deletes the role.

Parameters:

  • reason (String) (defaults to: nil)

    The reason for deleting the role.

Returns:

  • (Async::Task<void>)

    The task.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/discorb/role.rb', line 186

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

#edit(name: Discorb::Unset, position: Discorb::Unset, color: Discorb::Unset, hoist: Discorb::Unset, mentionable: Discorb::Unset, icon: Discorb::Unset, reason: nil) ⇒ Async::Task<void> Also known as: modify

Note:

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

Edits the role.

Parameters:

  • name (String) (defaults to: Discorb::Unset)

    The new name of the role.

  • position (Integer) (defaults to: Discorb::Unset)

    The new position of the role.

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

    The new color of the role.

  • hoist (Boolean) (defaults to: Discorb::Unset)

    Whether the role should be hoisted.

  • mentionable (Boolean) (defaults to: Discorb::Unset)

    Whether the role should be mentionable.

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

    The new icon or emoji of the role.

  • reason (String) (defaults to: nil)

    The reason for editing the role.

Returns:

  • (Async::Task<void>)

    The task.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/discorb/role.rb', line 139

def edit(
  name: Discorb::Unset,
  position: Discorb::Unset,
  color: Discorb::Unset,
  hoist: Discorb::Unset,
  mentionable: Discorb::Unset,
  icon: Discorb::Unset,
  reason: nil
)
  Async do
    payload = {}
    payload[:name] = name if name != Discorb::Unset
    payload[:position] = position if position != Discorb::Unset
    payload[:color] = color.to_i if color != Discorb::Unset
    payload[:hoist] = hoist if hoist != Discorb::Unset
    payload[:mentionable] = mentionable if mentionable != Discorb::Unset
    if icon != Discorb::Unset
      if icon.is_a?(Discorb::Image)
        payload[:icon] = icon.to_s
      else
        payload[:unicode_emoji] = icon.to_s
      end
    end
    @client
      .http
      .request(
        Route.new(
          "/guilds/#{@guild.id}/roles/#{@id}",
          "//guilds/:guild_id/roles/:role_id",
          :patch
        ),
        payload,
        audit_log_reason: reason
      )
      .wait
  end
end

#iconObject



60
61
62
# File 'lib/discorb/role.rb', line 60

def icon
  @custom_icon || @emoji
end

#inspectObject



94
95
96
# File 'lib/discorb/role.rb', line 94

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

#mentionObject



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

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

#move(position, reason: nil) ⇒ Async::Task<void>

Moves the role to a new position.

Parameters:

  • position (Integer)

    The new position.

  • reason (String) (defaults to: nil)

    The reason for moving the role.

Returns:

  • (Async::Task<void>)

    The task.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/discorb/role.rb', line 107

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

#tagObject Also known as: tags



205
206
207
# File 'lib/discorb/role.rb', line 205

def tag
  Tag.new(@tags)
end

#to_sString

Formats the role as a string.

Returns:

  • (String)

    The formatted string.



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

def to_s
  "@#{@name}"
end