Class: Discordrb::Member

Inherits:
User
  • Object
show all
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.

Constant Summary

Constants included from MemberAttributes

Discordrb::MemberAttributes::MEMBER_FLAGS

Constants included from UserAttributes

UserAttributes::FLAGS

Instance Attribute Summary

Attributes included from MemberAttributes

#boosting_since, #communication_disabled_until, #flags, #joined_at, #nick, #pending, #server_avatar_decoration, #server_avatar_id, #server_banner_id

Attributes inherited from User

#activities, #client_status, #status

Attributes included from UserAttributes

#avatar_decoration, #avatar_id, #bot_account, #collectibles, #discriminator, #global_name, #primary_server, #public_flags, #system_account, #username, #webhook_account

Attributes included from IDObject

#id

Instance Method Summary collapse

Methods included from PermissionCalculator

#defined_permission?, #permission?

Methods included from MemberAttributes

#server_avatar_url, #server_banner_url

Methods inherited from User

#await, #await!, #banner_id, #current_bot?, #dnd?, #game, #idle?, #offline?, #on, #online?, #pm, #send_file, #stream_type, #stream_url

Methods included from UserAttributes

#avatar_url, #banner_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.

Parameters:



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/discordrb/data/member.rb', line 229

def add_role(role, reason = nil)
  role_ids = role_id_array(role)

  if role_ids.one?
    API::Server.add_member_role(@bot.token, @server_id, @user.id, role_ids[0], reason)
  else
    old_role_ids = resolve_role_ids
    new_role_ids = (old_role_ids + role_ids).uniq
    update_member_data(roles: new_role_ids, reason: reason)
  end
end

#ban(message_days = 0, message_seconds: nil, reason: nil) ⇒ Object

Bans this member from the server.

Parameters:

  • message_days (Integer) (defaults to: 0)

    How many days worth of messages sent by the member should be deleted. This parameter is deprecated and will be removed in 4.0.

  • message_seconds (Integer) (defaults to: nil)

    How many seconds worth of messages sent by the member should be deleted.

  • reason (String) (defaults to: nil)

    The reason this member is being banned.



322
323
324
# File 'lib/discordrb/data/member.rb', line 322

def ban(message_days = 0, message_seconds: nil, reason: nil)
  server.ban(@user, message_days, message_seconds: message_seconds, reason: reason)
end

#boosting?true, false

Returns if this user is a Nitro Booster of this server.

Returns:

  • (true, false)

    if this user is a Nitro Booster of this server.



162
163
164
# File 'lib/discordrb/data/member.rb', line 162

def boosting?
  !@boosting_since.nil?
end

#colourColourRGB? Also known as: color

Returns the colour this member has.

Returns:

  • (ColourRGB, nil)

    the colour this member has.



280
281
282
283
284
# File 'lib/discordrb/data/member.rb', line 280

def colour
  return nil unless colour_role

  colour_role.color
end

#colour_roleRole? Also known as: color_role

Returns the role this member is basing their colour on.

Returns:

  • (Role, nil)

    the role this member is basing their colour on.



270
271
272
273
274
275
# File 'lib/discordrb/data/member.rb', line 270

def colour_role
  coloured_roles = roles.select { |v| v.colour.combined.nonzero? }
  return nil if coloured_roles.empty?

  coloured_roles.max_by(&:position)
end

#communication_disabled?true, false Also known as: timeout?

Check if the current user has communication disabled.

Returns:

  • (true, false)


185
186
187
# File 'lib/discordrb/data/member.rb', line 185

def communication_disabled?
  !@communication_disabled_until.nil? && @communication_disabled_until > Time.now
end

#communication_disabled_until=(timeout_until) ⇒ Object Also known as: timeout=

Set a user's timeout duration, or remove it by setting the timeout to nil.

Parameters:

  • timeout_until (Time, nil)

    When the timeout will end.

Raises:

  • (ArgumentError)


193
194
195
196
197
# File 'lib/discordrb/data/member.rb', line 193

def communication_disabled_until=(timeout_until)
  raise ArgumentError, 'A time out cannot exceed 28 days' if timeout_until && timeout_until > (Time.now + 2_419_200)

  update_member_data(communication_disabled_until: timeout_until&.iso8601)
end

#deaftrue, false Also known as: deafened?

