Update ESLint rules, enhance environment schema with new metrics and health port, and refactor Redis import. Change TypeScript definitions path in package.json.
This commit is contained in:
39
apps/bot/src/jobs.ts
Normal file
39
apps/bot/src/jobs.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { Queue, Worker } from 'bullmq';
|
||||
import { PermissionFlagsBits } from 'discord.js';
|
||||
import { redis } from './redis.js';
|
||||
import { logger } from './logger.js';
|
||||
import type { BotContext } from './types.js';
|
||||
|
||||
export const moderationQueueName = 'moderation';
|
||||
export const moderationQueue = new Queue(moderationQueueName, { connection: redis });
|
||||
|
||||
type TempBanJob = {
|
||||
guildId: string;
|
||||
userId: string;
|
||||
reason: string | null;
|
||||
};
|
||||
|
||||
export function startWorkers(context: BotContext): Worker[] {
|
||||
const worker = new Worker<TempBanJob>(
|
||||
moderationQueueName,
|
||||
async (job) => {
|
||||
if (job.name !== 'tempBanExpire') {
|
||||
return;
|
||||
}
|
||||
|
||||
const guild = await context.client.guilds.fetch(job.data.guildId);
|
||||
const me = await guild.members.fetchMe();
|
||||
if (!me.permissions.has(PermissionFlagsBits.BanMembers)) {
|
||||
throw new Error('Missing BanMembers permission for temp ban expiration');
|
||||
}
|
||||
await guild.members.unban(job.data.userId, job.data.reason ?? 'Temporary ban expired');
|
||||
},
|
||||
{ connection: redis }
|
||||
);
|
||||
|
||||
worker.on('failed', (job, error) => {
|
||||
logger.error({ jobId: job?.id, error }, 'Moderation job failed');
|
||||
});
|
||||
|
||||
return [worker];
|
||||
}
|
||||
Reference in New Issue
Block a user