Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
47.0 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import {
	type ChannelID,
	createChannelID,
	createGuildID,
	createRoleID,
	type GuildID,
	guildIdToRoleId,
	type RoleID,
	type UserID,
} from '@app/api/BrandedTypes';
import type {ChannelRepository} from '@app/api/channel/ChannelRepository';
import {
	type ChannelFollowerRemovalCopyMode,
	scheduleDeletedChannelFollowerRemoval,
} from '@app/api/channel/services/ChannelFollowers';
import type {ChannelService} from '@app/api/channel/services/ChannelService';
import {getContentMessage} from '@app/api/content_i18n/ContentI18n';
import {BatchBuilder} from '@app/api/database/CassandraQueryExecution';
import {Db, type DbOp} from '@app/api/database/CassandraTypes';
import type {PermissionOverwrite} from '@app/api/database/types/ChannelTypes';
import type {GuildRow} from '@app/api/database/types/GuildTypes';
import {isTainted} from '@app/api/experiment/ChannelThreadsGate';
import {mapGuildToGuildResponse, mapGuildToPartialResponse} from '@app/api/guild/GuildModel';
import type {GuildDiscoveryRepository} from '@app/api/guild/repositories/GuildDiscoveryRepository';
import type {GuildRepository} from '@app/api/guild/repositories/GuildRepository';
import type {GuildDataHelpers} from '@app/api/guild/services/data/GuildDataHelpers';
import {
	buildTemplateThreadParentConfig,
	clipTemplateTopic,
	mapOtherPlatformTemplateChannelType,
} from '@app/api/guild/services/data/TemplateChannelImport';
import {memberEndsInvitePause} from '@app/api/guild/services/GuildInvitePauseApplier';
import {enqueuePruneGuildVisits, onGuildVisitSettingsChanged} from '@app/api/guild/visits/GuildVisitLifecycle';
import {emitGuildCreated, emitGuildFeaturesChanged} from '@app/api/infrastructure/activity/GuildEvents';
import {contentModerationService} from '@app/api/infrastructure/ContentModerationService';
import type {EntityAssetService, PreparedAssetUpload} from '@app/api/infrastructure/EntityAssetService';
import type {IGatewayService} from '@app/api/infrastructure/IGatewayService';
import type {ISnowflakeService} from '@app/api/infrastructure/ISnowflakeService';
import type {InviteRepository} from '@app/api/invite/InviteRepository';
import {Logger} from '@app/api/Logger';
import type {LimitConfigService} from '@app/api/limits/LimitConfigService';
import {resolveLimitSafe} from '@app/api/limits/LimitConfigUtils';
import {createLimitMatchContext} from '@app/api/limits/LimitMatchContextBuilder';
import {getKVThreadAutoArchiveQueue} from '@app/api/middleware/ServiceSingletons';
import {Guild} from '@app/api/models/Guild';
import type {User} from '@app/api/models/User';
import {getGuildSearchService} from '@app/api/SearchFactory';
import type {GuildDiscoveryContext} from '@app/api/search/guild/GuildSearchSerializer';
import {deleteChannelMessageSearchDocuments} from '@app/api/search/MessageSearchIndexCleanup';
import {deleteThreadSearchDocuments} from '@app/api/search/thread/ThreadSearchService';
import {
	Channels,
	ChannelsByGuild,
	GuildMembers,
	GuildMembersByUserId,
	GuildRoles,
	Guilds,
	ThreadOnlyChannelsByGuild,
	ThreadParentConfig as ThreadParentConfigTable,
} from '@app/api/Tables';
import {assertTierAllows, NO_TIER_CONTEXT} from '@app/api/user/AccountTier';
import type {UserRepository} from '@app/api/user/repositories/UserRepository';
import {mapUserSettingsToResponse} from '@app/api/user/UserMappers';
import {addGuildToUncategorizedFolder, removeGuildFromUserFolders} from '@app/api/user/utils/GuildFolderUtils';
import {serializeGuildForAudit} from '@app/api/utils/AuditSerializationUtils';
import {mapWithConcurrency} from '@app/api/utils/ConcurrencyUtils';
import type {WebhookRepository} from '@app/api/webhook/WebhookRepository';
import type {WorkerTaskName} from '@app/api/worker/WorkerLaneConfig';
import {TierGatedActions} from '@fluxer/constants/src/AccountTierConstants';
import {AuditLogActionType} from '@fluxer/constants/src/AuditLogActionType';
import {ALL_PERMISSIONS, ChannelTypes, DEFAULT_PERMISSIONS, Permissions} from '@fluxer/constants/src/ChannelConstants';
import {DiscoveryApplicationStatus} from '@fluxer/constants/src/DiscoveryConstants';
import {
	ContentWarningLevel,
	GuildFeatures,
	GuildNSFWLevel,
	GuildSplashCardAlignment,
	GuildVerificationLevel,
	JoinSourceTypes,
	resolveVoiceChannelBitrate,
	SystemChannelFlags,
} from '@fluxer/constants/src/GuildConstants';
import {
	MAX_GUILD_CHANNELS,
	MAX_GUILD_ROLES,
	VOICE_CHANNEL_BITRATE_DEFAULT,
	VOICE_CHANNEL_CONNECTION_LIMIT_DEFAULT,
	VOICE_CHANNEL_USER_LIMIT_MAX,
} from '@fluxer/constants/src/LimitConstants';
import {THREAD_ONLY_CHANNEL_TYPES} from '@fluxer/constants/src/ThreadConstants';
import {
	DEFAULT_THREAD_PERMISSIONS,
	THREAD_AWARE_ALL_PERMISSIONS,
	THREAD_PERMISSIONS,
} from '@fluxer/constants/src/ThreadPermissionUtils';
import {DEFAULT_GUILD_FOLDER_ICON} from '@fluxer/constants/src/UserConstants';
import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';
import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';
import {MissingPermissionsError} from '@fluxer/errors/src/domains/core/MissingPermissionsError';
import {BotsCannotCreateGuildsError} from '@fluxer/errors/src/domains/guild/BotsCannotCreateGuildsError';
import {GuildTemplateInvalidError} from '@fluxer/errors/src/domains/guild/GuildTemplateInvalidError';
import {MaxGuildsError} from '@fluxer/errors/src/domains/guild/MaxGuildsError';
import {UnknownGuildError} from '@fluxer/errors/src/domains/guild/UnknownGuildError';
import {DEFAULT_STOCK_LIMITS} from '@fluxer/limits/src/LimitDefaults';
import type {GuildCreateRequest, GuildUpdateRequest} from '@fluxer/schema/src/domains/guild/GuildRequestSchemas';
import type {GuildPartialResponse, GuildResponse} from '@fluxer/schema/src/domains/guild/GuildResponseSchemas';
import type {
	TemplateChannel,
	TemplateRole,
	TemplateSerializedGuild,
} from '@fluxer/schema/src/domains/guild/GuildTemplateSchemas';
import {isValidSingleUnicodeEmoji} from '@fluxer/schema/src/primitives/EmojiValidators';
import {normalizeString} from '@fluxer/schema/src/primitives/SchemaPrimitives';
import {extractTimestamp} from '@fluxer/snowflake/src/SnowflakeUtils';
import type {IWorkerService} from '@pkgs/worker/src/contracts/IWorkerService';