Returns whether this member is deafened server-wide.

Returns:

  • (true, false)

    whether this member is deafened server-wide.



88
89
90
# File 'lib/discordrb/data/member.rb', line 88

def deaf
  voice_state_attribute(:deaf)
end

#display_avatar_decorationAvatarDecoration?

Returns the avatar decoration that the user displays (server avatar decoration if they have one, user avatar decoration if they have one, nil otherwise).

Returns:

  • (AvatarDecoration, nil)

    the avatar decoration that the user displays (server avatar decoration if they have one, user avatar decoration if they have one, nil otherwise)



377
378
379
# File 'lib/discordrb/data/member.rb', line 377

def display_avatar_decoration
  server_avatar_decoration || avatar_decoration
end

#display_avatar_url(format = nil) ⇒ String?

Returns the avatar that the user has displayed (server avatar if they have one, user avatar if they have one, nil otherwise).

Parameters:

  • format (String, nil) (defaults to: nil)

    If nil, the URL will default to webp for static avatars, and will detect if the member has a gif avatar. You can otherwise specify one of webp, jpg, png, or gif to override this.

Returns:

  • (String, nil)

    the avatar that the user has displayed (server avatar if they have one, user avatar if they have one, nil otherwise)



366
367
368
# File 'lib/discordrb/data/member.rb', line 366

def display_avatar_url(format = nil)
  server_avatar_url(format) || avatar_url(format)
end

#display_banner_url(format = nil) ⇒ String?

Returns the banner that the user has displayed (server banner if they have one, user banner if they have one, nil otherwise).

Parameters:

  • format (String, nil) (defaults to: nil)

    If nil, the URL will default to webp for static banners, and will detect if the member has a gif banner. You can otherwise specify one of webp, jpg, png, or gif to override this.

Returns:

  • (String, nil)

    the banner that the user has displayed (server banner if they have one, user banner if they have one, nil otherwise)



372
373
374
# File 'lib/discordrb/data/member.rb', line 372

def display_banner_url(format = nil)
  server_banner_url(format) || banner_url(format)
end

#display_nameString

Returns the name the user displays as (nickname if they have one, global_name if they have one, username otherwise).

Returns:

  • (String)

    the name the user displays as (nickname if they have one, global_name if they have one, username otherwise)



360
361
362
# File 'lib/discordrb/data/member.rb', line 360

def display_name
  nickname || global_name || username
end

#flags=(flags) ⇒ Object

Set the flags for this member.

Parameters:

  • flags (Integer, nil)

    The new bitwise value of flags for this member, or nil.



383
384
385
# File 'lib/discordrb/data/member.rb', line 383

def flags=(flags)
  update_member_data(flags: flags)
end

#highest_roleRole

Returns the highest role this member has.

Returns:

  • (Role)

    the highest role this member has.



257
258
259
# File 'lib/discordrb/data/member.rb', line 257

def highest_role
  roles.max_by(&:position)
end

#hoist_roleRole?

Returns the role this member is being hoisted with.

Returns:

  • (Role, nil)

    the role this member is being hoisted with.



262
263
264
265
266
267
# File 'lib/discordrb/data/member.rb', line 262

def hoist_role
  hoisted_roles = roles.select(&:hoist)
  return nil if hoisted_roles.empty?

  hoisted_roles.max_by(&:position)
end

#inspectObject

Overwriting inspect for debug purposes



482
483
484
# File 'lib/discordrb/data/member.rb', line 482

def inspect
  "<Member user=#{@user.inspect} server=#{@server&.inspect || @server_id} joined_at=#{@joined_at} roles=#{@roles&.inspect || @role_ids} voice_channel=#{voice_channel.inspect} mute=#{mute} deaf=#{deaf} self_mute=#{self_mute} self_deaf=#{self_deaf}>"
end

#kick(reason = nil) ⇒ Object

Kicks this member from the server.

Parameters:

  • reason (String) (defaults to: nil)

    The reason this member is being kicked.



334
335
336
# File 'lib/discordrb/data/member.rb', line 334

def kick(reason = nil)
  server.kick(@user, reason)
end

#modify_roles(add, remove, reason = nil) ⇒ Object

Adds and removes roles from a member.

Examples:

Remove the 'Member' role from a user, and add the 'Muted' role to them.

