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.
This commit is contained in:
Zane Helton
2025-06-29 13:27:14 +10:00
committed by Madeline
parent eec2a8ba85
commit 33fde3bc4a
4 changed files with 43 additions and 24 deletions
+28 -1
View File
@@ -270,6 +270,9 @@ export class Guild extends BaseClass {
@Column({ nullable: true })
verification_level?: number;
/**
* DEPRECATED: Look at the new Guild onboarding screens.
*/
@Column({ type: "simple-json" })
welcome_screen: GuildWelcomeScreen;
@@ -308,7 +311,9 @@ export class Guild extends BaseClass {
name?: string;
icon?: string | null;
owner_id?: string;
roles?: Partial<Role>[];
channels?: Partial<Channel>[];
template_guild_id: string | null;
}) {
const guild_id = Snowflake.generate();
@@ -364,10 +369,32 @@ export class Guild extends BaseClass {
flags: 0, // TODO?
}).save();
if (!body.channels || !body.channels.length)
// create custom roles if provided
if (body.roles && body.roles.length) {
await Promise.all(
body.roles?.map((role) => {
new Promise((resolve) => {
Role.create({
...role,
guild_id,
id:
// role.id === body.template_guild_id indicates that this is the @everyone role
role.id === body.template_guild_id
? guild_id
: Snowflake.generate(),
})
.save()
.then(resolve);
});
}),
);
}
if (!body.channels || !body.channels.length) {
body.channels = [
{ id: "01", type: 0, name: "general", nsfw: false },
];
}
const ids = new Map();