sympact
sympact
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
So I got this from gpt as I dont know what the heck cron schedule is
import cron from 'node-cron';
import { PrismaClient } from '@prisma/client';
import { TextChannel } from 'discord.js';
const prisma = new PrismaClient();


class Scheduler {
public start() {
// Run every minute
cron.schedule('* * * * *', async () => {
const now = new Date();
const giveaways = await prisma.giveaway.findMany({
where: {
endsAt: {
lte: now,
},
},
});

for (const giveaway of giveaways) {
await this.endGiveaway(giveaway.id);
}
});
}

private async endGiveaway(giveawayId: number) {
const giveaway = await prisma.giveaway.findUnique({
where: { id: giveawayId },
});

if (!giveaway) return;

const channel = await client.channels.fetch(giveaway.channelId) as TextChannel;
if (!channel) return;

const message = await channel.messages.fetch(giveaway.messageId);
if (!message) return;

const users = await message.reactions.resolve('🎉')?.users.fetch();
if (!users) return;

const participants = users.filter(user => !user.bot).map(user => user.id);
if (participants.length === 0) {
await channel.send(`No participants for the giveaway: ${giveaway.prize}`);
return;
}

const winners = this.pickWinners(participants, giveaway.winners);

await channel.send(`Congratulations to the winners of the giveaway for **${giveaway.prize}**: ${winners.map(id => `<@${id}>`).join(', ')}`);

await prisma.giveaway.delete({
where: { id: giveawayId },
});
}

private pickWinners(participants: string[], winnerCount: number): string[] {
const winners = new Set<string>();
while (winners.size < winnerCount && winners.size < participants.length) {
const randomIndex = Math.floor(Math.random() * participants.length);
winners.add(participants[randomIndex]);
}
return Array.from(winners);
}
}

const scheduler = new Scheduler();
export default scheduler;
import cron from 'node-cron';
import { PrismaClient } from '@prisma/client';
import { TextChannel } from 'discord.js';
const prisma = new PrismaClient();


class Scheduler {
public start() {
// Run every minute
cron.schedule('* * * * *', async () => {
const now = new Date();
const giveaways = await prisma.giveaway.findMany({
where: {
endsAt: {
lte: now,
},
},
});

for (const giveaway of giveaways) {
await this.endGiveaway(giveaway.id);
}
});
}

private async endGiveaway(giveawayId: number) {
const giveaway = await prisma.giveaway.findUnique({
where: { id: giveawayId },
});

if (!giveaway) return;

const channel = await client.channels.fetch(giveaway.channelId) as TextChannel;
if (!channel) return;

const message = await channel.messages.fetch(giveaway.messageId);
if (!message) return;

const users = await message.reactions.resolve('🎉')?.users.fetch();
if (!users) return;

const participants = users.filter(user => !user.bot).map(user => user.id);
if (participants.length === 0) {
await channel.send(`No participants for the giveaway: ${giveaway.prize}`);
return;
}

const winners = this.pickWinners(participants, giveaway.winners);

await channel.send(`Congratulations to the winners of the giveaway for **${giveaway.prize}**: ${winners.map(id => `<@${id}>`).join(', ')}`);

await prisma.giveaway.delete({
where: { id: giveawayId },
});
}

private pickWinners(participants: string[], winnerCount: number): string[] {
const winners = new Set<string>();
while (winners.size < winnerCount && winners.size < participants.length) {
const randomIndex = Math.floor(Math.random() * participants.length);
winners.add(participants[randomIndex]);
}
return Array.from(winners);
}
}

const scheduler = new Scheduler();
export default scheduler;
but it couldnt find the client. Made it in src/Scheduler.ts
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
I know
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
but normally designed for games indeed
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
like portainer
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
its just a docker management system
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
ill stick with nodecron then ig
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
and just localhost when dev
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
pterodactyl
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
do I need to host another serve rthenm
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
I want to host 1 single thing and thats the bot xd
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
but why redis etc
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
this one works
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
google
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
found
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
not found
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
sapphire has everything
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
damn
88 replies
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
node-cron?
88 replies