Allow enabling welcome screen and check if welcome screen channels exist within the guild (close #998)

This commit is contained in:
Madeline 2023-08-10 20:32:43 +10:00
parent be47c8a231
commit 25099aecb7
No known key found for this signature in database
GPG Key ID: 1958E017C36F2E47

View File

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