const GUILD_IMAGE_FIELDS = [
	{field: 'icon', hash: 'icon_hash', dimensions: null, gate: null},
	{
		field: 'banner',
		hash: 'banner_hash',
		dimensions: {width: 'banner_width', height: 'banner_height'},
		gate: {feature: GuildFeatures.BANNER, error: ValidationErrorCodes.GUILD_BANNER_REQUIRES_FEATURE},
	},
	{
		field: 'splash',
		hash: 'splash_hash',
		dimensions: {width: 'splash_width', height: 'splash_height'},
		gate: {feature: GuildFeatures.INVITE_SPLASH, error: ValidationErrorCodes.INVITE_SPLASH_REQUIRES_FEATURE},
	},
	{
		field: 'embed_splash',
		hash: 'embed_splash_hash',
		dimensions: {width: 'embed_splash_width', height: 'embed_splash_height'},
		gate: {feature: GuildFeatures.INVITE_SPLASH, error: ValidationErrorCodes.EMBED_SPLASH_REQUIRES_FEATURE},
	},
] as const;

interface TemplateGuildSettings {
	verificationLevel: number;
	explicitContentFilter: number;
	defaultMessageNotifications: number;
	systemChannelFlags: number;
	afkTimeout: number;
}

interface MappedTemplateChannel {
	channel: TemplateChannel;
	channelId: ChannelID;
	fluxerType: number;
}

const BASE_GUILD_FEATURES: ReadonlyArray<string> = [
	GuildFeatures.ANIMATED_ICON,
	GuildFeatures.ANIMATED_BANNER,
	GuildFeatures.BANNER,
	GuildFeatures.INVITE_SPLASH,
];
const USER_TOGGLEABLE_GUILD_FEATURES: ReadonlySet<string> = new Set([
	GuildFeatures.INVITES_DISABLED,
	GuildFeatures.TEXT_CHANNEL_FLEXIBLE_NAMES,
	GuildFeatures.DETACHED_BANNER,
	GuildFeatures.CLONE_EMOJI_ENABLED,
	GuildFeatures.CLONE_STICKER_ENABLED,
	GuildFeatures.HIDE_OWNER_CROWN,
	GuildFeatures.VISITS_ENABLED,
]);
const SUPPORTED_SYSTEM_CHANNEL_FLAGS = SystemChannelFlags.SUPPRESS_JOIN_NOTIFICATIONS;
const GUILD_SETTINGS_AUDIT_KEYS: ReadonlySet<string> = new Set([
	'name',
	'icon_hash',
	'banner_hash',
	'banner_width',
	'banner_height',
	'splash_hash',
	'splash_width',
	'splash_height',
	'splash_card_alignment',
	'embed_splash_hash',
	'embed_splash_width',
	'embed_splash_height',
	'features',
	'verification_level',
	'mfa_level',
	'nsfw_level',
	'nsfw',
	'content_warning_level',
	'content_warning_text',
	'explicit_content_filter',
	'default_message_notifications',
	'system_channel_id',
	'system_channel_flags',
	'afk_channel_id',
	'afk_timeout',
	'message_history_cutoff',
]);

function setsEqual(a: ReadonlySet<string>, b: ReadonlySet<string>): boolean {
	if (a === b) return true;
	if (a.size !== b.size) return false;
	for (const value of a) {
		if (!b.has(value)) return false;
	}
	return true;
}

const TEMPLATE_AFK_TIMEOUT_MIN_SECONDS = 60;
const THREAD_PURGE_CONCURRENCY = 8;
const GUILD_DELETION_MEMBER_CONCURRENCY = 25;
const GUILD_DELETION_PENDING_FEATURES = [GuildFeatures.UNAVAILABLE_FOR_EVERYONE, GuildFeatures.UNAVAILABLE_HIDDEN];
const TEMPLATE_AFK_TIMEOUT_MAX_SECONDS = 3600;

function isSystemChannelType(type: number): boolean {
	return type === ChannelTypes.GUILD_TEXT || type === ChannelTypes.GUILD_ANNOUNCEMENT;
}

function templateChannelFallbackName(type: number, locale: string | null): string {
	if (type === ChannelTypes.GUILD_CATEGORY) {
		return getContentMessage('guild.default_category_text', locale);
	}
	if (type === ChannelTypes.GUILD_VOICE) {
		return getContentMessage('guild.default_channel_voice', locale);
	}
	return getContentMessage('guild.default_channel_text', locale);
}

export class GuildOperationsService {
	constructor(
		private readonly guildRepository: GuildRepository,
		private readonly channelRepository: ChannelRepository,
		private readonly inviteRepository: InviteRepository,
		private readonly channelService: ChannelService,
		private readonly gatewayService: IGatewayService,
		private readonly entityAssetService: EntityAssetService,
		private readonly userRepository: UserRepository,
		private readonly snowflakeService: ISnowflakeService,
		private readonly webhookRepository: WebhookRepository,
		private readonly helpers: GuildDataHelpers,
		private readonly limitConfigService: LimitConfigService,
		private readonly discoveryRepository: GuildDiscoveryRepository,
		private readonly workerService: IWorkerService<WorkerTaskName>,
	) {}

	async getGuild(params: {userId: UserID; guildId: GuildID}): Promise<GuildResponse> {
		return this.helpers.getGuildData(params);
	}

