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); diff --git a/util/src/entities/Config.ts b/util/src/entities/Config.ts index b3167ac7..48a8b34e 100644 --- a/util/src/entities/Config.ts +++ b/util/src/entities/Config.ts @@ -160,6 +160,9 @@ export interface ConfigValue { kafka: { brokers: KafkaBroker[] | null; }; + templates: { + allowDiscordTemplates: Boolean + } } export const DefaultConfigOptions: ConfigValue = { @@ -321,4 +324,7 @@ export const DefaultConfigOptions: ConfigValue = { kafka: { brokers: null, }, + templates: { + allowDiscordTemplates: true + } };