UNPKG

discord-giveaway-easy

Version:

A module allowing the simple creation of a discord giveaway with your bot supported by Discord.js^14.0.0

141 lines (96 loc) 3.95 kB
const Discord = require("discord.js"); const {CreateInteraction, MessageGiveaways} = require("./Constants") const EmbedGeneratorBuilder = require("./EmbedGeneratorBuilder") const {CustomId} = require("./functions") module.exports = class Interaction { /** * * @param {CreateInteraction} options */ constructor(options) { this.buttonEmoji = options.buttonEmoji; this.buttonStyle = options.buttonStyle; this.client = options.client; this.winnerNumber = options.winnerNumber; this.customID = CustomId(); this.customIdStopButton = CustomId(); this.customIdFinish = CustomId(); this.customIdReroll = CustomId(); this.memberParticipate = []; } /** * @param {Discord.Message} message * @param {Object} timeout * @param {EmbedGeneratorBuilder} embedbuilder */ createInteraction(message, timeout, embedbuilder) { this.client.on("interactionCreate", async (interaction) => { if (interaction.type === Discord.InteractionType.MessageComponent) { if (interaction.customId === this.customID) { if (this.memberParticipate.includes(interaction.user.id)) return interaction.reply({content: "You are already in the giveaway!", ephemeral: true}) await interaction?.reply({content: 'You have been queued for this giveaway. Good luck!', ephemeral: true}) this.memberParticipate.push(interaction.user.id) let embed = embedbuilder.editEmbed(this.memberParticipate.length) await message.edit({embeds: [embed]}) } if (interaction.customId === this.customIdStopButton) { if (!interaction.memberPermissions.toArray().includes("KickMembers")) return interaction.reply({content: "❌ Error: You are not authorized to perform this action.", ephemeral: true}) let EmbedStopped = new Discord.EmbedBuilder() .setDescription(`This giveaway has been stopped!\n${this.memberParticipate.length < 1 ? "" : `Entries: **${this.memberParticipate.length}**.`}`) .setColor("#2f3136") .setTimestamp() clearTimeout(timeout); await message.edit({content: MessageGiveaways.giveawayStoped, embeds: [EmbedStopped], components: [], ephemeral: true}) return interaction.reply({content: "The giveaway has been successfully stopped", ephemeral: true}) } } }); } createButton() { let button = new Discord.ActionRowBuilder() .addComponents( new Discord.ButtonBuilder() .setCustomId(this.customID) .setLabel('Participate') .setEmoji(this.buttonEmoji) .setStyle(this.buttonStyle) ) .addComponents( new Discord.ButtonBuilder() .setCustomId(this.customIdStopButton) .setLabel("Stop") .setEmoji("⛔") .setStyle(Discord.ButtonStyle.Danger) ) .addComponents( new Discord.ButtonBuilder() .setCustomId(this.customIdFinish) .setLabel("Finish") .setEmoji("↗️") .setStyle(Discord.ButtonStyle.Success) ) return button; } getFinishCustomIdButton() { return this.customIdFinish; } getEntries() { return this.memberParticipate.length; } /** * * @param {Number} number */ getWinnerRandom(number) { let tirage = []; let giveawayRamdom = this.memberParticipate; if (number > giveawayRamdom.length) number = giveawayRamdom.length; for (let i = 0; i < number; i++) { let random = `${giveawayRamdom[Math.floor(Math.random() * giveawayRamdom.length)]}` tirage.push(`<@${random}>`) let index = giveawayRamdom.indexOf(random) delete giveawayRamdom[index]; } return tirage; } }