	async getUserGuilds(
		userId: UserID,
		options?: {
			before?: GuildID;
			after?: GuildID;
			limit?: number;
			withCounts?: boolean;
		},
	): Promise<Array<GuildResponse>> {
		let guilds = await this.guildRepository.listUserGuilds(userId);
		guilds.sort((a, b) => (a.id < b.id ? -1 : 1));
		if (options?.after) {
			const index = guilds.findIndex((g) => g.id === options.after);
			if (index !== -1) {
				guilds = guilds.slice(index + 1);
			}
		} else if (options?.before) {
			const index = guilds.findIndex((g) => g.id === options.before);
			if (index !== -1) {
				guilds = guilds.slice(0, index);
			}
		}
		const limit = options?.limit ?? 200;
		guilds = guilds.slice(0, limit);
		const guildIds = guilds.map((g) => g.id);
		let permissionsMap = new Map<GuildID, bigint>();
		try {
			permissionsMap = await this.gatewayService.getUserPermissionsBatch({guildIds, userId});
		} catch (error) {
			Logger.warn(
				{userId: userId.toString(), guildCount: guildIds.length, error},
				'[GuildOperationsService] Failed to fetch guild permissions batch for list_guilds; returning without permissions',
			);
		}
		const responses = guilds.map((guild) => {
			const permissions = permissionsMap.get(guild.id);
			if (permissions == null) {
				return mapGuildToGuildResponse(guild);
			}
			return mapGuildToGuildResponse(guild, {permissions});
		});
		if (!options?.withCounts) {
			return responses;
		}
		const guildsWithCounts: Array<GuildResponse> = [];
		const countBatchSize = 25;
		for (let index = 0; index < guilds.length; index += countBatchSize) {
			const guildChunk = guilds.slice(index, index + countBatchSize);
			const responseChunk = responses.slice(index, index + countBatchSize);
			const batchResults = await Promise.all(
				guildChunk.map(async (guild, chunkIndex) => {
					const baseResponse = responseChunk[chunkIndex] ?? mapGuildToGuildResponse(guild);
					try {
						const counts = await this.gatewayService.getGuildCounts(guild.id);
						return {
							...baseResponse,
							approximate_member_count: counts.memberCount,
							approximate_presence_count: counts.presenceCount,
						};
					} catch (error) {
						Logger.warn(
							{guildId: guild.id.toString(), userId: userId.toString(), error},
							'[GuildOperationsService] Failed to fetch guild counts for list_guilds; returning without counts for guild',
						);
						return baseResponse;
					}
				}),
			);
			guildsWithCounts.push(...batchResults);
		}
		return guildsWithCounts;
	}

	async getPublicGuildData(guildId: GuildID): Promise<GuildPartialResponse> {
		const guild = await this.guildRepository.findUnique(guildId);
		if (!guild) throw new UnknownGuildError();
		return mapGuildToPartialResponse(guild);
	}

	async getGuildSystem(guildId: GuildID): Promise<Guild> {
		const guild = await this.guildRepository.findUnique(guildId);
		if (!guild) throw new UnknownGuildError();
		return guild;
	}

	async createGuild(
		params: {
			user: User;
			data: GuildCreateRequest;
			locale?: string | null;
		},
		_auditLogReason?: string | null,
	): Promise<GuildResponse> {
		const {user, data, locale = null} = params;
		if (user.isBot) {
			throw new BotsCannotCreateGuildsError();
		}
		assertTierAllows(user, TierGatedActions.GUILD_CREATE, NO_TIER_CONTEXT);
		const currentGuildCount = await this.guildRepository.countUserGuilds(user.id);
		const ctx = createLimitMatchContext({user});
		const maxGuilds = resolveLimitSafe(
			this.limitConfigService.getConfigSnapshot(),
			ctx,
			'max_guilds',
			DEFAULT_STOCK_LIMITS.max_guilds,
		);
		if (currentGuildCount >= maxGuilds) throw new MaxGuildsError(maxGuilds);
		const guildId = createGuildID(await this.snowflakeService.generate());
		contentModerationService.scanText(data.name, {
			userId: user.id,
			guildId,
			channelId: null,
			messageId: null,
			surface: 'profile_field',
		});
		let preparedIcon: PreparedAssetUpload | null = null;
		if (data.icon) {
			preparedIcon = await this.entityAssetService.prepareAssetUpload({
				assetType: 'icon',
				entityType: 'guild',
				entityId: guildId,
				previousHash: null,
				base64Image: data.icon,
				errorPath: 'icon',
			});
		}
		const iconKey = preparedIcon?.newHash ?? null;
		const shouldUseEmptyFeatures = data.empty_features ?? false;
		const featuresSet = shouldUseEmptyFeatures
			? new Set<string>()
			: new Set([...BASE_GUILD_FEATURES, GuildFeatures.VISITS_ENABLED]);
		const templateSettings = this.sanitiseTemplateGuildSettings(data.template);
		const batch = new BatchBuilder();
		let systemChannelId: ChannelID;
		if (data.template) {
			const templateBatch = new BatchBuilder();
			const templateResult = await this.buildTemplateEntities(guildId, data.template, templateBatch, locale);
			systemChannelId = templateResult.systemChannelId;
			await templateBatch.executeChunked(50);
		} else {
			const defaultResult = await this.buildDefaultEntities(guildId, batch, locale);
			systemChannelId = defaultResult.systemChannelId;
		}
		const guildData: GuildRow = {
			guild_id: guildId,
			owner_id: user.id,
			name: data.name,
			vanity_url_code: null,
			icon_hash: iconKey,
			banner_hash: null,
			banner_width: null,
			banner_height: null,
			splash_hash: null,
			splash_width: null,
			splash_height: null,
			splash_card_alignment: GuildSplashCardAlignment.CENTER,
			embed_splash_hash: null,
			embed_splash_width: null,
			embed_splash_height: null,
			features: featuresSet,
			badge_ids: null,
			verification_level: templateSettings.verificationLevel,
			mfa_level: 0,
			nsfw_level: 0,
			nsfw: false,
			content_warning_level: null,
			content_warning_text: null,
			explicit_content_filter: templateSettings.explicitContentFilter,
			default_message_notifications: templateSettings.defaultMessageNotifications,
			system_channel_id: systemChannelId,
			system_channel_flags: templateSettings.systemChannelFlags,
			rules_channel_id: null,
			afk_channel_id: null,
			afk_timeout: templateSettings.afkTimeout,
			disabled_operations: 0,
			member_count: 1,
			audit_logs_indexed_at: null,
			members_indexed_at: null,
			message_history_cutoff: null,
			invites_paused_until: null,
			version: 1,
		};
		batch.addPrepared(Guilds.insert(guildData));
		batch.addPrepared(
			GuildMembers.insert({
				guild_id: guildId,
				user_id: user.id,
				joined_at: new Date(),
				nick: null,
				avatar_hash: null,
				banner_hash: null,
				bio: null,
				pronouns: null,
				accent_color: null,
				join_source_type: JoinSourceTypes.CREATOR,
				source_invite_code: null,
				inviter_id: null,
				deaf: false,
				mute: false,
				communication_disabled_until: null,
				role_ids: null,
				is_premium_sanitized: null,
				temporary: false,
				profile_flags: null,
				mention_flags: null,
				version: 1,
			}),
		);
		batch.addPrepared(GuildMembersByUserId.insert({user_id: user.id, guild_id: guildId}));
		await batch.execute();
		await this.channelRepository.threads.ensureGuildMarker(guildId, {permsSeededAt: new Date()});
		const guild = new Guild(guildData);
		await emitGuildCreated(guild);
		await this.gatewayService.startGuild(guildId);
		await this.gatewayService.joinGuild({userId: user.id, guildId});
		if (!user.isBot) {
			const userSettings = await this.userRepository.findSettings(user.id);
			if (userSettings) {
				const settingsRow = userSettings.toRow();
				const {folders: nextFolders, modified} = addGuildToUncategorizedFolder({
					folders: settingsRow.guild_folders ?? [],
					guildId,
					defaultIcon: DEFAULT_GUILD_FOLDER_ICON,
				});
				if (modified) {
					settingsRow.guild_folders = nextFolders;
					const updatedSettings = await this.userRepository.upsertSettings(settingsRow, userSettings.toRow());
					await this.gatewayService.dispatchPresence({
						userId: user.id,
						event: 'USER_SETTINGS_UPDATE',
						data: mapUserSettingsToResponse({settings: updatedSettings}),
					});
				}
			}
		}
		const guildSearchService = getGuildSearchService();
		if (guildSearchService) {
			await guildSearchService.indexGuild(guild).catch((error) => {
				Logger.error({guildId: guild.id, error}, 'Failed to index guild in search');
			});
		}
		return mapGuildToGuildResponse(guild);
	}

