Fix creating guilds

This commit is contained in:
Rory& 2025-07-14 22:34:27 +02:00
parent 387722d017
commit ac47e03d19

View File

@ -16,14 +16,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { import { Column, Entity, JoinColumn, ManyToOne, OneToMany, RelationId } from "typeorm";
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
RelationId,
} from "typeorm";
import { Config, GuildWelcomeScreen, Snowflake, handleFile } from ".."; import { Config, GuildWelcomeScreen, Snowflake, handleFile } from "..";
import { Ban } from "./Ban"; import { Ban } from "./Ban";
import { BaseClass } from "./BaseClass"; import { BaseClass } from "./BaseClass";
@ -337,17 +330,13 @@ export class Guild extends BaseClass {
welcome_channels: [], welcome_channels: [],
}, },
channel_ordering: [], channel_ordering: [],
afk_timeout: Config.get().defaults.guild.afkTimeout, afk_timeout: Config.get().defaults.guild.afkTimeout,
default_message_notifications: default_message_notifications: Config.get().defaults.guild.defaultMessageNotifications,
Config.get().defaults.guild.defaultMessageNotifications, explicit_content_filter: Config.get().defaults.guild.explicitContentFilter,
explicit_content_filter:
Config.get().defaults.guild.explicitContentFilter,
features: Config.get().guild.defaultFeatures, features: Config.get().guild.defaultFeatures,
max_members: Config.get().limits.guild.maxMembers, max_members: Config.get().limits.guild.maxMembers,
max_presences: Config.get().defaults.guild.maxPresences, max_presences: Config.get().defaults.guild.maxPresences,
max_video_channel_users: max_video_channel_users: Config.get().defaults.guild.maxVideoChannelUsers,
Config.get().defaults.guild.maxVideoChannelUsers,
region: Config.get().regions.default, region: Config.get().regions.default,
}).save(); }).save();
@ -357,12 +346,12 @@ export class Guild extends BaseClass {
id: guild_id, id: guild_id,
guild_id: guild_id, guild_id: guild_id,
color: 0, color: 0,
colors: { primary_color: 0 },
hoist: false, hoist: false,
managed: false, managed: false,
// NB: in Spacebar, every role will be non-managed, as we use user-groups instead of roles for managed groups
mentionable: false, mentionable: false,
name: "@everyone", name: "@everyone",
permissions: String("2251804225"), permissions: "2251804225",
position: 0, position: 0,
icon: undefined, icon: undefined,
unicode_emoji: undefined, unicode_emoji: undefined,
@ -379,9 +368,7 @@ export class Guild extends BaseClass {
guild_id, guild_id,
id: id:
// role.id === body.template_guild_id indicates that this is the @everyone role // role.id === body.template_guild_id indicates that this is the @everyone role
role.id === body.template_guild_id role.id === body.template_guild_id ? guild_id : Snowflake.generate(),
? guild_id
: Snowflake.generate(),
}) })
.save() .save()
.then(resolve); .then(resolve);
@ -391,9 +378,7 @@ export class Guild extends BaseClass {
} }
if (!body.channels || !body.channels.length) { if (!body.channels || !body.channels.length) {
body.channels = [ body.channels = [{ id: "01", type: 0, name: "general", nsfw: false }];
{ id: "01", type: 0, name: "general", nsfw: false },
];
} }
const ids = new Map(); const ids = new Map();
@ -404,60 +389,29 @@ export class Guild extends BaseClass {
} }
}); });
for (const channel of body.channels.sort((a) => for (const channel of body.channels.sort((a) => (a.parent_id ? 1 : -1))) {
a.parent_id ? 1 : -1,
)) {
const id = ids.get(channel.id) || Snowflake.generate(); const id = ids.get(channel.id) || Snowflake.generate();
const parent_id = ids.get(channel.parent_id); const parent_id = ids.get(channel.parent_id);
const saved = await Channel.createChannel( const saved = await Channel.createChannel({ ...channel, guild_id, id, parent_id }, body.owner_id, {
{ ...channel, guild_id, id, parent_id }, keepId: true,
body.owner_id, skipExistsCheck: true,
{ skipPermissionCheck: true,
keepId: true, skipEventEmit: true,
skipExistsCheck: true, });
skipPermissionCheck: true,
skipEventEmit: true,
},
);
await Guild.insertChannelInOrder( await Guild.insertChannelInOrder(guild.id, saved.id, parent_id ?? channel.position ?? 0, guild);
guild.id,
saved.id,
parent_id ?? channel.position ?? 0,
guild,
);
} }
return guild; return guild;
} }
/** Insert a channel into the guild ordering by parent channel id or position */ /** Insert a channel into the guild ordering by parent channel id or position */
static async insertChannelInOrder( static async insertChannelInOrder(guild_id: string, channel_id: string, position: number, guild?: Guild): Promise<number>;
guild_id: string, static async insertChannelInOrder(guild_id: string, channel_id: string, parent_id: string, guild?: Guild): Promise<number>;
channel_id: string, static async insertChannelInOrder(guild_id: string, channel_id: string, insertPoint: string | number, guild?: Guild): Promise<number>;
position: number, static async insertChannelInOrder(guild_id: string, channel_id: string, insertPoint: string | number, guild?: Guild): Promise<number> {
guild?: Guild,
): Promise<number>;
static async insertChannelInOrder(
guild_id: string,
channel_id: string,
parent_id: string,
guild?: Guild,
): Promise<number>;
static async insertChannelInOrder(
guild_id: string,
channel_id: string,
insertPoint: string | number,
guild?: Guild,
): Promise<number>;
static async insertChannelInOrder(
guild_id: string,
channel_id: string,
insertPoint: string | number,
guild?: Guild,
): Promise<number> {
if (!guild) if (!guild)
guild = await Guild.findOneOrFail({ guild = await Guild.findOneOrFail({
where: { id: guild_id }, where: { id: guild_id },
@ -465,17 +419,13 @@ export class Guild extends BaseClass {
}); });
let position; let position;
if (typeof insertPoint == "string") if (typeof insertPoint == "string") position = guild.channel_ordering.indexOf(insertPoint) + 1;
position = guild.channel_ordering.indexOf(insertPoint) + 1;
else position = insertPoint; else position = insertPoint;
guild.channel_ordering.remove(channel_id); guild.channel_ordering.remove(channel_id);
guild.channel_ordering.splice(position, 0, channel_id); guild.channel_ordering.splice(position, 0, channel_id);
await Guild.update( await Guild.update({ id: guild_id }, { channel_ordering: guild.channel_ordering });
{ id: guild_id },
{ channel_ordering: guild.channel_ordering },
);
return position; return position;
} }