summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-08-10 20:32:43 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-08-10 20:33:42 +1000
commit25099aecb7835437958062df82277191b943921f (patch)
treeb140eef34259eda33076326d415a94d871e6fdeb /src
parentremove changelog.js and cleanup package.json scripts (diff)
downloadserver-25099aecb7835437958062df82277191b943921f.tar.xz
Allow enabling welcome screen and check if welcome screen channels exist within the guild (close #998)
Diffstat (limited to 'src')
-rw-r--r--src/api/routes/guilds/#guild_id/welcome-screen.ts33
1 files changed, 24 insertions, 9 deletions
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);
 	},
 );