So in my code everything works, the only thing is that if the bot is crashed/restarted the giveaway does not work anymore, so like it does not do anything in the code anymore. If you can try to use node-cron that’s fine too but I can’t figure it out.
import { giveaway, giveaway_endDate } from "../../data/mongodb.js";
import ms from "ms";
import { v4 as uuidv4 } from 'uuid';
export const data = {
name: "giveawaycreate",
type: 1,
description: "Creates a new giveaway",
options: [
{
type: 3, // STRING Type
name: "name",
description: "Title of giveaway",
required: true
},
{
type: 4, // INTEGER Type
name: "winners",
description: "Number of winners",
required: true
},
{
type: 3, // STRING Type
name: "duration",
description: "Duration of the giveaway",
required: true
}
],
dm_permission: false,
default_member_permissions: 0
};
export async function execute(interaction, client) {
await interaction.deferReply({ ephemeral: true });
const title = interaction.options.getString("name", true);
const winnerCount = interaction.options.getInteger("winners", true);
const durationOption = interaction.options.getString("duration", true);
const msDuration = await ms(durationOption);
const endDate = new Date(Date.now() + msDuration);
const formattedEndDate = `${endDate.getMonth() + 1}/${endDate.getDate()}/${endDate.getFullYear()}`;
const unix = Math.floor(endDate / 1000);
const remainingTime = `<t:${unix}:R>`;
const giveawayId = uuidv4();
const embed = {
color: client.settings.color,
footer: { text: `${winnerCount} winners | Ends at u2022 ${formattedEndDate}` },
url: "https://www.google.com/search?q=hello+world",
image: { url: `https://thumbs2.imgbox.com/a2/41/pNr8rADZ_t.png` },
description: `### ${title}`,
fields: [
{ name: "Time Remaining", value: `${remainingTime}`, inline: true },
{ name: "Hosted by", value: `${interaction.user}`, inline: true },
],
};
const hiddenEmbed = {
description: giveawayId,
url: "https://www.google.com/search?q=hello+world"
};
const giveaway_enter = {
type: 1,
components: [{
type: 2,
label: `Enter`,
emoji: "<:tada:1230682264384311357>",
style: 3,
custom_id: `giveaway_enter`,
}, {
type: 2,
label: `Leave`,
style: 2,
custom_id: `giveaway_leave`,
}]
};
const originalReply = await interaction.channel?.send({ embeds: , ephemeral: false, components: [giveaway_enter] });
await interaction.deleteReply();
const logChannel = interaction.guild.channels.cache.get(client.settings.hrLogChannelId);
if (!logChannel) {
return interaction.reply({
content: "CONFIGURATION ERROR: Check Log Channel ID!",
ephemeral: true,
});
}
const logEmbed = {
title: "SFRA Command Logging",
description: `${interaction.user} used **giveaway** command in <#${interaction.channel.id}>.`,
fields: [
{ name: "Moderator", value: `${interaction.user}`, inline: false },
{ name: "Name", value: `${title}`, inline: false },
{ name: "Winners", value: `${winnerCount}`, inline: false },
{ name: "Time Remaining", value: `${remainingTime}`, inline: false },
],
color: client.settings.color,
footer: { text: `${interaction.guild.name}`, icon_url: interaction.guild.iconURL() || undefined },
};
logChannel.send({ embeds: [logEmbed], ephemeral: false });
const giveawayEndDateSave = new giveaway_endDate({
endDate: endDate,
});
await giveawayEndDateSave.save();
const interval = setInterval(async () => {
const currentDate = new Date();
if (currentDate >= endDate) {
clearInterval(interval);
const winners = await selectWinnersFromDatabase(giveawayId, winnerCount);
embed.fields.push({ name: "Winners", value: winners.map(winner => `<@${winner}>`).join('n'), inline: true });
await originalReply.edit({ embeds: , components: [] });
await giveaway_endDate.deleteMany({ endDate: endDate });
await giveaway.deleteMany({ giveawayId: giveawayId });
const winnerIds = winners.map(winner => `<@${winner}>`).join(' ');
await interaction.channel.send(`Congratulations ${winnerIds}, you won ${title}!`);
await clearGiveawayData(giveawayId);
}
}, 60000);
}
async function selectWinnersFromDatabase(giveawayId, winnerCount) {
const participants = await giveaway.find({ giveawayId });
shuffleArray(participants);
return participants.slice(0, winnerCount).map(participant => participant.userId);
}
async function clearGiveawayData(giveawayId) {
await giveaway.deleteMany({ giveawayId });
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
Please ignore this part it wants me to put more things than code so i am going to just put a lot of “g’s”.
ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg