🐛 fix vanity url

This commit is contained in:
Flam3rboy 2021-09-16 22:03:30 +02:00
parent b446a3110b
commit 4003fef534
2 changed files with 5 additions and 3 deletions

1
api/package-lock.json generated
View File

@ -68,6 +68,7 @@
} }
}, },
"../util": { "../util": {
"name": "@fosscord/util",
"version": "1.0.0", "version": "1.0.0",
"hasInstallScript": true, "hasInstallScript": true,
"license": "GPLV3", "license": "GPLV3",

View File

@ -1,6 +1,7 @@
import { Channel, ChannelType, getPermission, Guild, Invite, trimSpecial } from "@fosscord/util"; import { Channel, ChannelType, getPermission, Guild, Invite, trimSpecial } from "@fosscord/util";
import { Router, Request, Response } from "express"; import { Router, Request, Response } from "express";
import { route } from "@fosscord/api"; import { route } from "@fosscord/api";
import { HTTPError } from "lambert-server";
const router = Router(); const router = Router();
@ -29,14 +30,14 @@ router.patch("/", route({ body: "VanityUrlSchema", permission: "MANAGE_GUILD" })
const body = req.body as VanityUrlSchema; const body = req.body as VanityUrlSchema;
const code = body.code?.replace(InviteRegex, ""); const code = body.code?.replace(InviteRegex, "");
await Invite.findOneOrFail({ code }); const invite = await Invite.findOne({ code });
if (invite) throw new HTTPError("Invite already exists");
const guild = await Guild.findOneOrFail({ id: guild_id }); const guild = await Guild.findOneOrFail({ id: guild_id });
const { id } = await Channel.findOneOrFail({ guild_id, type: ChannelType.GUILD_TEXT }); const { id } = await Channel.findOneOrFail({ guild_id, type: ChannelType.GUILD_TEXT });
guild.vanity_url_code = code;
Promise.all([ Promise.all([
guild.save(), Guild.update({ id: guild_id }, { vanity_url_code: code }),
Invite.delete({ code: guild.vanity_url_code }), Invite.delete({ code: guild.vanity_url_code }),
new Invite({ new Invite({
code: code, code: code,