class Discordrb::Member

A member is a user on a server. It differs from regular users in that it has roles, voice statuses and things like that.

Public Class Methods

new(data, server, bot) click to toggle source

@!visibility private

Calls superclass method
# File lib/discordrb/data.rb, line 553
def initialize(data, server, bot)
  @bot = bot

  @user = bot.ensure_user(data['user'])
  super @user # Initialize the delegate class

  # Somehow, Discord doesn't send the server ID in the standard member format...
  raise ArgumentError, 'Cannot create a member without any information about the server!' if server.nil? && data['guild_id'].nil?

  @server = server || bot.server(data['guild_id'].to_i)

  # Initialize the roles by getting the roles from the server one-by-one
  update_roles(data['roles'])

  @nick = data['nick']
  @joined_at = data['joined_at'] ? Time.parse(data['joined_at']) : nil
end

Public Instance Methods

add_role(role, reason = nil) click to toggle source

Adds one or more roles to this member. @param role [Role, Array<Role, resolve_id>, resolve_id] The role(s) to add. @param reason [String] The reason the user's roles are being changed.

# File lib/discordrb/data.rb, line 616
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
color()
Alias for: colour
color_role()
Alias for: colour_role
colour() click to toggle source

@return [ColourRGB, nil] the colour this member has.

# File lib/discordrb/data.rb, line 666
def colour
  return nil unless colour_role

  colour_role.color
end
Also aliased as: color
colour_role() click to toggle source

@return [Role, nil] the role this member is basing their colour on.

# File lib/discordrb/data.rb, line 657
def colour_role
  coloured_roles = @roles.select { |v| v.colour.combined.nonzero? }
  return nil if coloured_roles.empty?

  coloured_roles.max_by(&:position)
end
Also aliased as: color_role
deaf() click to toggle source

@return [true, false] whether this member is deafened server-wide.

# File lib/discordrb/data.rb, line 526
def deaf
  voice_state_attribute(:deaf)
end
Also aliased as: deafened?
deafened?()
Alias for: deaf
display_name() click to toggle source

@return [String] the name the user displays as (nickname if they have one, username otherwise)

# File lib/discordrb/data.rb, line 718
def display_name
  nickname || username
end
highest_role() click to toggle source

@return [Role] the highest role this member has.

# File lib/discordrb/data.rb, line 644
def highest_role
  @roles.max_by(&:position)
end
hoist_role() click to toggle source

@return [Role, nil] the role this member is being hoisted with.

# File lib/discordrb/data.rb, line 649
def hoist_role
  hoisted_roles = @roles.select(&:hoist)
  return nil if hoisted_roles.empty?

  hoisted_roles.max_by(&:position)
end
inspect() click to toggle source

Overwriting inspect for debug purposes

# File lib/discordrb/data.rb, line 745
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) click to toggle source

Adds and removes roles from a member. @param add [Role, Array<Role>] The role(s) to add. @param remove [Role, Array<Role>] The role(s) to remove. @param reason [String] The reason the user's roles are being changed. @example 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)
# File lib/discordrb/data.rb, line 604
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() click to toggle source

@return [true, false] whether this member is muted server-wide.

# File lib/discordrb/data.rb, line 521
def mute
  voice_state_attribute(:mute)
end
Also aliased as: muted?
muted?()
Alias for: mute
nick=(nick) click to toggle source

@see Member#set_nick

# File lib/discordrb/data.rb, line 694
def nick=(nick)
  set_nick(nick)
end
Also aliased as: nickname=
nickname=(nick)
Alias for: nick=
owner?() click to toggle source

@return [true, false] whether this member is the server owner.

# File lib/discordrb/data.rb, line 572
def owner?
  @server.owner == self
end
remove_role(role, reason = nil) click to toggle source

Removes one or more roles from this member. @param role [Role, Array<Role>] The role(s) to remove. @param reason [String] The reason the user's roles are being changed.

