Class: Discordrb::Role
- Inherits:
-
Object
- Object
- Discordrb::Role
- Includes:
- IDObject
- Defined in:
- lib/discordrb/data.rb
Overview
A Discord role that contains permissions and applies to certain users
Defined Under Namespace
Classes: RoleWriter
Instance Attribute Summary collapse
-
#colour ⇒ ColourRGB
(also: #color)
The role colour.
-
#hoist ⇒ true, false
Whether or not this role should be displayed separately from other users.
-
#managed ⇒ true, false
(also: #managed?)
readonly
Whether or not this role is managed by an integration or a bot.
-
#mentionable ⇒ true, false
(also: #mentionable?)
Whether this role can be mentioned using a role mention.
-
#name ⇒ String
This role's name ("new role" if it hasn't been changed).
-
#permissions ⇒ Permissions
readonly
This role's permissions.
-
#position ⇒ Integer
readonly
The position of this role in the hierarchy.
-
#server ⇒ Server
readonly
The server this role belongs to.
Attributes included from IDObject
Instance Method Summary collapse
-
#delete(reason = nil) ⇒ Object
Deletes this role.
-
#inspect ⇒ Object
The inspect method is overwritten to give more useful output.
-
#members ⇒ Array<Member>
(also: #users)
An array of members who have this role.
-
#mention ⇒ String
A string that will mention this role, if it is mentionable.
-
#packed=(packed, update_perms = true) ⇒ Object
Changes this role's permissions to a fixed bitfield.
-
#sort_above(other = nil) ⇒ Integer
(also: #move_above)
Moves this role above another role in the list.
Methods included from IDObject
#==, #creation_time, synthesise
Instance Attribute Details
#colour ⇒ ColourRGB Also known as: color
Returns the role colour.
915 916 917 |
# File 'lib/discordrb/data.rb', line 915 def colour @colour end |
#hoist ⇒ true, false
Returns whether or not this role should be displayed separately from other users.
904 905 906 |
# File 'lib/discordrb/data.rb', line 904 def hoist @hoist end |
#managed ⇒ true, false (readonly) Also known as: managed?
Returns whether or not this role is managed by an integration or a bot.
907 908 909 |
# File 'lib/discordrb/data.rb', line 907 def managed @managed end |
#mentionable ⇒ true, false Also known as: mentionable?
Returns whether this role can be mentioned using a role mention.
911 912 913 |
# File 'lib/discordrb/data.rb', line 911 def mentionable @mentionable end |
#name ⇒ String
Returns this role's name ("new role" if it hasn't been changed).
898 899 900 |
# File 'lib/discordrb/data.rb', line 898 def name @name end |
#permissions ⇒ Permissions (readonly)
Returns this role's permissions.
895 896 897 |
# File 'lib/discordrb/data.rb', line 895 def @permissions end |
#position ⇒ Integer (readonly)
Returns the position of this role in the hierarchy.
919 920 921 |
# File 'lib/discordrb/data.rb', line 919 def position @position end |
#server ⇒ Server (readonly)
Returns the server this role belongs to.
901 902 903 |
# File 'lib/discordrb/data.rb', line 901 def server @server end |
Instance Method Details
#delete(reason = nil) ⇒ Object
Deletes this role. This cannot be undone without recreating the role!
1055 1056 1057 1058 |
# File 'lib/discordrb/data.rb', line 1055 def delete(reason = nil) API::Server.delete_role(@bot.token, @server.id, @id, reason) @server.delete_role(@id) end |
#inspect ⇒ Object
The inspect method is overwritten to give more useful output
1061 1062 1063 |
# File 'lib/discordrb/data.rb', line 1061 def inspect "<Role name=#{@name} permissions=#{@permissions.inspect} hoist=#{@hoist} colour=#{@colour.inspect} server=#{@server.inspect}>" end |
#members ⇒ Array<Member> Also known as: users
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.
965 966 967 |
# File 'lib/discordrb/data.rb', line 965 def members @server.members.select { |m| m.role? self } end |
#mention ⇒ String
Returns a string that will mention this role, if it is mentionable.
959 960 961 |
# File 'lib/discordrb/data.rb', line 959 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://discordapp.com/developers/docs/topics/permissions.
1029 1030 1031 1032 |
# File 'lib/discordrb/data.rb', line 1029 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.
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 |
# File 'lib/discordrb/data.rb', line 1038 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 |