Class: Discordrb::Role

Inherits:
Object
  • Object
show all
Includes:
IDObject
Defined in:
lib/discordrb/data/role.rb

Overview

A Discord role that contains permissions and applies to certain users

Defined Under Namespace

Classes: RoleWriter

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



29
30
31
# File 'lib/discordrb/data/role.rb', line 29

def colour
  @colour
end

#hoisttrue, false



18
19
20
# File 'lib/discordrb/data/role.rb', line 18

def hoist
  @hoist
end

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



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

def managed
  @managed
end

#mentionabletrue, false Also known as: mentionable?



25
26
27
# File 'lib/discordrb/data/role.rb', line 25

def mentionable
  @mentionable
end

#nameString



12
13
14
# File 'lib/discordrb/data/role.rb', line 12

def name
  @name
end

#permissionsPermissions (readonly)



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

def permissions
  @permissions
end

#positionInteger (readonly)



33
34
35
# File 'lib/discordrb/data/role.rb', line 33

def position
  @position
end

#serverServer (readonly)



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

def server
  @server
end

Instance Method Details

#delete(reason = nil) ⇒ Object

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



169
170
171
172
# File 'lib/discordrb/data/role.rb', line 169

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

#inspectObject

The inspect method is overwritten to give more useful output



175
176
177
# File 'lib/discordrb/data/role.rb', line 175

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.



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

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

#mentionString



73
74
75
# File 'lib/discordrb/data/role.rb', line 73

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


143
144
145
146
# File 'lib/discordrb/data/role.rb', line 143

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.



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/discordrb/data/role.rb', line 152

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