From 25099aecb7835437958062df82277191b943921f Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Thu, 10 Aug 2023 20:32:43 +1000 Subject: Allow enabling welcome screen and check if welcome screen channels exist within the guild (close #998) --- src/api/routes/guilds/#guild_id/welcome-screen.ts | 33 ++++++++++++++++------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'src/api/routes') diff --git a/src/api/routes/guilds/#guild_id/welcome-screen.ts b/src/api/routes/guilds/#guild_id/welcome-screen.ts index 2a739683..81000b4b 100644 --- a/src/api/routes/guilds/#guild_id/welcome-screen.ts +++ b/src/api/routes/guilds/#guild_id/welcome-screen.ts @@ -17,9 +17,13 @@ */ import { route } from "@spacebar/api"; -import { Guild, GuildUpdateWelcomeScreenSchema, Member } from "@spacebar/util"; +import { + Channel, + Guild, + GuildUpdateWelcomeScreenSchema, + Member, +} from "@spacebar/util"; import { Request, Response, Router } from "express"; -import { HTTPError } from "lambert-server"; const router: Router = Router(); @@ -66,17 +70,28 @@ router.patch( const guild = await Guild.findOneOrFail({ where: { id: guild_id } }); - if (!guild.welcome_screen.enabled) - throw new HTTPError("Welcome screen disabled", 400); - if (body.welcome_channels) - guild.welcome_screen.welcome_channels = body.welcome_channels; // TODO: check if they exist and are valid - if (body.description) + if (body.enabled != undefined) + guild.welcome_screen.enabled = body.enabled; + + if (body.description != undefined) guild.welcome_screen.description = body.description; - if (body.enabled != null) guild.welcome_screen.enabled = body.enabled; + + if (body.welcome_channels != undefined) { + // Ensure channels exist within the guild + await Promise.all( + body.welcome_channels?.map(({ channel_id }) => + Channel.findOneOrFail({ + where: { id: channel_id, guild_id }, + select: { id: true }, + }), + ) || [], + ); + guild.welcome_screen.welcome_channels = body.welcome_channels; + } await guild.save(); - res.sendStatus(204); + res.status(200).json(guild.welcome_screen); }, ); -- cgit 1.4.1