feat(scripts): add config migration script for guild settings
Add script to migrate existing config.json values to database with bun run db:migrate-config command.
This commit is contained in:
51
shared/scripts/migrate-config-to-db.ts
Normal file
51
shared/scripts/migrate-config-to-db.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { guildSettingsService } from "@shared/modules/guild-settings/guild-settings.service";
|
||||
import { config } from "@shared/lib/config";
|
||||
import { env } from "@shared/lib/env";
|
||||
|
||||
async function migrateConfigToDatabase() {
|
||||
const guildId = env.DISCORD_GUILD_ID;
|
||||
|
||||
if (!guildId) {
|
||||
console.error("DISCORD_GUILD_ID not set. Cannot migrate config.");
|
||||
console.log("Set DISCORD_GUILD_ID in your environment to migrate config.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`Migrating config for guild ${guildId}...`);
|
||||
|
||||
const existing = await guildSettingsService.getSettings(guildId);
|
||||
if (existing) {
|
||||
console.log("Guild settings already exist in database:");
|
||||
console.log(JSON.stringify(existing, null, 2));
|
||||
console.log("\nSkipping migration. Delete existing settings first if you want to re-migrate.");
|
||||
return;
|
||||
}
|
||||
|
||||
await guildSettingsService.upsertSettings({
|
||||
guildId,
|
||||
studentRoleId: config.studentRole ?? undefined,
|
||||
visitorRoleId: config.visitorRole ?? undefined,
|
||||
colorRoleIds: config.colorRoles ?? [],
|
||||
welcomeChannelId: config.welcomeChannelId ?? undefined,
|
||||
welcomeMessage: config.welcomeMessage ?? undefined,
|
||||
feedbackChannelId: config.feedbackChannelId ?? undefined,
|
||||
terminalChannelId: config.terminal?.channelId ?? undefined,
|
||||
terminalMessageId: config.terminal?.messageId ?? undefined,
|
||||
moderationLogChannelId: config.moderation?.cases?.logChannelId ?? undefined,
|
||||
moderationDmOnWarn: config.moderation?.cases?.dmOnWarn ?? true,
|
||||
moderationAutoTimeoutThreshold: config.moderation?.cases?.autoTimeoutThreshold ?? undefined,
|
||||
});
|
||||
|
||||
console.log("✅ Migration complete!");
|
||||
console.log("\nGuild settings are now stored in the database.");
|
||||
console.log("You can manage them via:");
|
||||
console.log(" - /settings command in Discord");
|
||||
console.log(" - API endpoints at /api/guilds/:guildId/settings");
|
||||
}
|
||||
|
||||
migrateConfigToDatabase()
|
||||
.then(() => process.exit(0))
|
||||
.catch((error) => {
|
||||
console.error("Migration failed:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user