mirror of
https://github.com/spacebarchat/server.git
synced 2026-08-28 13:54:08 +00:00
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:
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user