# File lib/discordrb/data.rb, line 631
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) click to toggle source

@param role [Role, Integer, resolve_id] the role to check or its ID. @return [true, false] whether this member has the specified role.

# File lib/discordrb/data.rb, line 578
def role?(role)
  role = role.resolve_id
  @roles.any? { |e| e.id == role }
end
roles=(role) click to toggle source

@see Member#set_roles

# File lib/discordrb/data.rb, line 584
def roles=(role)
  set_roles(role)
end
self_deaf() click to toggle source

@return [true, false] whether this member has deafened themselves.

# File lib/discordrb/data.rb, line 536
def self_deaf
  voice_state_attribute(:self_deaf)
end
Also aliased as: self_deafened?
self_deafened?()
Alias for: self_deaf
self_mute() click to toggle source

@return [true, false] whether this member has muted themselves.

# File lib/discordrb/data.rb, line 531
def self_mute
  voice_state_attribute(:self_mute)
end
Also aliased as: self_muted?
self_muted?()
Alias for: self_mute
server_deafen() click to toggle source

Server deafens this member.

# File lib/discordrb/data.rb, line 674
def server_deafen
  API::Server.update_member(@bot.token, @server.id, @user.id, deaf: true)
end
server_mute() click to toggle source

Server mutes this member.

# File lib/discordrb/data.rb, line 684
def server_mute
  API::Server.update_member(@bot.token, @server.id, @user.id, mute: true)
end
server_undeafen() click to toggle source

Server undeafens this member.

# File lib/discordrb/data.rb, line 679
def server_undeafen
  API::Server.update_member(@bot.token, @server.id, @user.id, deaf: false)
end
server_unmute() click to toggle source

Server unmutes this member.

# File lib/discordrb/data.rb, line 689
def server_unmute
  API::Server.update_member(@bot.token, @server.id, @user.id, mute: false)
end
set_nick(nick, reason = nil) click to toggle source

Sets or resets this member's nickname. Requires the Change Nickname permission for the bot itself and Manage Nicknames for other users. @param nick [String, nil] The string to set the nickname to, or nil if it should be reset. @param reason [String] The reason the user's nickname is being changed.

# File lib/discordrb/data.rb, line 704
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
Also aliased as: set_nickname
set_nickname(nick, reason = nil)
Alias for: set_nick
set_roles(role, reason = nil) click to toggle source

Bulk sets a member's roles. @param role [Role, Array<Role>] The role(s) to set. @param reason [String] The reason the user's roles are being changed.

# File lib/discordrb/data.rb, line 591
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
update_nick(nick) click to toggle source

Update this member's nick @note For internal use only. @!visibility private

# File lib/discordrb/data.rb, line 738
def update_nick(nick)
  @nick = nick
end
update_roles(role_ids) click to toggle source

Update this member's roles @note For internal use only. @!visibility private

# File lib/discordrb/data.rb, line 725
def update_roles(role_ids)
  @roles = []
  role_ids.each do |id|
    # It is posible for members to have roles that do not exist
    # on the server any longer. See https://github.com/meew0/discordrb/issues/371
    role = @server.role(id)
    @roles << role if role
  end
end
voice_channel() click to toggle source

@return [Channel] the voice channel this member is in.

# File lib/discordrb/data.rb, line 541
def voice_channel
  voice_state_attribute(:voice_channel)
end

Private Instance Methods

role_id_array(role) click to toggle source

Utility method to get a list of role IDs from one role or an array of roles

# File lib/discordrb/data.rb, line 752
def role_id_array(role)
  if role.is_a? Array
    role.map(&:resolve_id)
  else
    [role.resolve_id]
  end
end
voice_state_attribute(name) click to toggle source

Utility method to get data out of this member's voice state

# File lib/discordrb/data.rb, line 761
def voice_state_attribute(name)
  voice_state = @server.voice_states[@user.id]
  voice_state&.send name
end