|
| 1 | +import { config } from 'dotenv'; |
| 2 | +import { program } from 'commander'; |
| 3 | +import { join } from 'path'; |
| 4 | +import ora from 'ora'; |
| 5 | +import { PrismaClient } from '@prisma/client'; |
| 6 | + |
| 7 | +config(); |
| 8 | + |
| 9 | +program |
| 10 | + .requiredOption('-y, --yes', 'ARE YOU SURE?') |
| 11 | + .option('-d, --days <number>', 'number of days', 365); |
| 12 | + |
| 13 | +program.parse(); |
| 14 | + |
| 15 | +const options = program.opts(); |
| 16 | + |
| 17 | +let spinner = ora('Connecting').start(); |
| 18 | + |
| 19 | +const prisma_options = {}; |
| 20 | + |
| 21 | +if (process.env.DB_PROVIDER === 'sqlite' && !process.env.DB_CONNECTION_URL) { |
| 22 | + prisma_options.datasources = { db: { url: 'file:' + join(process.cwd(), './user/database.db') } }; |
| 23 | +} |
| 24 | + |
| 25 | +const prisma = new PrismaClient(prisma_options); |
| 26 | + |
| 27 | +if (process.env.DB_PROVIDER === 'sqlite') { |
| 28 | + const { default: sqliteMiddleware } = await import('../src/lib/middleware/prisma-sqlite.js'); |
| 29 | + prisma.$use(sqliteMiddleware); |
| 30 | + await prisma.$queryRaw`PRAGMA journal_mode=WAL;`; |
| 31 | + await prisma.$queryRaw`PRAGMA synchronous=normal;`; |
| 32 | +} |
| 33 | + |
| 34 | +spinner.succeed('Connected'); |
| 35 | + |
| 36 | +const now = Date.now(); |
| 37 | +const elapsed = 1000 * 60 * 60 * 24 * options.days; |
| 38 | +const cutoff = now - elapsed; |
| 39 | + |
| 40 | +spinner = ora('Counting total guilds').start(); |
| 41 | +const total = await prisma.guild.count(); |
| 42 | +spinner.succeed(`Found ${total} total guilds`); |
| 43 | + |
| 44 | +// ! the bot might still be in these guilds |
| 45 | +spinner = ora(`Deleting guilds inactive for more than ${options.days} days`).start(); |
| 46 | +const result = await prisma.guild.deleteMany({ where: { tickets: { none: { createdAt: { gt: new Date(cutoff) } } } } }); |
| 47 | +spinner.succeed(`Deleted ${result.count} guilds; ${total - result.count} remaining`); |
| 48 | + |
| 49 | +process.exit(0); |
0 commit comments