Class: Discordrb::Member
- Includes:
- MemberAttributes, PermissionCalculator
- Defined in:
- lib/discordrb/data/member.rb
Overview
A member is a user on a server. It differs from regular users in that it has roles, voice statuses and things like that.
Instance Attribute Summary
Attributes included from MemberAttributes
#boosting_since, #joined_at, #nick, #roles, #server
Attributes inherited from User
#activities, #client_status, #status
Attributes included from UserAttributes
#avatar_id, #bot_account, #discriminator, #username
Attributes included from IDObject
Instance Method Summary collapse
-
#add_role(role, reason = nil) ⇒ Object
Adds one or more roles to this member.
-
#boosting? ⇒ true, false
If this user is a Nitro Booster of this server.
-
#colour ⇒ ColourRGB?
(also: #color)
The colour this member has.
-
#colour_role ⇒ Role?
(also: #color_role)
The role this member is basing their colour on.
-
#deaf ⇒ true, false
(also: #deafened?)
Whether this member is deafened server-wide.
-
#display_name ⇒ String
The name the user displays as (nickname if they have one, username otherwise).
-
#highest_role ⇒ Role
The highest role this member has.
-
#hoist_role ⇒ Role?
The role this member is being hoisted with.
-
#inspect ⇒ Object
Overwriting inspect for debug purposes.
-
#modify_roles(add, remove, reason = nil) ⇒ Object
Adds and removes roles from a member.
-
#mute ⇒ true, false
(also: #muted?)
Whether this member is muted server-wide.
- #nick=(nick) ⇒ Object (also: #nickname=)
-
#owner? ⇒ true, false
Whether this member is the server owner.
-
#remove_role(role, reason = nil) ⇒ Object
Removes one or more roles from this member.
-
#role?(role) ⇒ true, false
Whether this member has the specified role.
- #roles=(role) ⇒ Object
-
#self_deaf ⇒ true, false
(also: #self_deafened?)
Whether this member has deafened themselves.
-
#self_mute ⇒ true, false
(also: #self_muted?)
Whether this member has muted themselves.
-
#server_deafen ⇒ Object
Server deafens this member.
-
#server_mute ⇒ Object
Server mutes this member.
-
#server_undeafen ⇒ Object
Server undeafens this member.
-
#server_unmute ⇒ Object
Server unmutes this member.
-
#set_nick(nick, reason = nil) ⇒ Object
(also: #set_nickname)
Sets or resets this member's nickname.
-
#set_roles(role, reason = nil) ⇒ Object
Bulk sets a member's roles.
-
#voice_channel ⇒ Channel
The voice channel this member is in.
Methods included from PermissionCalculator
#defined_permission?, #permission?
Methods inherited from User
#await, #await!, #current_bot?, #dnd?, #game, #idle?, #offline?, #on, #online?, #pm, #send_file, #stream_type, #stream_url, #webhook?
Methods included from UserAttributes
#avatar_url, #distinct, #mention
Methods included from IDObject
#==, #creation_time, synthesise
Instance Method Details
#add_role(role, reason = nil) ⇒ Object
Adds one or more roles to this member.
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/discordrb/data/member.rb', line 128 def add_role(role, reason = nil) role_ids = role_id_array(role) if role_ids.count == 1 API::Server.add_member_role(@bot.token, @server.id, @user.id, role_ids[0], reason) else old_role_ids = @roles.map(&:id) new_role_ids = (old_role_ids + role_ids).uniq API::Server.update_member(@bot.token, @server.id, @user.id, roles: new_role_ids, reason: reason) end end |
#boosting? ⇒ true, false
Returns if this user is a Nitro Booster of this server.
79 80 81 |
# File 'lib/discordrb/data/member.rb', line 79 def boosting? !@boosting_since.nil? end |
#colour ⇒ ColourRGB? Also known as: color
Returns the colour this member has.
178 179 180 181 182 |
# File 'lib/discordrb/data/member.rb', line 178 def colour return nil unless colour_role colour_role.color end |
#colour_role ⇒ Role? Also known as: color_role
Returns the role this member is basing their colour on.
169 170 171 172 173 174 |
# File 'lib/discordrb/data/member.rb', line 169 def colour_role coloured_roles = @roles.select { |v| v.colour.combined.nonzero? } return nil if coloured_roles.empty? coloured_roles.max_by(&:position) end |
#deaf ⇒ true, false Also known as: deafened?
Returns whether this member is deafened server-wide.
32 33 34 |
# File 'lib/discordrb/data/member.rb', line 32 def deaf voice_state_attribute(:deaf) end |
#display_name ⇒ String
Returns the name the user displays as (nickname if they have one, username otherwise).
230 231 232 |
# File 'lib/discordrb/data/member.rb', line 230 def display_name nickname || username end |
#highest_role ⇒ Role
Returns the highest role this member has.
156 157 158 |
# File 'lib/discordrb/data/member.rb', line 156 def highest_role @roles.max_by(&:position) end |
#hoist_role ⇒ Role?
Returns the role this member is being hoisted with.
161 162 163 164 165 166 |
# File 'lib/discordrb/data/member.rb', line 161 def hoist_role hoisted_roles = @roles.select(&:hoist) return nil if hoisted_roles.empty? hoisted_roles.max_by(&:position) end |
#inspect ⇒ Object
Overwriting inspect for debug purposes
276 277 278 |
# File 'lib/discordrb/data/member.rb', line 276 def inspect "<Member user=#{@user.inspect} server=#{@server.inspect} joined_at=#{@joined_at} roles=#{@roles.inspect} voice_channel=#{@voice_channel.inspect} mute=#{@mute} deaf=#{@deaf} self_mute=#{@self_mute} self_deaf=#{@self_deaf}>" end |
#modify_roles(add, remove, reason = nil) ⇒ Object
Adds and removes roles from a member.
116 117 118 119 120 121 122 123 |
# File 'lib/discordrb/data/member.rb', line 116 def modify_roles(add, remove, reason = nil) add_role_ids = role_id_array(add) remove_role_ids = role_id_array(remove) old_role_ids = @roles.map(&:id) new_role_ids = (old_role_ids - remove_role_ids + add_role_ids).uniq API::Server.update_member(@bot.token, @server.id, @user.id, roles: new_role_ids, reason: reason) end |
#mute ⇒ true, false Also known as: muted?
Returns whether this member is muted server-wide.
27 28 29 |
# File 'lib/discordrb/data/member.rb', line 27 def mute voice_state_attribute(:mute) end |
#nick=(nick) ⇒ Object Also known as: nickname=
206 207 208 |
# File 'lib/discordrb/data/member.rb', line 206 def nick=(nick) set_nick(nick) end |
#owner? ⇒ true, false
Returns whether this member is the server owner.
84 85 86 |
# File 'lib/discordrb/data/member.rb', line 84 def owner? @server.owner == self end |
#remove_role(role, reason = nil) ⇒ Object
Removes one or more roles from this member.
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/discordrb/data/member.rb', line 143 def remove_role(role, reason = nil) role_ids = role_id_array(role) if role_ids.count == 1 API::Server.remove_member_role(@bot.token, @server.id, @user.id, role_ids[0], reason) else old_role_ids = @roles.map(&:id) new_role_ids = old_role_ids.reject { |i| role_ids.include?(i) } API::Server.update_member(@bot.token, @server.id, @user.id, roles: new_role_ids, reason: reason) end end |
#role?(role) ⇒ true, false
Returns whether this member has the specified role.
90 91 92 93 |
# File 'lib/discordrb/data/member.rb', line 90 def role?(role) role = role.resolve_id @roles.any? { |e| e.id == role } end |
#roles=(role) ⇒ Object
96 97 98 |
# File 'lib/discordrb/data/member.rb', line 96 def roles=(role) set_roles(role) end |
#self_deaf ⇒ true, false Also known as: self_deafened?
Returns whether this member has deafened themselves.
42 43 44 |
# File 'lib/discordrb/data/member.rb', line 42 def self_deaf voice_state_attribute(:self_deaf) end |
#self_mute ⇒ true, false Also known as: self_muted?
Returns whether this member has muted themselves.
37 38 39 |
# File 'lib/discordrb/data/member.rb', line 37 def self_mute voice_state_attribute(:self_mute) end |
#server_deafen ⇒ Object
Server deafens this member.
186 187 188 |
# File 'lib/discordrb/data/member.rb', line 186 def server_deafen API::Server.update_member(@bot.token, @server.id, @user.id, deaf: true) end |
#server_mute ⇒ Object
Server mutes this member.
196 197 198 |
# File 'lib/discordrb/data/member.rb', line 196 def server_mute API::Server.update_member(@bot.token, @server.id, @user.id, mute: true) end |
#server_undeafen ⇒ Object
Server undeafens this member.
191 192 193 |
# File 'lib/discordrb/data/member.rb', line 191 def server_undeafen API::Server.update_member(@bot.token, @server.id, @user.id, deaf: false) end |
#server_unmute ⇒ Object
Server unmutes this member.
201 202 203 |
# File 'lib/discordrb/data/member.rb', line 201 def server_unmute API::Server.update_member(@bot.token, @server.id, @user.id, mute: false) end |
#set_nick(nick, reason = nil) ⇒ Object Also known as: set_nickname
Sets or resets this member's nickname. Requires the Change Nickname permission for the bot itself and Manage Nicknames for other users.
216 217 218 219 220 221 222 223 224 225 |
# File 'lib/discordrb/data/member.rb', line 216 def set_nick(nick, reason = nil) # Discord uses the empty string to signify 'no nickname' so we convert nil into that nick ||= '' if @user.current_bot? API::User.change_own_nickname(@bot.token, @server.id, nick, reason) else API::Server.update_member(@bot.token, @server.id, @user.id, nick: nick, reason: nil) end end |
#set_roles(role, reason = nil) ⇒ Object
Bulk sets a member's roles.
103 104 105 106 |
# File 'lib/discordrb/data/member.rb', line 103 def set_roles(role, reason = nil) role_ids = role_id_array(role) API::Server.update_member(@bot.token, @server.id, @user.id, roles: role_ids, reason: reason) end |
#voice_channel ⇒ Channel
Returns the voice channel this member is in.
47 48 49 |
# File 'lib/discordrb/data/member.rb', line 47 def voice_channel voice_state_attribute(:voice_channel) end |