	async updateGuild(
		params: {
			userId: UserID;
			guildId: GuildID;
			data: GuildUpdateRequest;
		},
		auditLogReason?: string | null,
	): Promise<{
		guild: GuildResponse;
		previousFeatures: Set<string>;
		updatedFeatures: Set<string>;
	}> {
		const {userId, guildId, data} = params;
		const {checkPermission, guildData} = await this.helpers.getGuildAuthenticated({userId, guildId});
		await checkPermission(Permissions.MANAGE_GUILD);
		const currentGuild = await this.guildRepository.findUnique(guildId);
		if (!currentGuild) throw new UnknownGuildError();
		const previousSnapshot = serializeGuildForAudit(currentGuild);
		const previousFeatures = new Set(currentGuild.features);
		contentModerationService.scanText(data.name ?? null, {
			userId,
			guildId,
			channelId: null,
			messageId: null,
			surface: 'profile_field',
		});
		if (
			data.verification_level !== undefined &&
			data.verification_level < GuildVerificationLevel.LOW &&
			currentGuild.features.has(GuildFeatures.DISCOVERABLE)
		) {
			throw InputValidationError.fromCode(
				'verification_level',
				ValidationErrorCodes.DISCOVERABLE_GUILD_VERIFICATION_LEVEL_TOO_LOW,
			);
		}
		const isMfaLevelChange = data.mfa_level !== undefined && data.mfa_level !== currentGuild.mfaLevel;
		if (isMfaLevelChange) {
			const isOwner = guildData.owner_id === userId.toString();
			if (!isOwner) {
				throw new MissingPermissionsError();
			}
			const owner = await this.userRepository.findUniqueAssert(userId);
			if (owner.authenticatorTypes.size === 0) {
				throw InputValidationError.fromCode(
					'mfa_level',
					ValidationErrorCodes.MUST_ENABLE_2FA_BEFORE_REQUIRING_FOR_MODS,
				);
			}
		}
		const updatedFeatures = this.computeUpdatedFeatures(previousFeatures, data.features);
		const endsInvitePause = memberEndsInvitePause(currentGuild, updatedFeatures);
		if (endsInvitePause) {
			updatedFeatures.delete(GuildFeatures.RAID_DETECTED);
		}
		const featuresChanged = !setsEqual(previousFeatures, updatedFeatures);
		const patch: Partial<GuildRow> = {};
		if (data.name !== undefined) {
			patch.name = data.name;
		}
		if (data.splash_card_alignment !== undefined) {
			patch.splash_card_alignment = data.splash_card_alignment;
		}
		if (data.afk_channel_id !== undefined) {
			if (data.afk_channel_id) {
				const afkChannelId = createChannelID(data.afk_channel_id);
				const afkChannel = await this.channelRepository.channelData.findUnique(afkChannelId);
				if (!afkChannel || afkChannel.guildId !== guildId) {
					throw InputValidationError.fromCode('afk_channel_id', ValidationErrorCodes.AFK_CHANNEL_MUST_BE_IN_GUILD);
				}
				if (afkChannel.type !== ChannelTypes.GUILD_VOICE) {
					throw InputValidationError.fromCode('afk_channel_id', ValidationErrorCodes.AFK_CHANNEL_MUST_BE_VOICE);
				}
				patch.afk_channel_id = afkChannelId;
			} else {
				patch.afk_channel_id = null;
			}
		}
		if (data.afk_timeout !== undefined) {
			patch.afk_timeout = data.afk_timeout;
		}
		if (data.system_channel_id !== undefined) {
			if (data.system_channel_id) {
				const systemChannelId = createChannelID(data.system_channel_id);
				const systemChannel = await this.channelRepository.channelData.findUnique(systemChannelId);
				if (!systemChannel || systemChannel.guildId !== guildId) {
					throw InputValidationError.fromCode(
						'system_channel_id',
						ValidationErrorCodes.SYSTEM_CHANNEL_MUST_BE_IN_GUILD,
					);
				}
				if (!isSystemChannelType(systemChannel.type)) {
					throw InputValidationError.fromCode('system_channel_id', ValidationErrorCodes.SYSTEM_CHANNEL_MUST_BE_TEXT);
				}
				patch.system_channel_id = systemChannelId;
			} else {
				patch.system_channel_id = null;
			}
		}
		if (data.system_channel_flags !== undefined) {
			patch.system_channel_flags = data.system_channel_flags & SUPPORTED_SYSTEM_CHANNEL_FLAGS;
		}
		if (data.default_message_notifications !== undefined) {
			patch.default_message_notifications = data.default_message_notifications;
		}
		if (data.verification_level !== undefined) {
			patch.verification_level = data.verification_level;
		}
		if (data.mfa_level !== undefined) {
			patch.mfa_level = data.mfa_level;
		}
		let nextNsfw: boolean | undefined;
		if (data.nsfw !== undefined) {
			nextNsfw = data.nsfw;
		} else if (data.nsfw_level !== undefined) {
			nextNsfw = data.nsfw_level === GuildNSFWLevel.AGE_RESTRICTED;
			if (
				nextNsfw === true &&
				data.content_warning_level === undefined &&
				(currentGuild.contentWarningLevel ?? 0) === ContentWarningLevel.INHERIT
			) {
				patch.content_warning_level = ContentWarningLevel.CONTENT_WARNING;
			}
		}
		if (nextNsfw !== undefined) {
			patch.nsfw = nextNsfw;
			patch.nsfw_level = nextNsfw ? GuildNSFWLevel.AGE_RESTRICTED : GuildNSFWLevel.SAFE;
		}
		if (data.content_warning_level !== undefined) {
			patch.content_warning_level =
				data.content_warning_level === ContentWarningLevel.CONTENT_WARNING
					? ContentWarningLevel.CONTENT_WARNING
					: ContentWarningLevel.INHERIT;
		}
		if (data.content_warning_text !== undefined) {
			const trimmed = data.content_warning_text == null ? null : data.content_warning_text.trim();
			patch.content_warning_text = trimmed && trimmed.length > 0 ? trimmed : null;
		}
		if (data.explicit_content_filter !== undefined) {
			patch.explicit_content_filter = data.explicit_content_filter;
		}
		if (data.message_history_cutoff !== undefined) {
			if (data.message_history_cutoff === null) {
				patch.message_history_cutoff = null;
			} else {
				const cutoffDate = new Date(data.message_history_cutoff);
				const guildCreationTimestamp = extractTimestamp(guildId.toString());
				if (cutoffDate.getTime() < guildCreationTimestamp) {
					throw InputValidationError.fromCode(
						'message_history_cutoff',
						ValidationErrorCodes.MESSAGE_HISTORY_CUTOFF_BEFORE_GUILD_CREATION,
					);
				}
				if (cutoffDate.getTime() > Date.now()) {
					throw InputValidationError.fromCode(
						'message_history_cutoff',
						ValidationErrorCodes.MESSAGE_HISTORY_CUTOFF_IN_FUTURE,
					);
				}
				patch.message_history_cutoff = cutoffDate;
			}
		}
		if (featuresChanged) {
			patch.features = updatedFeatures;
		}
		if (endsInvitePause) {
			patch.invites_paused_until = null;
		}
		const preparedAssets = await this.prepareGuildAssetUpdates(currentGuild, data, patch);
		let updatedGuild: Guild;
		if (Object.keys(patch).length === 0) {
			updatedGuild = currentGuild;
		} else {
			try {
				updatedGuild = await this.guildRepository.upsertPartial(guildId, patch, currentGuild.toRow());
			} catch (error) {
				Logger.error({error, guildId}, 'Guild update failed with unknown commit status; retaining uploaded assets');
				throw error;
			}
		}
		try {
			await this.entityAssetService.commitAssetChanges(preparedAssets);
		} catch (error) {
			Logger.error({error, guildId}, 'Failed to commit asset changes after successful guild update');
		}
		await this.helpers.dispatchGuildUpdate(updatedGuild);
		if (featuresChanged) {
			await emitGuildFeaturesChanged({
				previous: currentGuild,
				updated: updatedGuild,
				actorId: userId,
				source: 'member',
			});
		}
		if (updatedGuild !== currentGuild) {
			await onGuildVisitSettingsChanged({guildId, before: currentGuild, after: updatedGuild});
		}
		const guildSearchService = getGuildSearchService();
		if (guildSearchService) {
			let discoveryContext: GuildDiscoveryContext | undefined;
			if (updatedGuild.features.has(GuildFeatures.DISCOVERABLE)) {
				const discoveryRow = await this.discoveryRepository.findByGuildId(updatedGuild.id).catch(() => null);
				if (discoveryRow?.status === DiscoveryApplicationStatus.APPROVED) {
					discoveryContext = {
						description: discoveryRow.description,
						categoryId: discoveryRow.category_type,
						primaryLanguage: discoveryRow.primary_language ?? null,
						tags: discoveryRow.custom_tags ?? [],
					};
				}
			}
			await guildSearchService.updateGuild(updatedGuild, discoveryContext).catch((error) => {
				Logger.error({guildId: updatedGuild.id, error}, 'Failed to update guild in search');
			});
		}
		const auditLogChanges = this.helpers.computeGuildChanges(previousSnapshot, updatedGuild, GUILD_SETTINGS_AUDIT_KEYS);
		if (auditLogChanges.length > 0) {
			await this.helpers.guildAuditLogService.record({
				guildId,
				userId,
				action: AuditLogActionType.GUILD_UPDATE,
				targetId: guildId,
				auditLogReason: auditLogReason ?? null,
				changes: auditLogChanges,
			});
		}
		return {
			guild: mapGuildToGuildResponse(updatedGuild),
			previousFeatures,
			updatedFeatures: new Set(updatedGuild.features),
		};
	}

