From 1e7ae9415b5271d20300857696c62df480290744 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 8 May 2021 13:55:51 +0200 Subject: [PATCH] :bug: fix add/remove Member function --- src/util/Member.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/util/Member.ts b/src/util/Member.ts index 2be9686e..95a34235 100644 --- a/src/util/Member.ts +++ b/src/util/Member.ts @@ -28,14 +28,16 @@ export const PublicMemberProjection = { export async function addMember(user_id: string, guild_id: string, cache?: { guild?: Guild }) { const user = await getPublicUser(user_id, { guilds: true }); - const guildSize = user.guilds.length; - const { maxGuilds } = Config.get().limits.user; - if (guildSize >= maxGuilds) { + if (user.guilds.length >= maxGuilds) { throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403); } const guild = cache?.guild || (await GuildModel.findOne({ id: guild_id }).exec()); + if (!guild) throw new HTTPError("Guild not found", 404); + + if (await MemberModel.exists({ id: user.id, guild_id })) throw new HTTPError("You are already a member of this guild", 400); + const member = { id: user_id, guild_id: guild_id, @@ -79,7 +81,7 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui emitEvent({ event: "GUILD_CREATE", data: guild, - guild_id: guild_id, + user_id, } as GuildCreateEvent), ]); } @@ -90,6 +92,7 @@ export async function removeMember(user_id: string, guild_id: string) { const guild = await GuildModel.findOne({ id: guild_id }, { owner_id: true }).exec(); if (!guild) throw new HTTPError("Guild not found", 404); if (guild.owner_id === user_id) throw new Error("The owner cannot be removed of the guild"); + if (!(await MemberModel.exists({ id: user.id, guild_id }))) throw new HTTPError("You are not member of this guild", 404); // use promise all to execute all promises at the same time -> save time return Promise.all([