Class: Rubycord::Member

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

UserAttributes::FLAGS

Instance Attribute Summary

Attributes included from MemberAttributes

#boosting_since, #communication_disabled_until, #joined_at, #nick

Attributes inherited from User

#activities, #client_status, #status

Attributes included from UserAttributes

#avatar_id, #bot_account, #discriminator, #global_name, #public_flags, #username, #webhook_account

Attributes included from IDObject

#id

Instance Method Summary collapse

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

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.

Parameters:



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rubycord/data/member.rb', line 170

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 = resolve_role_ids
    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

#ban(message_days = 0, 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.

  • reason (String) (defaults to: nil)

    The reason this member is being banned.



250
251
252
# File 'lib/rubycord/data/member.rb', line 250

def ban(message_days = 0, reason: nil)
  server.ban(@user, message_days, 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.



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

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.



220
221
222
223
224
# File 'lib/rubycord/data/member.rb', line 220

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.



211
212
213
214
215
216
# File 'lib/rubycord/data/member.rb', line 211

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)


126
127
128
# File 'lib/rubycord/data/member.rb', line 126

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)


134
135
136
137
138
# File 'lib/rubycord/data/member.rb', line 134

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)

  API::Server.update_member(@bot.token, @server_id, @user.id, 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.



34
35
36
# File 'lib/rubycord/data/member.rb', line 34

def deaf
  voice_state_attribute(:deaf)
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)



291
292
293
# File 'lib/rubycord/data/member.rb', line 291

def display_name
  nickname || global_name || username
end

#highest_roleRole

Returns the highest role this member has.

Returns:

  • (Role)

    the highest role this member has.



198
199
200
# File 'lib/rubycord/data/member.rb', line 198

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.



203
204
205
206
207
208
# File 'lib/rubycord/data/member.rb', line 203

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



345
346
347
# File 'lib/rubycord/data/member.rb', line 345

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.



262
263
264
# File 'lib/rubycord/data/member.rb', line 262

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.



158
159
160
161
162
163
164
165
# File 'lib/rubycord/data/member.rb', line 158

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

  API::Server.update_member(@bot.token, @server_id, @user.id, 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.



29
30
31
# File 'lib/rubycord/data/member.rb', line 29

def mute
  voice_state_attribute(:mute)
end

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

See Also:



267
268
269
# File 'lib/rubycord/data/member.rb', line 267

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.



108
109
110
# File 'lib/rubycord/data/member.rb', line 108

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.



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rubycord/data/member.rb', line 185

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 = resolve_role_ids
    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.

Parameters:

Returns:

  • (true, false)

    whether this member has the specified role.



114
115
116
117
# File 'lib/rubycord/data/member.rb', line 114

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:



95
96
97
98
99
100
# File 'lib/rubycord/data/member.rb', line 95

def roles
  return @roles if @roles

  update_roles(@role_ids)
  @roles
end

#roles=(role) ⇒ Object

See Also:



120
121
122
# File 'lib/rubycord/data/member.rb', line 120

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.



44
45
46
# File 'lib/rubycord/data/member.rb', line 44

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.



39
40
41
# File 'lib/rubycord/data/member.rb', line 39

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:



83
84
85
86
87
88
89
90
# File 'lib/rubycord/data/member.rb', line 83

def server
  return @server if @server

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

  @server
end

#server_deafenObject

Server deafens this member.



228
229
230
# File 'lib/rubycord/data/member.rb', line 228

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

#server_muteObject

Server mutes this member.



238
239
240
# File 'lib/rubycord/data/member.rb', line 238

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

#server_undeafenObject

Server undeafens this member.



233
234
235
# File 'lib/rubycord/data/member.rb', line 233

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

#server_unmuteObject

Server unmutes this member.



243
244
245
# File 'lib/rubycord/data/member.rb', line 243

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.

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.



277
278
279
280
281
282
283
284
285
286
# File 'lib/rubycord/data/member.rb', line 277

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.

Parameters:

  • role (Role, Array<Role>)

    The role(s) to set.

  • reason (String) (defaults to: nil)

    The reason the user's roles are being changed.



145
146
147
148
# File 'lib/rubycord/data/member.rb', line 145

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

#unban(reason = nil) ⇒ Object

Unbans this member from the server.

Parameters:

  • reason (String) (defaults to: nil)

    The reason this member is being unbanned.



256
257
258
# File 'lib/rubycord/data/member.rb', line 256

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.



49
50
51
# File 'lib/rubycord/data/member.rb', line 49

def voice_channel
  voice_state_attribute(:voice_channel)
end