	private computeUpdatedFeatures(
		previousFeatures: ReadonlySet<string>,
		desiredFeatures: ReadonlyArray<string> | undefined,
	): Set<string> {
		if (!desiredFeatures) {
			return new Set(previousFeatures);
		}
		const desired = new Set(desiredFeatures);
		const next = new Set(previousFeatures);
		for (const feature of desired) {
			if (previousFeatures.has(feature)) continue;
			if (!USER_TOGGLEABLE_GUILD_FEATURES.has(feature)) {
				throw InputValidationError.fromCode('features', ValidationErrorCodes.GUILD_FEATURE_NOT_TOGGLEABLE, {
					feature,
				});
			}
			next.add(feature);
		}
		for (const feature of previousFeatures) {
			if (desired.has(feature)) continue;
			if (!USER_TOGGLEABLE_GUILD_FEATURES.has(feature)) continue;
			next.delete(feature);
		}
		return next;
	}

	private async prepareGuildAssetUpdates(
		guild: Guild,
		data: GuildUpdateRequest,
		patch: Partial<GuildRow>,
	): Promise<Array<PreparedAssetUpload>> {
		const previous = guild.toRow();
		const preparedAssets: Array<PreparedAssetUpload> = [];
		try {
			for (const {field, hash, dimensions, gate} of GUILD_IMAGE_FIELDS) {
				const image = data[field];
				if (image === undefined) continue;
				if (image && gate && !guild.features.has(gate.feature)) {
					throw InputValidationError.fromCode(field, gate.error);
				}
				const prepared = await this.entityAssetService.prepareAssetUpload({
					assetType: field,
					entityType: 'guild',
					entityId: guild.id,
					previousHash: previous[hash],
					base64Image: image,
					errorPath: field,
				});
				preparedAssets.push(prepared);
				if (field === 'banner' && prepared.isAnimated && !guild.features.has(GuildFeatures.ANIMATED_BANNER)) {
					throw InputValidationError.fromCode(field, ValidationErrorCodes.ANIMATED_GUILD_BANNER_REQUIRES_FEATURE);
				}
				patch[hash] = prepared.newHash;
				if (!dimensions) continue;
				if (image === null) {
					patch[dimensions.width] = null;
					patch[dimensions.height] = null;
					continue;
				}
				const sameHash = prepared.newHash === previous[hash];
				patch[dimensions.width] = (sameHash ? previous[dimensions.width] : null) ?? prepared.width ?? null;
				patch[dimensions.height] = (sameHash ? previous[dimensions.height] : null) ?? prepared.height ?? null;
			}
			return preparedAssets;
		} catch (error) {
			await Promise.all(preparedAssets.map((prepared) => this.entityAssetService.rollbackAssetUpload(prepared)));
			throw error;
		}
	}

