xfinity
Version:
Easy to use discord.js multipurpose package for your bot
209 lines (204 loc) • 6.77 kB
JavaScript
const {
MessageEmbed,
User,
MessageReaction,
CommandInteraction,
} = require("discord.js");
const ms = require("ms");
function isNumeric(str) {
if (typeof str != "string") return false;
return !isNaN(str) && !isNaN(parseFloat(str));
}
/**
*
* @param {CommandInteraction} msg
*/
async function giveaway(msg, options = []) {
await msg.followUp({
content:
"Alright! Lets config your giveaway (say end at any time to end the process)",
});
const filter = (m) => {
return m.author.id == msg.member.user.id;
};
await msg.channel.send({
content:
'How long do you want your giveaway to be? \n(E.g 7 days is 7d, 1 month is 1m, 1 week is 1w, 7 minutes is 7mi). Say "help" for all supported formats.',
});
const length = msg.channel.createMessageCollector({ filter, time: 15000 });
length.on("collect", async (m) => {
if (m.content === "end") {
length.stop();
return msg.channel.send({ content: "Ended the giveaway process." });
}
if (m.content.toLowerCase() == "help") {
return msg.channel.send({
content:
"Seconds (s), minutes (mi), hours (h), days (d), week (w), month (m)",
});
}
let splitContent = m.content.split("");
let modifier = splitContent[splitContent.length - 1];
splitContent.pop();
let amountoftime = splitContent.join("");
if (isNaN(amountoftime) == true) {
return msg.channel.send({ content: "Please enter a number." });
}
if (modifier == undefined) {
return msg.channel.send({
content: "Please enter a modifier, like day or month.",
});
}
if (amountoftime == 0) {
return msg.channel.send({ content: "Please enter a non-0 number" });
}
length.stop();
msg.channel.send({ content: "What do you want to give away?" });
const giveawayThing = msg.channel.createMessageCollector({
filter,
time: 15000,
});
giveawayThing.on("collect", async (m) => {
if (m.content == "end") {
giveawayThing.stop();
return msg.channel.send({ content: "Ended the giveaway process." });
}
giveawayItem = m.content;
giveawayThing.stop();
await msg.channel.send({ content: "How many winners do you want?" });
const winnerAmount = msg.channel.createMessageCollector({
filter,
time: 15000,
});
winnerAmount.on("collect", async (m) => {
if (m.content == "end") {
winnerAmount.stop();
return msg.channel.send({ content: "Ended the giveaway process." });
}
let winnersAmount;
if (isNumeric(m.content) == false) {
return msg.channel.send({ content: "Please enter a number." });
}
if (m.content == 0) {
return msg.channel.send({
content: "You need at least one winner!",
});
}
winnersAmount = m.content;
const giveawayEmbed = new MessageEmbed()
.setColor(options.color || "#0099ff ")
.setAuthor(
`This giveaway is active!`,
msg.member.user.displayAvatarURL()
)
.setTitle(`${giveawayItem}`)
.setDescription("React with 🎉 to join!")
.addFields(
{
name: "Time",
value: `${amountoftime}${modifier}`,
inline: true,
},
{
name: "Amount of winners",
value: `${winnersAmount}`,
inline: true,
}
)
.setTimestamp()
.setFooter(
`${amountoftime}${modifier} Giveaway • ${winnersAmount} Winners • ${msg.guild.name} Giveaways`,
msg.guild.iconURL({ dynamic: true })
);
let giveawayEmbedmsg = await msg.channel.send({
embeds: [giveawayEmbed],
});
giveawayEmbedmsg.react("🎉");
let time = [];
time.push(amountoftime);
modifier = modifier.toLowerCase();
if (modifier == "s") {
time.push("seconds");
}
if (modifier == "mi") {
time.push("minutes");
}
if (modifier == "h") {
time.push("hours");
}
if (modifier == "d") {
time.push("days");
} else if (modifier == "w") {
time.push("weeks");
} else if (modifier == "m") {
time.push("months");
}
let timeinms = ms(time.join(" "));
/**
*
* @param {MessageReaction} reaction
* @param {User} user
* @returns
*/
const filter = (reaction, user) => {
if (user.bot) return;
return reaction.emoji.name === "🎉";
};
let col = giveawayEmbedmsg.createReactionCollector({
filter,
time: timeinms,
});
let allReacted = [];
col.on("collect", (reaction, user) => {
if (reaction.emoji.name != "🎉") {
return;
}
allReacted.push(`<@${user.id}>`);
winnerAmount.stop();
setTimeout(async () => {
try {
const kek = determineWinners(allReacted, winnersAmount);
const winners = kek.map((user) => user.toString()).join(", ");
msg.channel
.send({ content: `${winners}` })
.then((tag) => tag.delete());
let newEmbed = new MessageEmbed()
.setTitle(giveawayEmbedmsg.embeds[0].title)
.setColor(giveawayEmbedmsg.embeds[0].hexColor)
.setAuthor(
`This giveaway is over!`,
msg.member.user.displayAvatarURL()
)
.addFields({
name: "Winners",
value: `${winners}`,
})
.setFooter(
options.congratsmsg || "Congratulations to all the winners."
);
await giveawayEmbedmsg.edit({ embeds: [newEmbed] });
} catch (e) {
console.log(e);
}
}, timeinms);
});
});
});
});
}
module.exports = giveaway;
function determineWinners(users, max) {
if (users.length <= max) return users;
const numbers = new Set();
const winnersArray = [];
let i = 0;
while (i < max) {
const random = Math.floor(Math.random() * users.length);
const selected = users[random];
if (!numbers.has(random)) {
winnersArray.push(selected);
i++;
}
}
return winnersArray;
}