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
@!visibility private
# 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
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
@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
@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
@return [true, false] whether this member is deafened server-wide.
# File lib/discordrb/data.rb, line 526 def deaf voice_state_attribute(:deaf) end
@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
@return [Role] the highest role this member has.
# File lib/discordrb/data.rb, line 644 def highest_role @roles.max_by(&:position) end
@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
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
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
@return [true, false] whether this member is muted server-wide.
# File lib/discordrb/data.rb, line 521 def mute voice_state_attribute(:mute) end
@see Member#set_nick
# File lib/discordrb/data.rb, line 694 def nick=(nick) set_nick(nick) end
@return [true, false] whether this member is the server owner.
# File lib/discordrb/data.rb, line 572 def owner? @server.owner == self end
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
@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
@see Member#set_roles
# File lib/discordrb/data.rb, line 584 def roles=(role) set_roles(role) end
@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
@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
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
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
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
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
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
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 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 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
@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
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
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