	async deleteGuild(
		params: {
			user: User;
			guildId: GuildID;
		},
		_auditLogReason?: string | null,
	): Promise<void> {
		const {user, guildId} = params;
		const {guildData} = await this.helpers.getGuildAuthenticated({userId: user.id, guildId});
		if (!guildData || guildData.owner_id !== user.id.toString()) {
			throw new MissingPermissionsError();
		}
		await this.scheduleGuildDeletion(guildId, 'source_deleted');
	}

	async deleteGuildById(guildId: GuildID): Promise<void> {
		await this.scheduleGuildDeletion(guildId, 'purge');
	}

	private async scheduleGuildDeletion(guildId: GuildID, copyMode: ChannelFollowerRemovalCopyMode): Promise<void> {
		const guild = await this.guildRepository.findUnique(guildId);
		if (!guild) {
			throw new UnknownGuildError();
		}
		const features = new Set(guild.features);
		for (const feature of GUILD_DELETION_PENDING_FEATURES) {
			features.add(feature);
		}
		await this.guildRepository.upsertPartial(guildId, {features}, guild.toRow());
		await this.gatewayService.reloadGuild(guildId).catch((error) => {
			Logger.warn({guildId, error}, 'Failed to reload guild after scheduling deletion');
		});
		await this.gatewayService.dispatchGuild({
			guildId,
			event: 'GUILD_DELETE',
			data: {id: guildId.toString()},
		});
		await this.workerService.addJob(
			'guildProcessDeletion',
			{guildId: guildId.toString(), copyMode},
			{jobKey: `guild-deletion-${guildId}`},
		);
	}

	async purgeGuild(guildId: GuildID, copyMode: ChannelFollowerRemovalCopyMode): Promise<void> {
		const guild = await this.guildRepository.findUnique(guildId);
		if (!guild) {
			return;
		}
		const channels = await this.channelRepository.channelData.listGuildChannels(guildId, 'complete');
		for (const channel of channels) {
			await scheduleDeletedChannelFollowerRemoval({channel, crossposts: this.channelRepository.crossposts, copyMode});
		}
		const members = await this.guildRepository.listMembers(guildId);
		await this.gatewayService.dispatchGuild({
			guildId,
			event: 'GUILD_DELETE',
			data: {id: guildId.toString()},
		});
		await enqueuePruneGuildVisits(guildId, 'deleted');
		await mapWithConcurrency(members, GUILD_DELETION_MEMBER_CONCURRENCY, async (member) => {
			await this.gatewayService.leaveGuild({userId: member.userId, guildId});
			await this.userRepository.deleteGuildSettings(member.userId, guildId);
			const user = await this.userRepository.findUnique(member.userId);
			if (user && !user.isBot) {
				await removeGuildFromUserFolders({
					userId: member.userId,
					guildId,
					userRepository: this.userRepository,
					gatewayService: this.gatewayService,
				});
			}
		});
		const invites = await this.inviteRepository.listGuildInvites(guildId);
		await Promise.all(invites.map((invite) => this.inviteRepository.delete(invite.code)));
		const webhooks = await this.webhookRepository.listByGuild(guildId);
		await Promise.all(webhooks.map((webhook) => this.webhookRepository.delete(webhook.id)));
		const threadIds = await this.channelRepository.threads.listGuildThreadIds(guildId, {parents: channels});
		for (const channel of channels) {
			await this.channelService.attachments.purgeChannelAttachments(channel);
		}
		await Promise.all(channels.map((channel) => this.channelRepository.messages.deleteAllChannelMessages(channel.id)));
		await Promise.all(
			channels.map((channel) => deleteChannelMessageSearchDocuments(channel.id, {context: {source: 'guild_delete'}})),
		);
		const discoveryRow = await this.discoveryRepository.findByGuildId(guildId);
		if (discoveryRow) {
			await this.discoveryRepository.deleteByGuildId(guildId, discoveryRow.status, discoveryRow.applied_at);
		}
		if (threadIds.length > 0) {
			const threadChannels = new Map(
				(await this.channelRepository.channelData.listChannels(threadIds)).map((channel) => [channel.id, channel]),
			);
			await mapWithConcurrency(threadIds, THREAD_PURGE_CONCURRENCY, async (threadId) => {
				const thread = threadChannels.get(threadId);
				if (thread) {
					await deleteChannelMessageSearchDocuments(threadId, {context: {source: 'guild_delete'}});
					await this.channelService.attachments.purgeChannelAttachments(thread);
				}
				await this.channelRepository.threads.purgeThread(threadId);
			});
			await deleteThreadSearchDocuments(threadIds);
		}
		if (await isTainted(guildId, {fresh: true})) {
			await this.channelRepository.threads.purgeGuild(guildId);
			await getKVThreadAutoArchiveQueue().removeGuild(guildId);
		}
		await this.guildRepository.delete(guildId, guild.ownerId);
		await this.gatewayService.stopGuild(guildId);
		const guildSearchService = getGuildSearchService();
		if (guildSearchService) {
			await guildSearchService.deleteGuild(guildId).catch((error) => {
				Logger.error({guildId, error}, 'Failed to delete guild from search');
			});
		}
	}

	private async buildDefaultEntities(
		guildId: GuildID,
		batch: BatchBuilder,
		locale: string | null,
	): Promise<{
		systemChannelId: ChannelID;
	}> {
		const textCategoryId = createChannelID(await this.snowflakeService.generate());
		const voiceCategoryId = createChannelID(await this.snowflakeService.generate());
		const generalChannelId = createChannelID(await this.snowflakeService.generate());
		const generalVoiceId = createChannelID(await this.snowflakeService.generate());
		const addChannel = (
			channelId: ChannelID,
			type: number,
			name: string,
			parentId: ChannelID | null,
			position: number,
			bitrate: number | null = null,
		) => {
			batch.addPrepared(
				Channels.insert({
					channel_id: channelId,
					guild_id: guildId,
					type,
					name,
					topic: null,
					icon_hash: null,
					url: null,
					parent_id: parentId,
					position,
					owner_id: null,
					recipient_ids: null,
					nsfw: null,
					content_warning_level: null,
					content_warning_text: null,
					rate_limit_per_user: 0,
					bitrate,
					user_limit: bitrate !== null ? 0 : null,
					voice_connection_limit: bitrate !== null ? VOICE_CHANNEL_CONNECTION_LIMIT_DEFAULT : null,
					rtc_region: null,
					rtc_p2p: null,
					last_message_id: null,
					last_pin_timestamp: null,
					permission_overwrites: null,
					nicks: null,
					soft_deleted: false,
					indexed_at: null,
					version: 1,
				}),
			);
			batch.addPrepared(
				ChannelsByGuild.upsertAll({
					guild_id: guildId,
					channel_id: channelId,
				}),
			);
		};
		addChannel(
			textCategoryId,
			ChannelTypes.GUILD_CATEGORY,
			getContentMessage('guild.default_category_text', locale),
			null,
			0,
		);
		addChannel(
			voiceCategoryId,
			ChannelTypes.GUILD_CATEGORY,
			getContentMessage('guild.default_category_voice', locale),
			null,
			1,
		);
		addChannel(
			generalChannelId,
			ChannelTypes.GUILD_TEXT,
			getContentMessage('guild.default_channel_text', locale),
			textCategoryId,
			0,
		);
		addChannel(
			generalVoiceId,
			ChannelTypes.GUILD_VOICE,
			getContentMessage('guild.default_channel_voice', locale),
			voiceCategoryId,
			0,
			VOICE_CHANNEL_BITRATE_DEFAULT,
		);
		batch.addPrepared(
			GuildRoles.insert({
				guild_id: guildId,
				role_id: guildIdToRoleId(guildId),
				name: '@everyone',
				permissions: DEFAULT_PERMISSIONS | DEFAULT_THREAD_PERMISSIONS,
				position: 0,
				hoist_position: null,
				color: 0,
				icon_hash: null,
				unicode_emoji: null,
				hoist: false,
				mentionable: false,
				version: 1,
			}),
		);
		return {systemChannelId: generalChannelId};
	}

