From e2db4c94f6f2cddb5b4e3fb76b78aacf2a24d587 Mon Sep 17 00:00:00 2001 From: notsapinho Date: Tue, 11 May 2021 10:33:56 -0300 Subject: [PATCH] Some refactor, fix channel_delete event and @everyone role appearing on member roles --- .prettierrc | 3 ++- src/routes/channels/#channel_id/index.ts | 14 +++++++++----- src/routes/guilds/#guild_id/channels.ts | 11 +++++++---- src/routes/guilds/index.ts | 12 ++++++------ 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/.prettierrc b/.prettierrc index 73ac8239..8a2c607f 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,6 @@ { "tabWidth": 4, "useTabs": true, - "printWidth": 140 + "printWidth": 140, + "trailingComma": "none" } diff --git a/src/routes/channels/#channel_id/index.ts b/src/routes/channels/#channel_id/index.ts index d66b7570..0e5a5124 100644 --- a/src/routes/channels/#channel_id/index.ts +++ b/src/routes/channels/#channel_id/index.ts @@ -24,16 +24,18 @@ router.delete("/", async (req, res) => { const { channel_id } = req.params; const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + if (!channel) throw new HTTPError("Channel not found", 404); + const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel }); permission.hasThrow("MANAGE_CHANNELS"); - await ChannelModel.deleteOne({ id: channel_id }); - // TODO: Dm channel "close" not delete const data = toObject(channel); await emitEvent({ event: "CHANNEL_DELETE", data, guild_id: channel?.guild_id, channel_id } as ChannelDeleteEvent); + await ChannelModel.deleteOne({ id: channel_id }); + res.send(data); }); @@ -47,14 +49,16 @@ router.patch("/", check(ChannelModifySchema), async (req, res) => { const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload).exec(); if (!channel) throw new HTTPError("Channel not found", 404); + const data = toObject(channel); + await emitEvent({ event: "CHANNEL_UPDATE", - data: toObject(channel), + data, guild_id: channel.guild_id, - channel_id, + channel_id } as ChannelUpdateEvent); - res.send(toObject(channel)); + res.send(data); }); export default router; diff --git a/src/routes/guilds/#guild_id/channels.ts b/src/routes/guilds/#guild_id/channels.ts index 73982ed5..9e8d3506 100644 --- a/src/routes/guilds/#guild_id/channels.ts +++ b/src/routes/guilds/#guild_id/channels.ts @@ -16,9 +16,11 @@ router.get("/", async (req, res) => { router.post("/", check(ChannelModifySchema), async (req, res) => { const { guild_id } = req.params; const body = req.body as ChannelModifySchema; + if (!body.permission_overwrites) body.permission_overwrites = []; if (!body.topic) body.topic = ""; if (!body.rate_limit_per_user) body.rate_limit_per_user = 0; + switch (body.type) { case ChannelType.DM: case ChannelType.GROUP_DM: @@ -31,9 +33,9 @@ router.post("/", check(ChannelModifySchema), async (req, res) => { } if (body.parent_id) { - const exists = await ChannelModel.findOne({ id: body.parent_id }, {guild_id:true}).exec(); + const exists = await ChannelModel.findOne({ id: body.parent_id }, { guild_id: true }).exec(); if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400); - if (exists.guild_id !== guild_id) throw new HTTPError("The category channel needs to be in the guild") + if (exists.guild_id !== guild_id) throw new HTTPError("The category channel needs to be in the guild"); } const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); @@ -43,8 +45,9 @@ router.post("/", check(ChannelModifySchema), async (req, res) => { ...body, id: Snowflake.generate(), created_at: new Date(), - guild_id, + guild_id }; + await new ChannelModel(channel).save(); await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id } as ChannelCreateEvent); @@ -63,7 +66,7 @@ router.patch("/", check(ChannelModifySchema), async (req, res) => { ...body }; const channelm = await ChannelModel.find({ guild_id }).exec(); - if(!channelm) throw new HTTPError("Channel not found", 404); + if (!channelm) throw new HTTPError("Channel not found", 404); await new ChannelModel(channel).save(); diff --git a/src/routes/guilds/index.ts b/src/routes/guilds/index.ts index 176e4e69..1ed9d0ff 100644 --- a/src/routes/guilds/index.ts +++ b/src/routes/guilds/index.ts @@ -58,10 +58,10 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) = welcome_screen: { enabled: false, description: "No description", - welcome_channels: [], + welcome_channels: [] }, widget_channel_id: undefined, - widget_enabled: false, + widget_enabled: false }; const [guild_doc, role] = await Promise.all([ @@ -71,13 +71,13 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) = guild_id: guild_id, color: 0, hoist: false, - managed: true, - mentionable: true, + managed: false, + mentionable: false, name: "@everyone", permissions: 2251804225n, position: 0, - tags: null, - }).save(), + tags: null + }).save() ]); await addMember(req.user_id, guild_id, { guild: guild_doc });