summary refs log tree commit diff
path: root/src/api
diff options
context:
space:
mode:
authorZane Helton <zane99@me.com>2025-06-28 22:09:56 -0400
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2025-06-29 13:27:14 +1000
commit33fde3bc4a338dccd8707a7712763535862aa037 (patch)
tree0a0c9742ce8867295c4df814a64bf624ec2c082f /src/api
parentSet `premium_tier` to 0 when creating guild from template (diff)
downloadserver-ts-33fde3bc4a338dccd8707a7712763535862aa037.tar.xz
Fix the creation of guilds from templates
The biggest hold-up was missing fields (`premium_tier`,
`welcome_screen`, etc.) but it looks like someone has provided a
helpful function called `createGuild(...)` to provide sensible default
values.

This commit fixes the errors related to creating a guild from a
template. I've also refactored the code to include roles and channels in
the template.

To make sure that the @everyone role is cloned correctly, when
creating the guild from a template, we check if the role's ID
matches the template's `source_guild_id`. If it does, we set the
@everyone role to the new guild's ID.
Diffstat (limited to 'src/api')
-rw-r--r--src/api/routes/guilds/#guild_id/templates.ts4
-rw-r--r--src/api/routes/guilds/index.ts1
-rw-r--r--src/api/routes/guilds/templates/index.ts33
3 files changed, 15 insertions, 23 deletions
diff --git a/src/api/routes/guilds/#guild_id/templates.ts b/src/api/routes/guilds/#guild_id/templates.ts

index d6f12992..a9c6182f 100644 --- a/src/api/routes/guilds/#guild_id/templates.ts +++ b/src/api/routes/guilds/#guild_id/templates.ts
@@ -24,6 +24,7 @@ import { HTTPError } from "lambert-server"; const router: Router = Router(); const TemplateGuildProjection: (keyof Guild)[] = [ + "id", "name", "description", "region", @@ -32,7 +33,7 @@ const TemplateGuildProjection: (keyof Guild)[] = [ "explicit_content_filter", "preferred_locale", "afk_timeout", - "roles", + // "roles", // "channels", "afk_channel_id", "system_channel_id", @@ -85,6 +86,7 @@ router.post( const guild = await Guild.findOneOrFail({ where: { id: guild_id }, select: TemplateGuildProjection, + relations: ["roles", "channels"], }); const exists = await Template.findOne({ where: { id: guild_id }, diff --git a/src/api/routes/guilds/index.ts b/src/api/routes/guilds/index.ts
index 545beb18..a740e60b 100644 --- a/src/api/routes/guilds/index.ts +++ b/src/api/routes/guilds/index.ts
@@ -61,6 +61,7 @@ router.post( const guild = await Guild.createGuild({ ...body, owner_id: req.user_id, + template_guild_id: null, }); const { autoJoin } = Config.get().guild; diff --git a/src/api/routes/guilds/templates/index.ts b/src/api/routes/guilds/templates/index.ts
index d6de87f9..cff8f778 100644 --- a/src/api/routes/guilds/templates/index.ts +++ b/src/api/routes/guilds/templates/index.ts
@@ -108,7 +108,7 @@ router.post( // allowDiscordTemplates, // allowRaws, } = Config.get().templates; - if (!enabled) + if (!enabled) { return res .json({ code: 403, @@ -116,13 +116,16 @@ router.post( "Template creation & usage is disabled on this instance.", }) .sendStatus(403); - if (!allowTemplateCreation) + } + + if (!allowTemplateCreation) { return res .json({ code: 403, message: "Template creation is disabled on this instance.", }) .sendStatus(403); + } const { code } = req.params; const body = req.body as GuildTemplateCreateSchema; @@ -138,29 +141,15 @@ router.post( where: { code: code }, }); - const guild_id = Snowflake.generate(); - - const guild = await Guild.create({ - ...body, + const guild = await Guild.createGuild({ ...template.serialized_source_guild, - id: guild_id, + // body comes after the template + ...body, owner_id: req.user_id, - premium_tier: 0, - }).save(); - - await Role.create({ - id: guild_id, - guild_id: guild_id, - color: 0, - hoist: false, - managed: true, - mentionable: true, - name: "@everyone", - permissions: BigInt("2251804225").toString(), // TODO: where did this come from? - position: 0, - }).save(); + template_guild_id: template.source_guild_id, + }); - await Member.addToGuild(req.user_id, guild_id); + await Member.addToGuild(req.user_id, guild.id); res.status(201).json({ id: guild.id }); },