	private async buildTemplateEntities(
		guildId: GuildID,
		template: TemplateSerializedGuild,
		batch: BatchBuilder,
		locale: string | null,
	): Promise<{
		systemChannelId: ChannelID;
	}> {
		if (template.channels.length > MAX_GUILD_CHANNELS) {
			throw new GuildTemplateInvalidError();
		}
		if (template.roles.length > MAX_GUILD_ROLES) {
			throw new GuildTemplateInvalidError();
		}
		const roleIdMap = new Map<string, RoleID>();
		const channelIdMap = new Map<string, ChannelID>();
		const channelTypeMap = new Map<string, number>();
		const everyoneRole = this.findTemplateEveryoneRole(template.roles);
		const everyoneRoleKey = everyoneRole ? this.getTemplateEntityKey(everyoneRole.id) : null;
		const everyoneRoleId = guildIdToRoleId(guildId);
		roleIdMap.set('0', everyoneRoleId);
		if (everyoneRoleKey) {
			roleIdMap.set(everyoneRoleKey, everyoneRoleId);
		}
		const seenRoleKeys = new Set<string>();
		for (const role of template.roles) {
			const roleKey = this.getTemplateEntityKey(role.id);
			if (seenRoleKeys.has(roleKey)) {
				throw new GuildTemplateInvalidError();
			}
			seenRoleKeys.add(roleKey);
			if (roleKey === '0' || roleKey === everyoneRoleKey) continue;
			roleIdMap.set(roleKey, createRoleID(await this.snowflakeService.generate()));
		}
		const seenChannelKeys = new Set<string>();
		const mappedChannels: Array<MappedTemplateChannel> = [];
		const skippedOtherPlatformChannelTypes = new Set<number>();
		for (const channel of template.channels) {
			const channelKey = this.getTemplateEntityKey(channel.id);
			if (seenChannelKeys.has(channelKey)) {
				throw new GuildTemplateInvalidError();
			}
			seenChannelKeys.add(channelKey);
			const fluxerType = mapOtherPlatformTemplateChannelType(channel.type);
			if (fluxerType == null) {
				skippedOtherPlatformChannelTypes.add(channel.type);
				continue;
			}
			const channelId = createChannelID(await this.snowflakeService.generate());
			channelIdMap.set(channelKey, channelId);
			channelTypeMap.set(channelKey, fluxerType);
			mappedChannels.push({channel, channelId, fluxerType});
		}
		if (skippedOtherPlatformChannelTypes.size > 0) {
			Logger.warn(
				{
					guildId: guildId.toString(),
					unsupportedOtherPlatformChannelTypes: Array.from(skippedOtherPlatformChannelTypes),
				},
				'[GuildOperationsService] Template import skipped unsupported channel types',
			);
		}
		const threadsActive = true;
		const permissionMask = threadsActive ? THREAD_AWARE_ALL_PERMISSIONS : ALL_PERMISSIONS;
		const templateEveryonePermissions =
			this.parseTemplatePermissionBitfield(
				everyoneRole?.permissions_new ?? everyoneRole?.permissions,
				permissionMask,
			) || DEFAULT_PERMISSIONS;
		const everyonePermissions =
			threadsActive &&
			(templateEveryonePermissions & THREAD_PERMISSIONS) === 0n &&
			(templateEveryonePermissions & Permissions.SEND_MESSAGES) !== 0n
				? templateEveryonePermissions | DEFAULT_THREAD_PERMISSIONS
				: templateEveryonePermissions;
		batch.addPrepared(
			GuildRoles.insert({
				guild_id: guildId,
				role_id: everyoneRoleId,
				name: '@everyone',
				permissions: everyonePermissions,
				position: 0,
				hoist_position: null,
				color: 0,
				icon_hash: null,
				unicode_emoji: null,
				hoist: false,
				mentionable: false,
				version: 1,
			}),
		);
		let nextRolePosition = 1;
		for (const role of template.roles) {
			const roleKey = this.getTemplateEntityKey(role.id);
			if (roleKey === '0' || roleKey === everyoneRoleKey) continue;
			const roleId = roleIdMap.get(roleKey);
			if (!roleId) continue;
			batch.addPrepared(
				GuildRoles.insert({
					guild_id: guildId,
					role_id: roleId,
					name: normalizeString(role.name) || 'new role',
					permissions: this.parseTemplatePermissionBitfield(role.permissions_new ?? role.permissions, permissionMask),
					position: nextRolePosition,
					hoist_position: null,
					color: role.color ?? 0,
					icon_hash: null,
					unicode_emoji:
						role.unicode_emoji != null && isValidSingleUnicodeEmoji(role.unicode_emoji) ? role.unicode_emoji : null,
					hoist: role.hoist ?? false,
					mentionable: role.mentionable ?? false,
					version: 1,
				}),
			);
			nextRolePosition += 1;
		}
		let firstTextChannelId: ChannelID | null = null;
		for (const mappedChannel of mappedChannels) {
			const {channel, channelId, fluxerType} = mappedChannel;
			const parentKey = channel.parent_id != null ? this.getTemplateEntityKey(channel.parent_id) : null;
			const parentType = parentKey != null ? channelTypeMap.get(parentKey) : null;
			const parentId =
				fluxerType !== ChannelTypes.GUILD_CATEGORY && parentKey != null && parentType === ChannelTypes.GUILD_CATEGORY
					? (channelIdMap.get(parentKey) ?? null)
					: null;
			const isVoice = fluxerType === ChannelTypes.GUILD_VOICE;
			let permissionOverwrites: Map<RoleID | UserID, PermissionOverwrite> | null = null;
			if (channel.permission_overwrites && channel.permission_overwrites.length > 0) {
				permissionOverwrites = new Map();
				for (const overwrite of channel.permission_overwrites) {
					if (overwrite.type !== 0) continue;
					const mappedRoleId = roleIdMap.get(this.getTemplateEntityKey(overwrite.id));
					if (!mappedRoleId) continue;
					const allow = this.parseTemplatePermissionBitfield(overwrite.allow, permissionMask);
					const deny = this.parseTemplatePermissionBitfield(overwrite.deny, permissionMask);
					permissionOverwrites.set(mappedRoleId, {
						type: overwrite.type,
						allow_: allow,
						deny_: deny,
					});
				}
				if (permissionOverwrites.size === 0) {
					permissionOverwrites = null;
				}
			}
			if (isSystemChannelType(fluxerType) && !firstTextChannelId) {
				firstTextChannelId = channelId;
			}
			batch.addPrepared(
				Channels.insert({
					channel_id: channelId,
					guild_id: guildId,
					type: fluxerType,
					name: normalizeString(channel.name) || templateChannelFallbackName(fluxerType, locale),
					topic: clipTemplateTopic(channel.topic, fluxerType),
					icon_hash: null,
					url: null,
					parent_id: parentId,
					position: channel.position,
					owner_id: null,
					recipient_ids: null,
					nsfw: channel.nsfw === true ? true : null,
					content_warning_level: null,
					content_warning_text: null,
					rate_limit_per_user: channel.rate_limit_per_user ?? 0,
					bitrate: isVoice ? resolveVoiceChannelBitrate(channel.bitrate, null) : null,
					user_limit: isVoice ? Math.min(channel.user_limit ?? 0, VOICE_CHANNEL_USER_LIMIT_MAX) : null,
					voice_connection_limit: isVoice
						? (channel.voice_connection_limit ?? VOICE_CHANNEL_CONNECTION_LIMIT_DEFAULT)
						: null,
					rtc_region: null,
					rtc_p2p: null,
					last_message_id: null,
					last_pin_timestamp: null,
					permission_overwrites: permissionOverwrites,
					nicks: null,
					soft_deleted: false,
					indexed_at: null,
					version: 1,
				}),
			);
			batch.addPrepared(
				THREAD_ONLY_CHANNEL_TYPES.has(fluxerType)
					? ThreadOnlyChannelsByGuild.upsertAll({guild_id: guildId, channel_id: channelId})
					: ChannelsByGuild.upsertAll({guild_id: guildId, channel_id: channelId}),
			);
			const parentConfig = await buildTemplateThreadParentConfig(channel, fluxerType, () =>
				this.snowflakeService.generate(),
			);
			if (parentConfig) {
				const ops: Record<string, DbOp<unknown>> = {};
				for (const [key, value] of Object.entries(parentConfig)) {
					if (value !== undefined) ops[key] = Db.set(value);
				}
				batch.addPrepared(
					ThreadParentConfigTable.patchByPk(
						{guild_id: guildId, channel_id: channelId},
						ops as Parameters<typeof ThreadParentConfigTable.patchByPk>[1],
					),
				);
			}
		}
		let systemChannelId: ChannelID | null = null;
		if (template.system_channel_id != null) {
			const templateSystemChannelKey = this.getTemplateEntityKey(template.system_channel_id);
			const templateSystemChannelType = channelTypeMap.get(templateSystemChannelKey);
			if (templateSystemChannelType !== undefined && isSystemChannelType(templateSystemChannelType)) {
				systemChannelId = channelIdMap.get(templateSystemChannelKey) ?? null;
			}
		}
		if (!systemChannelId && firstTextChannelId) {
			systemChannelId = firstTextChannelId;
		}
		if (!systemChannelId) {
			systemChannelId = createChannelID(await this.snowflakeService.generate());
			batch.addPrepared(
				Channels.insert({
					channel_id: systemChannelId,
					guild_id: guildId,
					type: ChannelTypes.GUILD_TEXT,
					name: getContentMessage('guild.default_channel_text', locale),
					topic: null,
					icon_hash: null,
					url: null,
					parent_id: null,
					position: 0,
					owner_id: null,
					recipient_ids: null,
					nsfw: null,
					content_warning_level: null,
					content_warning_text: null,
					rate_limit_per_user: 0,
					bitrate: null,
					user_limit: null,
					voice_connection_limit: null,
					rtc_region: null,
					rtc_p2p: null,
					last_message_id: null,
					last_pin_timestamp: null,
					permission_overwrites: null,
					nicks: null,
					soft_deleted: false,
					indexed_at: null,
					version: 1,
				}),
			);
			batch.addPrepared(
				ChannelsByGuild.upsertAll({
					guild_id: guildId,
					channel_id: systemChannelId,
				}),
			);
		}
		return {systemChannelId};
	}

