Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"ai": "^5.0.68",
"bson": "^6.10.4",
"bullmq": "^5.61.0",
"cron": "^4.3.4",
"discord-api-types": "^0.38.30",
"ioredis": "^5.8.1",
"zod": "^4.1.12"
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ObjectId } from "bson";
import { rateLimiter } from "./services/rate-limiter";
import { messageQueue, setWorkerContext } from "./services/message-queue";
import axios from "axios";
import { startMigrationCronJob } from "./scripts/cron-jobs";
import { startServer } from "./server";

const prisma = new PrismaClient();
Expand Down Expand Up @@ -74,6 +75,8 @@ client.once(GatewayDispatchEvents.Ready, ({ data }) => {
});

console.log("✅ Worker context initialized");

startMigrationCronJob(client.api, botId);
});

client.on(
Expand Down
36 changes: 36 additions & 0 deletions src/scripts/cron-jobs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { CronJob } from "cron";
import { migrateRoles } from "./migrate-roles";
import { Client } from "@discordjs/core";

export function startMigrationCronJob(api: Client["api"], botId: string) {
console.log("[CRON] 🕒 Scheduling role migration job...");

const migrateRolesJob = new CronJob(
"*/10 * * * *",
async () => {
try {
console.log("[CRON] 🚀 Starting scheduled role migration...");
const stats = await migrateRoles(api, botId);
console.log(
`[CRON] ✅ Scheduled role migration finished successfully. Added/Removed: ${
stats.premiumAdded +
stats.premiumRemoved +
stats.freemiumAdded +
stats.freemiumRemoved
}`,
);
} catch (error) {
console.error("[CRON] ❌ Scheduled role migration failed:", error);
}
},
null,
true,
"America/Sao_Paulo",
);

console.log(
`[CRON] ✅ Role migration job scheduled. Next run: ${migrateRolesJob
.nextDate()
.toString()}`,
);
}
Loading