Enhance giveaway job handling and update environment configuration
- Added support for a new `giveawayCreate` job in the bot's job processing, allowing for giveaways to be created via the WebUI. - Introduced `runGiveawayCreateJob` function to handle the creation of giveaways, ensuring consistent behavior with existing commands. - Updated `.env.example` to clarify the usage of `BOT_TOKEN` for both the bot and WebUI, enhancing documentation for environment variables. - Added `bullmq` dependency in the WebUI package for job management capabilities.
This commit is contained in:
@@ -12,7 +12,7 @@ import { completeVerification } from './modules/verification/handlers.js';
|
||||
import { closePoll } from './modules/utility/poll.js';
|
||||
import { sendReminder } from './modules/utility/reminders.js';
|
||||
import { expireSelfRole } from './modules/selfroles/service.js';
|
||||
import { endGiveaway } from './modules/giveaways/index.js';
|
||||
import { endGiveaway, runGiveawayCreateJob } from './modules/giveaways/index.js';
|
||||
import { closeInactiveTickets } from './modules/tickets/index.js';
|
||||
import { expireBirthdayRole, runBirthdayCheck } from './modules/birthdays/index.js';
|
||||
import { ensureStatsRecurringJobs, startStatsWorker } from './modules/stats/index.js';
|
||||
@@ -76,6 +76,19 @@ type GiveawayEndJob = {
|
||||
giveawayId: string;
|
||||
};
|
||||
|
||||
type GiveawayCreateJob = {
|
||||
guildId: string;
|
||||
channelId: string;
|
||||
hostId: string;
|
||||
prize: string;
|
||||
winnerCount: number;
|
||||
durationMs: number;
|
||||
requiredRoleId?: string | null;
|
||||
requiredLevel?: number | null;
|
||||
requiredMemberDays?: number | null;
|
||||
};
|
||||
|
||||
|
||||
type BirthdayRoleExpireJob = {
|
||||
guildId: string;
|
||||
userId: string;
|
||||
@@ -187,13 +200,16 @@ export function startWorkers(context: BotContext): Worker[] {
|
||||
logger.error({ jobId: job?.id, error }, 'Ticket queue job failed');
|
||||
});
|
||||
|
||||
const giveawayWorker = new Worker<GiveawayEndJob>(
|
||||
const giveawayWorker = new Worker<GiveawayEndJob | GiveawayCreateJob>(
|
||||
giveawayQueueName,
|
||||
async (job) => {
|
||||
if (job.name !== 'giveawayEnd') {
|
||||
if (job.name === 'giveawayEnd') {
|
||||
await endGiveaway(context, (job.data as GiveawayEndJob).giveawayId);
|
||||
return;
|
||||
}
|
||||
await endGiveaway(context, job.data.giveawayId);
|
||||
if (job.name === 'giveawayCreate') {
|
||||
return runGiveawayCreateJob(context, job.data as GiveawayCreateJob);
|
||||
}
|
||||
},
|
||||
{ connection: redis }
|
||||
);
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export { giveawayCommands } from './commands.js';
|
||||
export { isGiveawayButton, handleGiveawayButton } from './buttons.js';
|
||||
export { endGiveaway, scheduleGiveawayEnd } from './service.js';
|
||||
export { endGiveaway, scheduleGiveawayEnd, runGiveawayCreateJob } from './service.js';
|
||||
|
||||
@@ -408,6 +408,31 @@ export async function deleteGiveaway(context: BotContext, giveawayId: string): P
|
||||
await context.prisma.giveaway.delete({ where: { id: giveawayId } });
|
||||
}
|
||||
|
||||
/**
|
||||
* Entry point for the `giveawayCreate` BullMQ job, enqueued by the WebUI
|
||||
* dashboard (which has no discord.js Client to post the giveaway message
|
||||
* itself). Resolves the guild's locale and delegates to {@link startGiveaway}
|
||||
* so dashboard-created giveaways behave identically to `/giveaway start`.
|
||||
*/
|
||||
export async function runGiveawayCreateJob(
|
||||
context: BotContext,
|
||||
params: {
|
||||
guildId: string;
|
||||
channelId: string;
|
||||
hostId: string;
|
||||
prize: string;
|
||||
winnerCount: number;
|
||||
durationMs: number;
|
||||
requiredRoleId?: string | null;
|
||||
requiredLevel?: number | null;
|
||||
requiredMemberDays?: number | null;
|
||||
}
|
||||
): Promise<{ id: string }> {
|
||||
const locale = await getGuildLocale(context.prisma, params.guildId);
|
||||
const giveaway = await startGiveaway(context, params, locale);
|
||||
return { id: giveaway.id };
|
||||
}
|
||||
|
||||
export async function listActiveGiveaways(
|
||||
context: BotContext,
|
||||
guildId: string
|
||||
|
||||
Reference in New Issue
Block a user