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

195 lines (139 loc) 7.39 kB
'use strict'; const Discord = require("discord.js") const { GiveawaysManagerOptions, GiveawaysStartOptions, MessageGiveaways } = require("../utils/Constants.js") const Time = require("../utils/time") const EmbedGeneratorBuilder = require("../utils/EmbedGeneratorBuilder") const Interaction = require("../utils/Interaction") const data = require("../../../data/config.json") module.exports = class GiveawaysManager { /** * Manager * @param {Discord.Client} client */ constructor(client) { this.client = client; this.embedColor = data.color; this.buttonEmoji = data.emoji; this.message = null; } /** * Start the giveaway with the manager. * @param {Discord.Channel} channel * @param {GiveawaysStartOptions} options * @example * manager.start(message.channel, { time: "1d", //one day prize: "Title", winnerNumber: 2, //2 winners interaction: message, //message of your command buttonType: Discord.ButtonStyle.Primary, //Style of button "participate" }) */ async start(channel, options) { try { if (!channel) throw new Error("The channel property is not initialized") if (!options) throw new Error("The option property is not initialized") if (!options.interaction) throw new Error("The option.interaction property is not initialized") if (!options.buttonType) throw new Error("The option.buttonType property is not initialized") if (!options.prize) throw new Error("The option.prize property is not initialized") if (!options.time) throw new Error("The option.time property is not initialized") if (!options.winnerNumber) throw new Error("The option.winnerNumber property is not initialized") if (!options.time.includes("m") && !options.time.includes("h") && !options.time.includes("d")) return options.interaction.reply({ephemeral: true, content: "For the unit of time please enter only the following choices: `m`, `h`, `d`!"}); //Initialisation de diverses choses; let Channel = this.client.channels.cache.get(channel.id); let message = options.interaction; let time = new Time(options.time).msByTheTime(); let ms = new Time(options.time).ms() //vérifier si le module est activé ou pas ?? si 'false' => une erreur (comme quoi le module est désactiver) if (data.giveawayEnable === false) return message.reply({content: `❌ Error: You cannot perform this command because the ability to perform this command is set to 'false'; to be able to execute it please set the value to 'true'`, ephemeral: true}) //Le manager des Embeds let EmbedManager = new EmbedGeneratorBuilder(time, { interaction: message, winnerNumber: options.winnerNumber, Title: options.prize, Color: this.embedColor, Emoji: `${this.buttonEmoji}` }); //l'embed au lancement let Embed = EmbedManager.Embed() // Le manager des intéractions let InteractionManager = new Interaction({ buttonEmoji: this.buttonEmoji, buttonStyle: Discord.ButtonStyle.Primary, client: this.client, winnerNumber: options.winnerNumber }); //Les bouttons let button = InteractionManager.createButton() //Envoie des messages du giveaway au lancement await message.reply({content: "The giveaway has been launched!", ephemeral: true}); await Channel.send({content: `${MessageGiveaways.giveaway}`, embeds: [Embed], components: [button]}).then(async mss => { this.message = mss; }); // Après le giveaways let TimeoutGiveawayEnding = setTimeout(() => { //Initialisation de diverses choses; let winnerRandom = InteractionManager.getWinnerRandom(options.winnerNumber) let entrie = InteractionManager.getEntries() //Le manager des Embeds let EmbedManager = new EmbedGeneratorBuilder(time, { interaction: message, winnerNumber: options.winnerNumber, Title: options.prize, Color: this.embedColor, Emoji: `${this.buttonEmoji}`, winnerRandom: winnerRandom, entries: entrie }); //Les embeds après le giveaway let EntrieEmbed = EmbedManager.entrieEmbed(this.message) let Embed = EmbedManager.endEmbed(entrie) //vérifier si il n'y a personne qui a participer : si oui => une erreur if (winnerRandom.length < 1) { this.message.reply({content: `❌ Error: There must be at least one winner to complete this giveaway!`, embeds: [], ephemeral: true}); return this.message.delete(); } //les messages après le giveaway this.message.reply({content: `Congratulations ${winnerRandom.join(", ")}! You won the **${options.prize}**!`, embeds: [EntrieEmbed]}); this.message.edit({content: `${MessageGiveaways.giveawayEnded}`, embeds: [Embed], components: []}); }, ms) //Le boutton pour Finish this.client.on("interactionCreate", async (interaction) => { if (interaction.customId === InteractionManager.getFinishCustomIdButton()) { if (!interaction.memberPermissions.toArray().includes("KickMembers")) return interaction.reply({content: "❌ Error: You are not authorized to perform this action.", ephemeral: true}) //Initialisation de diverses choses; let winnerRandom = InteractionManager.getWinnerRandom(options.winnerNumber) let entrie = InteractionManager.getEntries() //Le manager des Embeds let EmbedManager = new EmbedGeneratorBuilder(time, { interaction: message, winnerNumber: options.winnerNumber, Title: options.prize, Color: this.embedColor, Emoji: `${this.buttonEmoji}`, winnerRandom: winnerRandom, entries: entrie }); clearTimeout(TimeoutGiveawayEnding) //Les embeds après le giveaway let EntrieEmbed = EmbedManager.entrieEmbed(this.message) let Embed = EmbedManager.endEmbed(entrie, Math.round((Date.now() / 1000) - time)) //vérifier si il n'y a personne qui a participer : si oui => une erreur if (winnerRandom.length < 1) { this.message.reply({content: `❌ Error: There must be at least one winner to complete this giveaway!`, embeds: [], ephemeral: true}); return this.message.delete(); } this.message.reply({content: `Congratulations ${winnerRandom.join(", ")}! You won the **${options.prize}**!`, embeds: [EntrieEmbed]}); this.message.edit({content: `${MessageGiveaways.giveawayEnded}`, embeds: [Embed], components: []}); } }) // créé l'intéraction des bouttons InteractionManager.createInteraction(this.message, TimeoutGiveawayEnding, EmbedManager); } catch (err) {} } }