to_add = server.roles.find {|role| role.name == 'Muted'}
to_remove = server.roles.find {|role| role.name == 'Member'}
member.modify_roles(to_add, to_remove)

Parameters:

  • add (Role, Array<Role>)

    The role(s) to add.

  • remove (Role, Array<Role>)

    The role(s) to remove.

  • reason (String) (defaults to: nil)

    The reason the user's roles are being changed.



217
218
219
220
221
222
223
224
# File 'lib/discordrb/data/member.rb', line 217

def modify_roles(add, remove, reason = nil)
  add_role_ids = role_id_array(add)
  remove_role_ids = role_id_array(remove)
  old_role_ids = resolve_role_ids
  new_role_ids = (old_role_ids - remove_role_ids + add_role_ids).uniq

  update_member_data(roles: new_role_ids, reason: reason)
end

#mutetrue, false Also known as: muted?

Returns whether this member is muted server-wide.

Returns:

  • (true, false)

    whether this member is muted server-wide.



83
84
85
# File 'lib/discordrb/data/member.rb', line 83

def mute
  voice_state_attribute(:mute)
end

#nick=(nick) ⇒ Object Also known as: nickname=

See Also:



339
340
341
# File 'lib/discordrb/data/member.rb', line 339

def nick=(nick)
  set_nick(nick)
end

#owner?true, false

Returns whether this member is the server owner.

Returns:

  • (true, false)

    whether this member is the server owner.



167
168
169
# File 'lib/discordrb/data/member.rb', line 167

def owner?
  server.owner == self
end

#remove_role(role, reason = nil) ⇒ Object

Removes one or more roles from this member.

Parameters:

  • role (Role, Array<Role>)

    The role(s) to remove.

  • reason (String) (defaults to: nil)

    The reason the user's roles are being changed.



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/discordrb/data/member.rb', line 244

def remove_role(role, reason = nil)
  role_ids = role_id_array(role)

  if role_ids.one?
    API::Server.remove_member_role(@bot.token, @server_id, @user.id, role_ids[0], reason)
  else
    old_role_ids = resolve_role_ids
    new_role_ids = old_role_ids.reject { |i| role_ids.include?(i) }
    update_member_data(roles: new_role_ids, reason: reason)
  end
end

#role?(role) ⇒ true, false

Returns whether this member has the specified role.

Parameters:

Returns:

  • (true, false)

    whether this member has the specified role.



173
174
175
176
# File 'lib/discordrb/data/member.rb', line 173

def role?(role)
  role = role.resolve_id
  roles.any?(role)
end

#rolesArray<Role>

Returns the roles this member has.

Returns:

  • (Array<Role>)

    the roles this member has.

Raises:



154
155
156
157
158
159
# File 'lib/discordrb/data/member.rb', line 154

def roles
  return @roles if @roles

  update_roles(@role_ids)
  @roles
end

#roles=(role) ⇒ Object

See Also:



179
180
181
# File 'lib/discordrb/data/member.rb', line 179

def roles=(role)
  set_roles(role)
end

#self_deaftrue, false Also known as: self_deafened?

Returns whether this member has deafened themselves.

Returns:

  • (true, false)

    whether this member has deafened themselves.



98
99
100
# File 'lib/discordrb/data/member.rb', line 98

def self_deaf
  voice_state_attribute(:self_deaf)
end

#self_mutetrue, false Also known as: self_muted?

Returns whether this member has muted themselves.

Returns:

  • (true, false)

    whether this member has muted themselves.



93
94
95
# File 'lib/discordrb/data/member.rb', line 93

def self_mute
  voice_state_attribute(:self_mute)
end

#serverServer

Returns the server this member is on.

Returns:

  • (Server)

    the server this member is on.

Raises:



142
143
144
145
146
147
148
149
# File 'lib/discordrb/data/member.rb', line 142

def server
  return @server if @server

  @server = @bot.server(@server_id)
  raise Discordrb::Errors::NoPermission, 'The bot does not have access to this server' unless @server

  @server
end

#server_avatar=(avatar) ⇒ Object

Set the server avatar for the current bot.

Parameters:

  • avatar (File, nil)

    A file like object that responds to read, or nil.



397
398
399
400
401
# File 'lib/discordrb/data/member.rb', line 397

