Class: Discordrb::Member

Inherits:
User
  • Object
show all
Includes:
MemberAttributes, PermissionCalculator
Defined in:
lib/discordrb/data.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

#joined_at, #nick, #roles, #server

Attributes inherited from User

#game, #status, #stream_type, #stream_url

Attributes included from UserAttributes

#avatar_id, #bot_account, #discriminator, #username

Attributes included from IDObject

#id

Instance Method Summary collapse

Methods included from PermissionCalculator

#defined_permission?, #permission?

Methods inherited from User

#await, #await!, #current_bot?, #on, #pm, #send_file, #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.


612
613
614
615
616
617
618
619
620
621
622
# File 'lib/discordrb/data.rb', line 612

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

#colourColourRGB? Also known as: color


660
661
662
663
# File 'lib/discordrb/data.rb', line 660

def colour
  return nil unless colour_role
  colour_role.color
end

#colour_roleRole? Also known as: color_role


652
653
654
655
656
# File 'lib/discordrb/data.rb', line 652

def colour_role
  coloured_roles = @roles.select { |v| v.colour.combined.nonzero? }
  return nil if coloured_roles.empty?
  coloured_roles.sort_by(&:position).last
end

#deaftrue, false Also known as: deafened?


523
524
525
# File 'lib/discordrb/data.rb', line 523

def deaf
  voice_state_attribute(:deaf)
end

#display_nameString


711
712
713
# File 'lib/discordrb/data.rb', line 711

def display_name
  nickname || username
end

#highest_roleRole


640
641
642
# File 'lib/discordrb/data.rb', line 640

def highest_role
  @roles.sort_by(&:position).last
end

#hoist_roleRole?


645
646
647
648
649
# File 'lib/discordrb/data.rb', line 645

def hoist_role
  hoisted_roles = @roles.select(&:hoist)
  return nil if hoisted_roles.empty?
  hoisted_roles.sort_by(&:position).last
end

#inspectObject

Overwriting inspect for debug purposes


738
739
740
# File 'lib/discordrb/data.rb', line 738

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.

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)

600
601
602
603
604
605
606
607
# File 'lib/discordrb/data.rb', line 600

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

#mutetrue, false Also known as: muted?


518
519
520
# File 'lib/discordrb/data.rb', line 518

def mute
  voice_state_attribute(:mute)
end

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

See Also:


687
688
689
# File 'lib/discordrb/data.rb', line 687

def nick=(nick)
  set_nick(nick)
end

#owner?true, false


568
569
570
# File 'lib/discordrb/data.rb', line 568

def owner?
  @server.owner == self
end

#remove_role(role, reason = nil) ⇒ Object

Removes one or more roles from this member.


627
628
629
630
631
632
633
634
635
636
637
# File 'lib/discordrb/data.rb', line 627

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


574
575
576
577
# File 'lib/discordrb/data.rb', line 574

def role?(role)
  role = role.resolve_id
  @roles.any? { |e| e.id == role }
end

#roles=(role) ⇒ Object

See Also:


580
581
582
# File 'lib/discordrb/data.rb', line 580

def roles=(role)
  set_roles(role)
end

#self_deaftrue, false Also known as: self_deafened?


533
534
535
# File 'lib/discordrb/data.rb', line 533

def self_deaf
  voice_state_attribute(:self_deaf)
end

#self_mutetrue, false Also known as: self_muted?


528
529
530
# File 'lib/discordrb/data.rb', line 528

def self_mute
  voice_state_attribute(:self_mute)
end

#server_deafenObject

Server deafens this member.


667
668
669
# File 'lib/discordrb/data.rb', line 667

def server_deafen
  API::Server.update_member(@bot.token, @server.id, @user.id, deaf: true)
end

#server_muteObject

Server mutes this member.


677
678
679
# File 'lib/discordrb/data.rb', line 677

def server_mute
  API::Server.update_member(@bot.token, @server.id, @user.id, mute: true)
end

#server_undeafenObject

Server undeafens this member.


672
673
674
# File 'lib/discordrb/data.rb', line 672

def server_undeafen
  API::Server.update_member(@bot.token, @server.id, @user.id, deaf: false)
end

#server_unmuteObject

Server unmutes this member.


682
683
684
# File 'lib/discordrb/data.rb', line 682

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.


697
698
699
700
701
702
703
704
705
706
# File 'lib/discordrb/data.rb', line 697

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.


587
588
589
590
# File 'lib/discordrb/data.rb', line 587

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_channelChannel


538
539
540
# File 'lib/discordrb/data.rb', line 538

def voice_channel
  voice_state_attribute(:voice_channel)
end