diff --git a/lib/gateway/Shard.ts b/lib/gateway/Shard.ts index 25127f2d..1e9e62eb 100644 --- a/lib/gateway/Shard.ts +++ b/lib/gateway/Shard.ts @@ -441,7 +441,8 @@ export default class Shard extends TypedEmitter { break; } guild.memberCount++; - this.client.emit("guildMemberAdd", guild.members.update({ ...packet.d, id: packet.d.user!.id }, guild.id)); + + this.client.emit("guildMemberAdd", this.client.util.updateMember(guild.id, packet.d.user!.id, packet.d)); break; } @@ -449,7 +450,7 @@ export default class Shard extends TypedEmitter { const guild = this.client.guilds.get(packet.d.guild_id)!; guild["updateMemberLimit"](packet.d.members.length); - const members = packet.d.members.map(member => guild.members.update({ ...member, id: member.user!.id }, guild.id)); + const members = packet.d.members.map(member => this.client.util.updateMember(guild.id, member.user!.id, member)); if (packet.d.presences) { packet.d.presences.forEach(presence => { const member = guild.members.get(presence.user.id); @@ -513,7 +514,7 @@ export default class Shard extends TypedEmitter { break; } const oldMember = guild.members.get(packet.d.user!.id)?.toJSON() || null; - this.client.emit("guildMemberUpdate", guild.members.update({ ...packet.d, id: packet.d.user!.id }, guild.id), oldMember); + this.client.emit("guildMemberUpdate", this.client.util.updateMember(guild.id, packet.d.user.id, { deaf: false, mute: false, ...packet.d }), oldMember); break; } @@ -717,7 +718,7 @@ export default class Shard extends TypedEmitter { const message = channel?.messages.get(packet.d.message_id) || { channel: channel || { id: packet.d.channel_id }, id: packet.d.message_id }; let reactor: Member | User | Uncached; if (guild && packet.d.member) { - reactor = guild.members.update({ ...packet.d.member, id: packet.d.user_id }, guild.id); + reactor = this.client.util.updateMember(guild.id, packet.d.user_id, packet.d.member); } else { reactor = this.client.users.get(packet.d.user_id) || { id: packet.d.user_id }; } @@ -1051,7 +1052,7 @@ export default class Shard extends TypedEmitter { const channel = this.client.getChannel(packet.d.channel_id) || { id: packet.d.channel_id }; const startTimestamp = new Date(packet.d.timestamp); if (guild) { - const member = guild.members.update({ ...packet.d.member!, id: packet.d.user_id }, guild.id); + const member = this.client.util.updateMember(guild.id, packet.d.user_id, packet.d.member!); this.client.emit("typingStart", channel, member, startTimestamp); } else { const user = this.client.users.get(packet.d.user_id); @@ -1073,7 +1074,7 @@ export default class Shard extends TypedEmitter { // @TODO voice packet.d.self_stream = !!packet.d.self_stream; const guild = this.client.guilds.get(packet.d.guild_id)!; - const member = guild.members.update({ ...packet.d.member!, id: packet.d.user_id }, guild.id); + const member = this.client.util.updateMember(guild.id, packet.d.user_id, packet.d.member!); const oldState = member.voiceState?.toJSON() || null; const state = guild.voiceStates.update({ ...packet.d, id: member.id }); member["update"]({ deaf: state.deaf, mute: state.mute }); diff --git a/lib/structures/AutocompleteInteraction.ts b/lib/structures/AutocompleteInteraction.ts index 2876cbac..5d7d2019 100644 --- a/lib/structures/AutocompleteInteraction.ts +++ b/lib/structures/AutocompleteInteraction.ts @@ -49,7 +49,7 @@ export default class AutocompleteInteraction extends Interaction { this.guildID = data.guild_id; this.guildLocale = data.guild_locale; this.locale = data.locale!; - this.member = data.member ? this.guild instanceof Guild ? this.guild.members.update({ ...data.member, id: data.member.user.id }, this.guildID!) : new Member(data.member, client, this.guildID!) : undefined; + this.member = data.member ? this.client.util.updateMember(data.guild_id!, data.member.user.id, data.member) : undefined; this.memberPermissions = data.member ? new Permission(data.member.permissions) : undefined; this.user = client.users.update((data.user || data.member!.user)!); } diff --git a/lib/structures/CommandInteraction.ts b/lib/structures/CommandInteraction.ts index a12ede74..af8e8482 100644 --- a/lib/structures/CommandInteraction.ts +++ b/lib/structures/CommandInteraction.ts @@ -66,7 +66,7 @@ export default class CommandInteraction extends Interaction { this.guildID = data.guild_id; this.guildLocale = data.guild_locale; this.locale = data.locale!; - this.member = data.member ? this.guild instanceof Guild ? this.guild.members.update({ ...data.member, id: data.member.user.id }, this.guildID!) : new Member(data.member, client, this.guildID!) : undefined; + this.member = data.member ? this.client.util.updateMember(data.guild_id!, data.member.user.id, data.member) : undefined; this.memberPermissions = data.member ? new Permission(data.member.permissions) : undefined; this.user = client.users.update((data.user || data.member!.user)!); @@ -87,10 +87,9 @@ export default class CommandInteraction extends Interaction { if (data.data.resolved.members) { Object.entries(data.data.resolved.members).forEach(([id, member]) => { - const m = member as unknown as RawMember & { id: string; user: RawUser; }; - m.id = id; + const m = member as unknown as RawMember & { user: RawUser; }; m.user = data.data.resolved!.users![id]!; - this.data.resolved.members.add(this.guild instanceof Guild ? this.guild.members.update(m, this.guildID!) : new Member(m, client, this.guildID!)); + this.data.resolved.members.add(client.util.updateMember(data.guild_id!, id, m)); }); } diff --git a/lib/structures/ComponentInteraction.ts b/lib/structures/ComponentInteraction.ts index d57d7a73..c471d8b7 100644 --- a/lib/structures/ComponentInteraction.ts +++ b/lib/structures/ComponentInteraction.ts @@ -50,7 +50,7 @@ export default class ComponentInteraction extends Interaction { this.guildID = data.guild_id; this.guildLocale = data.guild_locale; this.locale = data.locale!; - this.member = data.member ? this.guild instanceof Guild ? this.guild.members.update({ ...data.member, id: data.member.user.id }, this.guildID!) : new Member(data.member, client, this.guildID!) : undefined; + this.member = data.member ? this.client.util.updateMember(data.guild_id!, data.member.user.id, data.member) : undefined; this.memberPermissions = data.member ? new Permission(data.member.permissions) : undefined; this.message = "messages" in this.channel ? this.channel.messages.update(data.message) : new Message(data.message, client); this.user = client.users.update((data.user || data.member!.user)!); diff --git a/lib/structures/Guild.ts b/lib/structures/Guild.ts index d12e4a39..22e6ba9a 100644 --- a/lib/structures/Guild.ts +++ b/lib/structures/Guild.ts @@ -268,8 +268,12 @@ export default class Guild extends Base { if (data.members) { - for (const member of data.members) { - this.members.update({ ...member, id: member.user!.id }, this.id).guild = this; + for (const rawMember of data.members) { + const member = this.members.update({ ...rawMember, id: rawMember.user!.id }, this.id); + member.guild = this; + if (member.id === client.user.id) { + this._clientMember = member; + } } } @@ -306,6 +310,9 @@ export default class Guild extends Base { const channel = this.channels.get(voiceState.channel_id) as VoiceChannel | StageChannel; state.channel = channel; const member = this.members.update({ id: voiceState.user_id, deaf: voiceState.deaf, mute: voiceState.mute }, this.id); + if (this._clientMember) { + this._clientMember["update"]({ deaf: voiceState.deaf, mute: voiceState.mute }); + } if (channel && "voiceMembers" in channel) { channel.voiceMembers.add(member); } diff --git a/lib/structures/Invite.ts b/lib/structures/Invite.ts index e2655f59..fce43054 100644 --- a/lib/structures/Invite.ts +++ b/lib/structures/Invite.ts @@ -101,7 +101,7 @@ export default class Invite guild!.members.update(member, guild!.id)), + members: data.stage_instance.members.map(member => this.client.util.updateMember(guild!.id, member.user!.id, member)), participantCount: data.stage_instance.participant_count, speakerCount: data.stage_instance.speaker_count, topic: data.stage_instance.topic diff --git a/lib/structures/Member.ts b/lib/structures/Member.ts index 16d99ce8..e43fe0b9 100644 --- a/lib/structures/Member.ts +++ b/lib/structures/Member.ts @@ -105,10 +105,6 @@ export default class Member extends Base { if (data.user !== undefined) { this.user = this.client.users.update(data.user); } - - if (this.guild && this.id === this.client.user.id) { - this.guild["_clientMember"] = this: - } } /** If the member associated with the user is a bot. */ diff --git a/lib/structures/Message.ts b/lib/structures/Message.ts index 0660d4e3..6c5cc874 100644 --- a/lib/structures/Message.ts +++ b/lib/structures/Message.ts @@ -1,7 +1,6 @@ import Base from "./Base"; import Attachment from "./Attachment"; import User from "./User"; -import GuildChannel from "./GuildChannel"; import Guild from "./Guild"; import type Member from "./Member"; import PartialApplication from "./PartialApplication"; @@ -155,7 +154,7 @@ export default class Message = []; this.mentions.users = data.mentions.map(user => { - if (user.member && "guild" in this.channel && this.channel.guild instanceof Guild) { - members.push(this.channel.guild.members.update({ ...user.member, user, id: user.id }, this.channel.guildID)); + if (user.member && "guild" in this.channel) { + members.push(this.client.util.updateMember(this.channel.guildID, user.id, { ...user.member, user })); } return this.client.users.update(user); }); @@ -216,7 +215,7 @@ export default class Message