def server_avatar=(avatar)
  raise 'Can only set an avatar for the current bot' unless current_bot?

  update_current_member_data(avatar: avatar.respond_to?(:read) ? Discordrb.encode64(avatar) : avatar)
end

#server_banner=(banner) ⇒ Object

Set the server banner for the current bot.

Parameters:

  • banner (File, nil)

    A file like object that responds to read, or nil.



389
390
391
392
393
# File 'lib/discordrb/data/member.rb', line 389

def server_banner=(banner)
  raise 'Can only set a banner for the current bot' unless current_bot?

  update_current_member_data(banner: banner.respond_to?(:read) ? Discordrb.encode64(banner) : banner)
end

#server_bio=(bio) ⇒ Object

Set the server bio for the current bot.

Parameters:

  • bio (String, nil)

    The new server bio for the bot, or nil.



405
406
407
408
409
# File 'lib/discordrb/data/member.rb', line 405

def server_bio=(bio)
  raise 'Can only set a bio for the current bot' unless current_bot?

  update_current_member_data(bio: bio)
end

#server_deafen(reason: nil) ⇒ Object

Server deafens this member.

Parameters:

  • reason (String, nil) (defaults to: nil)

    The reason for defeaning this member.



296
297
298
# File 'lib/discordrb/data/member.rb', line 296

def server_deafen(reason: nil)
  update_member_data(deaf: true, reason: reason)
end

#server_mute(reason: nil) ⇒ Object

Server mutes this member.

Parameters:

  • reason (String, nil) (defaults to: nil)

    The reason for muting this member.



308
309
310
# File 'lib/discordrb/data/member.rb', line 308

def server_mute(reason: nil)
  update_member_data(mute: true, reason: reason)
end

#server_undeafen(reason: nil) ⇒ Object

Server undeafens this member.

Parameters:

  • reason (String, nil) (defaults to: nil)

    The reason for un-defeaning this member.



302
303
304
# File 'lib/discordrb/data/member.rb', line 302

def server_undeafen(reason: nil)
  update_member_data(deaf: false, reason: reason)
end

#server_unmute(reason: nil) ⇒ Object

Server unmutes this member.

Parameters:

  • reason (String, nil) (defaults to: nil)

    The reason for un-muting this member.



314
315
316
# File 'lib/discordrb/data/member.rb', line 314

def server_unmute(reason: nil)
  update_member_data(mute: false, reason: reason)
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.

Parameters:

  • nick (String, nil)

    The string to set the nickname to, or nil if it should be reset.

  • reason (String) (defaults to: nil)

    The reason the user's nickname is being changed.



349
350
351
352
353
354
355
# File 'lib/discordrb/data/member.rb', line 349

def set_nick(nick, reason = nil)
  if @user.current_bot?
    update_current_member_data(nick: nick, reason: reason)
  else
    update_member_data(nick: nick, reason: reason)
  end
end

#set_roles(role, reason = nil) ⇒ Object

Bulk sets a member's roles.

Parameters:

  • role (Role, Array<Role>)

    The role(s) to set.

  • reason (String) (defaults to: nil)

    The reason the user's roles are being changed.



204
205
206
207
# File 'lib/discordrb/data/member.rb', line 204

def set_roles(role, reason = nil)
  role_ids = role_id_array(role)
  update_member_data(roles: role_ids, reason: reason)
end

#sort_rolesArray<Role>

Get the member's roles sorted by their order in the hierarchy.

Returns:

  • (Array<Role>)

    the roles the member has, ordered by hierarchy.



290
291
292
# File 'lib/discordrb/data/member.rb', line 290

def sort_roles
  roles.sort_by { |role| [role.position, role.id] }
end

#unban(reason = nil) ⇒ Object

Unbans this member from the server.

Parameters:

  • reason (String) (defaults to: nil)

    The reason this member is being unbanned.



328
329
330
# File 'lib/discordrb/data/member.rb', line 328

def unban(reason = nil)
  server.unban(@user, reason)
end

#voice_channelChannel

Returns the voice channel this member is in.

Returns:

  • (Channel)

    the voice channel this member is in.



103
104
105
# File 'lib/discordrb/data/member.rb', line 103

def voice_channel
  voice_state_attribute(:voice_channel)
end