Class: Rubycord::Role

Inherits:
Object
  • Object
show all
Includes:
IDObject
Defined in:
lib/rubycord/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

Attributes included from IDObject

#id

Instance Method Summary collapse

Methods included from IDObject

#==, #creation_time, synthesise

Instance Attribute Details

#colourColourRGB Also known as: color

Returns the role colour.

Returns:



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

def colour
  @colour
end

#hoisttrue, false

Returns whether or not this role should be displayed separately from other users.

Returns:

  • (true, false)

    whether or not this role should be displayed separately from other users



16
17
18
# File 'lib/rubycord/data/role.rb', line 16

def hoist
  @hoist
end

#iconString?

Returns The icon hash for this role.

Returns:

  • (String, nil)

    The icon hash for this role.



34
35
36
# File 'lib/rubycord/data/role.rb', line 34

def icon
  @icon
end

#managedtrue, false (readonly) Also known as: managed?

Returns whether or not this role is managed by an integration or a bot.

Returns:

  • (true, false)

    whether or not this role is managed by an integration or a bot



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

def managed
  @managed
end

#mentionabletrue, false Also known as: mentionable?

Returns whether this role can be mentioned using a role mention.

Returns:

  • (true, false)

    whether this role can be mentioned using a role mention



23
24
25
# File 'lib/rubycord/data/role.rb', line 23

def mentionable
  @mentionable
end

#nameString

Returns this role's name ("new role" if it hasn't been changed).

Returns:

  • (String)

    this role's name ("new role" if it hasn't been changed)



10
11
12
# File 'lib/rubycord/data/role.rb', line 10

def name
  @name
end

#permissionsPermissions (readonly)

Returns this role's permissions.

Returns:



7
8
9
# File 'lib/rubycord/data/role.rb', line 7

def permissions
  @permissions
end

#positionInteger (readonly)

Returns the position of this role in the hierarchy.

Returns:

  • (Integer)

    the position of this role in the hierarchy



31
32
33
# File 'lib/rubycord/data/role.rb', line 31

def position
  @position
end

#serverServer (readonly)

Returns the server this role belongs to.

Returns:

  • (Server)

    the server this role belongs to



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

def server
  @server
end

#tagsTags? (readonly)

Returns The role tags.

Returns:

  • (Tags, nil)

    The role tags



37
38
39
# File 'lib/rubycord/data/role.rb', line 37

def tags
  @tags
end

Instance Method Details

#delete(reason = nil) ⇒ Object

Deletes this role. This cannot be undone without recreating the role!

Parameters:

  • reason (String) (defaults to: nil)

    the reason for this role's deletion



222
223
224
225
# File 'lib/rubycord/data/role.rb', line 222

def delete(reason = nil)
  API::Server.delete_role(@bot.token, @server.id, @id, reason)
  @server.delete_role(@id)
end

#icon_url(format = "webp") ⇒ String

Returns URL to the icon on Discord's CDN.

Parameters:

  • format ('webp', 'png', 'jpeg') (defaults to: "webp")

Returns:

  • (String)

    URL to the icon on Discord's CDN.



178
179
180
181
182
# File 'lib/rubycord/data/role.rb', line 178

def icon_url(format = "webp")
  return nil unless @icon

  Rubycord::API.role_icon_url(@id, @icon, format)
end

#inspectObject

The inspect method is overwritten to give more useful output



228
229
230
# File 'lib/rubycord/data/role.rb', line 228

def inspect
  "<Role name=#{@name} permissions=#{@permissions.inspect} hoist=#{@hoist} colour=#{@colour.inspect} server=#{@server.inspect} position=#{@position} mentionable=#{@mentionable}>"
end

#membersArray<Member> Also known as: users

Note:

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.

Returns:

  • (Array<Member>)

    an array of members who have this role.



117
118
119
# File 'lib/rubycord/data/role.rb', line 117

def members
  @server.members.select { |m| m.role? self }
end

#mentionString

Returns a string that will mention this role, if it is mentionable.

Returns:

  • (String)

    a string that will mention this role, if it is mentionable.



111
112
113
# File 'lib/rubycord/data/role.rb', line 111

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

#packed=(packed, 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.

Examples:

Remove all permissions from a role

role.packed = 0

Parameters:

  • packed (Integer)

    A bitfield with the desired permissions value.

  • update_perms (true, false) (defaults to: true)

    Whether the internal data should also be updated. This should always be true when calling externally.



196
197
198
199
# File 'lib/rubycord/data/role.rb', line 196

def packed=(packed, update_perms = true)
  update_role_data(permissions: packed)
  @permissions.bits = packed if update_perms
end

#sort_above(other = nil) ⇒ Integer Also known as: move_above

Moves this role above another role in the list.

Parameters:

  • other (Role, String, Integer, nil) (defaults to: nil)

    The role, or its ID, above which this role should be moved. If it is nil, the role will be moved above the @everyone role.

Returns:

  • (Integer)

    the new position of this role



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rubycord/data/role.rb', line 205

def sort_above(other = nil)
  other = @server.role(other.resolve_id) if other
  roles = @server.roles.sort_by(&:position)
  roles.delete_at(@position)

  index = other ? roles.index { |role| role.id == other.id } + 1 : 1
  roles.insert(index, self)

  updated_roles = roles.map.with_index { |role, position| {id: role.id, position: position} }
  @server.update_role_positions(updated_roles)
  index
end