diff --git a/util/src/entities/Config.ts b/util/src/entities/Config.ts
index 28926233..921a12c2 100644
--- a/util/src/entities/Config.ts
+++ b/util/src/entities/Config.ts
@@ -144,9 +144,13 @@ export interface ConfigValue {
useDefaultAsOptimal: boolean;
available: Region[];
};
-
guild: {
showAllGuildsInDiscovery: boolean;
+ autoJoin: {
+ enabled: boolean;
+ guilds: string[];
+ canLeave: boolean;
+ };
};
rabbitmq: {
host: string | null;
@@ -302,6 +306,11 @@ export const DefaultConfigOptions: ConfigValue = {
guild: {
showAllGuildsInDiscovery: false,
+ autoJoin: {
+ enabled: true,
+ canLeave: true,
+ guilds: [],
+ },
},
rabbitmq: {
host: null,
diff --git a/util/src/entities/Guild.ts b/util/src/entities/Guild.ts
index e107937d..35595191 100644
--- a/util/src/entities/Guild.ts
+++ b/util/src/entities/Guild.ts
@@ -1,4 +1,5 @@
import { Column, Entity, JoinColumn, ManyToMany, ManyToOne, OneToMany, OneToOne, RelationId } from "typeorm";
+import { Config, handleFile, Snowflake } from "..";
import { Ban } from "./Ban";
import { BaseClass } from "./BaseClass";
import { Channel } from "./Channel";
@@ -295,4 +296,84 @@ export class Guild extends BaseClass {
@Column({ nullable: true })
nsfw?: boolean;
+
+ static async createGuild(body: {
+ name?: string;
+ icon?: string | null;
+ owner_id?: string;
+ channels?: Partial<Channel>[];
+ }) {
+ const guild_id = Snowflake.generate();
+
+ const guild = await new Guild({
+ name: body.name || "Fosscord",
+ icon: await handleFile(`/icons/${guild_id}`, body.icon as string),
+ region: Config.get().regions.default,
+ owner_id: body.owner_id,
+ afk_timeout: 300,
+ default_message_notifications: 0,
+ explicit_content_filter: 0,
+ features: [],
+ id: guild_id,
+ max_members: 250000,
+ max_presences: 250000,
+ max_video_channel_users: 25,
+ presence_count: 0,
+ member_count: 0, // will automatically be increased by addMember()
+ mfa_level: 0,
+ preferred_locale: "en-US",
+ premium_subscription_count: 0,
+ premium_tier: 0,
+ system_channel_flags: 0,
+ unavailable: false,
+ nsfw: false,
+ nsfw_level: 0,
+ verification_level: 0,
+ welcome_screen: {
+ enabled: false,
+ description: "No description",
+ welcome_channels: [],
+ },
+ widget_enabled: false,
+ }).save();
+
+ // we have to create the role _after_ the guild because else we would get a "SQLITE_CONSTRAINT: FOREIGN KEY constraint failed" error
+ await new Role({
+ id: guild_id,
+ guild_id: guild_id,
+ color: 0,
+ hoist: false,
+ managed: false,
+ mentionable: false,
+ name: "@everyone",
+ permissions: String("2251804225"),
+ position: 0,
+ }).save();
+
+ if (!body.channels || !body.channels.length) body.channels = [{ id: "01", type: 0, name: "general" }];
+
+ const ids = new Map();
+
+ body.channels.forEach((x) => {
+ if (x.id) {
+ ids.set(x.id, Snowflake.generate());
+ }
+ });
+
+ for (const channel of body.channels?.sort((a, b) => (a.parent_id ? 1 : -1))) {
+ var id = ids.get(channel.id) || Snowflake.generate();
+
+ // TODO: should we abort if parent_id is a category? (to disallow sub category channels)
+ var parent_id = ids.get(channel.parent_id);
+
+ await Channel.createChannel({ ...channel, guild_id, id, parent_id }, body.owner_id, {
+ keepId: true,
+ skipExistsCheck: true,
+ skipPermissionCheck: true,
+ skipEventEmit: true,
+ });
+ }
+
+ return guild;
+ }
}
|