Skip to content

Commit

Permalink
make use of Util#updateMember & properly update Guild#clientMember
Browse files Browse the repository at this point in the history
  • Loading branch information
DonovanDMC committed Sep 10, 2022
1 parent f9a9c6c commit 909f4d0
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 27 deletions.
13 changes: 7 additions & 6 deletions lib/gateway/Shard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,16 @@ export default class Shard extends TypedEmitter<ShardEvents> {
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;
}

case "GUILD_MEMBERS_CHUNK": {
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);
Expand Down Expand Up @@ -513,7 +514,7 @@ export default class Shard extends TypedEmitter<ShardEvents> {
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;
}

Expand Down Expand Up @@ -717,7 +718,7 @@ export default class Shard extends TypedEmitter<ShardEvents> {
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 };
}
Expand Down Expand Up @@ -1051,7 +1052,7 @@ export default class Shard extends TypedEmitter<ShardEvents> {
const channel = this.client.getChannel<AnyTextChannel>(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);
Expand All @@ -1073,7 +1074,7 @@ export default class Shard extends TypedEmitter<ShardEvents> {
// @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 });
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/AutocompleteInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)!);
}
Expand Down
7 changes: 3 additions & 4 deletions lib/structures/CommandInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)!);

Expand All @@ -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));
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/structures/ComponentInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)!);
Expand Down
11 changes: 9 additions & 2 deletions lib/structures/Guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/Invite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class Invite<T extends InviteInfoTypes = "withMetadata", CH exten
}
if (data.stage_instance) {
this.stageInstance = {
members: data.stage_instance.members.map(member => 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
Expand Down
4 changes: 0 additions & 4 deletions lib/structures/Member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
9 changes: 4 additions & 5 deletions lib/structures/Message.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -155,7 +154,7 @@ export default class Message<T extends AnyTextChannel | Uncached = AnyTextChanne
}

if (data.member) {
this.member = "guild" in this.channel && this.channel.guild instanceof Guild ? this.channel.guild.members.update({ ...data.member, user: data.author, id: data.author.id }, this.channel.guildID) : undefined;
this.member = this.client.util.updateMember(data.guild_id!, data.author.id, { ...data.member, user: data.author });
}
}

Expand All @@ -169,8 +168,8 @@ export default class Message<T extends AnyTextChannel | Uncached = AnyTextChanne
if (data.mentions !== undefined) {
const members: Array<Member> = [];
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);
});
Expand Down Expand Up @@ -216,7 +215,7 @@ export default class Message<T extends AnyTextChannel | Uncached = AnyTextChanne
}
this.interaction = {
id: data.interaction.id,
member: this.channel instanceof GuildChannel && this.channel.guild instanceof Guild && member ? this.channel.guild.members.update({ ...member, user: data.interaction.user, id: data.interaction.user.id }, this.channel.guildID) : undefined,
member: member ? this.client.util.updateMember(data.guild_id!, data.interaction.user.id, member) : undefined,
name: data.interaction.name,
type: data.interaction.type,
user: this.client.users.update(data.interaction.user)
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/ModalSubmitInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class ModalSubmitInteraction 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)!);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/VoiceState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default class VoiceState extends Base {
this.guild = this.client.guilds.get(data.guild_id)!;
}
if (data.member !== undefined) {
this.member = this.guild ? this.guild.members.update({ ...data.member, id: this.id }, this.guildID!) : new Member(data.member, this.client, this.guildID!);
this.member = this.client.util.updateMember(data.guild_id!, this.id, data.member);
}
if (data.mute !== undefined) {
this.mute = data.mute;
Expand Down
11 changes: 10 additions & 1 deletion lib/util/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,16 @@ export default class Util {
}

updateMember(guildID: string, memberID: string, member: RawMember): Member {
return this.#client.guilds.has(guildID) ? this.#client.guilds.get(guildID)!.members.update({ ...member, id: memberID }, guildID) : new Member(member, this.#client, guildID);
const guild = this.#client.guilds.get(guildID);
if (guild && this.#client.user.id === memberID) {
if (guild["_clientMember"]) {
guild["_clientMember"]["update"](member);
} else {
guild["_clientMember"] = guild.members.update({ ...member, id: memberID }, guildID);
}
return guild["_clientMember"];
}
return guild ? guild.members.update({ ...member, id: memberID }, guildID) : new Member(member, this.#client, guildID);
}
}

Expand Down

0 comments on commit 909f4d0

Please sign in to comment.