From 3b6fc607838cc3abc0214813c96245a12ec7affd Mon Sep 17 00:00:00 2001 From: uurgothat Date: Sun, 17 Oct 2021 23:20:46 +0300 Subject: Add discord template support --- api/src/routes/guilds/templates/index.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'api/src/routes') diff --git a/api/src/routes/guilds/templates/index.ts b/api/src/routes/guilds/templates/index.ts index 86316d23..f303d690 100644 --- a/api/src/routes/guilds/templates/index.ts +++ b/api/src/routes/guilds/templates/index.ts @@ -3,7 +3,8 @@ const router: Router = Router(); import { Template, Guild, Role, Snowflake, Config, User, Member } from "@fosscord/util"; import { route } from "@fosscord/api"; import { DiscordApiErrors } from "@fosscord/util"; - +import fetch from "node-fetch"; +import { HTTPError } from "lambert-server"; export interface GuildTemplateCreateSchema { name: string; avatar?: string | null; @@ -12,6 +13,18 @@ export interface GuildTemplateCreateSchema { router.get("/:code", route({}), async (req: Request, res: Response) => { const { code } = req.params; + if (code.startsWith("discord:")) { + const discordTemplateID = code.split("discord:", 2)[1]; + if (Config.get().templates.allowDiscordTemplates == false) return res.json({ code: 403, message: "Discord templates are disabled on this instance."}).sendStatus(403) + + const discordTemplateData = await fetch(`https://discord.com/api/v9/guilds/templates/${discordTemplateID}`, { + method: "get", + headers: { "Content-Type": "application/json" } + }); + + return res.json(await discordTemplateData.json()); + } + const template = await Template.findOneOrFail({ code: code }); res.json(template); -- cgit 1.4.1