	private parseTemplatePermissionBitfield(value: string | number | undefined, mask: bigint): bigint {
		const valueToParse = value ?? '0';
		try {
			return BigInt(valueToParse) & mask;
		} catch {
			throw new GuildTemplateInvalidError();
		}
	}

	private getTemplateEntityKey(id: number | string): string {
		return String(id);
	}

	private findTemplateEveryoneRole(roles: Array<TemplateRole>): TemplateRole | null {
		const namedEveryoneRole = roles.find((role) => role.name === '@everyone');
		if (namedEveryoneRole) {
			return namedEveryoneRole;
		}
		return roles.find((role) => this.getTemplateEntityKey(role.id) === '0') ?? null;
	}

	private sanitiseTemplateGuildSettings(template?: TemplateSerializedGuild): TemplateGuildSettings {
		return {
			verificationLevel: this.clampTemplateSetting(
				template?.verification_level,
				GuildVerificationLevel.NONE,
				GuildVerificationLevel.HIGH,
				GuildVerificationLevel.NONE,
			),
			explicitContentFilter: this.clampTemplateSetting(template?.explicit_content_filter, 0, 2, 0),
			defaultMessageNotifications: this.clampTemplateSetting(template?.default_message_notifications, 0, 1, 0),
			systemChannelFlags: (template?.system_channel_flags ?? 0) & SUPPORTED_SYSTEM_CHANNEL_FLAGS,
			afkTimeout: this.clampTemplateSetting(
				template?.afk_timeout,
				TEMPLATE_AFK_TIMEOUT_MIN_SECONDS,
				TEMPLATE_AFK_TIMEOUT_MAX_SECONDS,
				300,
			),
		};
	}

	private clampTemplateSetting(value: number | undefined, min: number, max: number, fallback: number): number {
		if (value == null || Number.isNaN(value)) {
			return fallback;
		}
		const integerValue = Math.trunc(value);
		if (integerValue < min) return min;
		if (integerValue > max) return max;
		return integerValue;
	}
}