UNPKG

azury-djs

Version:

Azury-djs, the best NPM Package you can have, with fun Minigames, button interactions, and more! Fun for your bot!

251 lines (232 loc) 7.58 kB
const data = new Set(); const Discord = require('discord.js'); const disbut = require('discord-buttons'); const { convertTime, randomHexColor, getRandomString, checkForUpdates, getRandomSentence, } = require('../../functions/function'); module.exports = async (options) => { checkForUpdates(); if (!options.message) { throw new Error('⚠️ AZURY-DJS ERROR:\n→ message argument was not specified.'); } if (typeof options.message !== 'object') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ Invalid Discord Message was provided.'); } if (!options.embed) options.embed = {}; if (typeof options.embed !== 'object') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ embed must be an object.'); } if (!options.embed.title) { options.embed.title = 'Fast Type • Azury Devs'; } if (typeof options.embed.title !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ embed title must be a string.'); } if (!options.embed.description) { options.embed.description = 'You have **{{time}}** to type the below sentence.'; } if (typeof options.embed.description !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ embed color must be a string.'); } if (!options.embed.color) options.embed.color = randomHexColor(); if (typeof options.embed.color !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ embed color must be a string.'); } if (!options.embed.footer) { options.embed.footer = '©️ Azury Devs'; } if (typeof options.embed.footer !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ embed footer must be a string.'); } if (!options.embed.timestamp) options.embed.timestamp = true; if (typeof options.embed.timestamp !== 'boolean') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ timestamp must be a boolean.'); } if (!options.sentence) { options.sentence = getRandomSentence(Math.floor(Math.random() * 10) + 3) .toString() .split(',') .join(' '); } if (typeof options.sentence !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ sentence must be a string'); } if (!options.winMessage) { options.winMessage = '🎉 Congratulations, you have a wpm of **{{wpm}}** and You made it in **{{time}}**.'; } if (typeof options.winMessage !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ winMessage must be a string.'); } if (!options.loseMessage) options.loseMessage = 'Better luck next time!'; if (typeof options.loseMessage !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ loseMessage must be a string.'); } if (!options.time) options.time = 60000; if (parseInt(options.time) < 10000) { throw new Error( '⚠️ AZURY-DJS ERROR:\n→ time argument must be greater than 10 Seconds (in ms i.e. 10000).', ); } if (typeof options.time !== 'number') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ time must be a number.'); } if (!options.buttonText) options.buttonText = 'Cancel'; if (typeof options.buttonText !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ buttonText must be a string.'); } if (!options.othersMessage) { options.othersMessage = 'Only <@{{author}}> can use the buttons!'; } if (typeof options.othersMessage !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ othersMessage must be a string.'); } if (!options.cancelMessage) options.cancelMessage = 'You ended the game!'; if (typeof options.cancelMessage !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ othersMessage must be a string.'); } if (data.has(options.message.author.id)) return; data.add(options.message.author.id); const id = getRandomString(20) + '-' + getRandomString(20) + '-' + getRandomString(20) + '-' + getRandomString(20); const sentence = options.sentence .toLowerCase() .split(' ') .map((msg) => `\`${msg.split('').join(' ')}\``) .join(' '); const gameCreatedAt = Date.now(); let btn1 = new disbut.MessageButton() .setStyle('red') .setLabel(options.buttonText) .setID(id); const embed = new Discord.MessageEmbed() .setTitle(options.embed.title) .setDescription( `${options.embed.description.replace( '{{time}}', convertTime(options.time), )}`, ) .addField('Sentence:', `${sentence}`) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { embed.setTimestamp(); } const think = await options.message.inlineReply(embed); await think.edit({ embed, components: [{ type: 1, components: [btn1] }], }); const collector = new Discord.MessageCollector( options.message.channel, (m) => !m.author.bot, { time: options.time }, ); collector.on('collect', async (msg) => { if (msg.author.id !== options.message.author.id) return; if (msg.content.toLowerCase().trim() === options.sentence.toLowerCase()) { const time = Date.now() - gameCreatedAt; const minute = (time / 1000 / 60) % 60; const wpm = msg.content.toLowerCase().trim().length / 5 / minute; const _embed = new Discord.MessageEmbed() .setDescription( options.winMessage .replace('{{time}}', convertTime(time)) .replace('{{wpm}}', wpm.toFixed(2)), ) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { _embed.setTimestamp(); } options.message.inlineReply(_embed); btn1 = new disbut.MessageButton() .setStyle('red') .setLabel(options.buttonText) .setDisabled() .setID(id); await think.edit({ embed, components: [{ type: 1, components: [btn1] }], }); collector.stop(msg.author.username); data.delete(options.message.author.id); } else { const _embed = new Discord.MessageEmbed() .setDescription(`${options.loseMessage}`) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { _embed.setTimestamp(); } options.message.inlineReply(_embed); collector.stop(msg.author.username); data.delete(options.message.author.id); btn1 = new disbut.MessageButton() .setStyle('red') .setLabel(options.buttonText) .setDisabled() .setID(id); await think.edit({ embed, components: [{ type: 1, components: [btn1] }], }); } }); collector.on('end', async (_collected, reason) => { if (reason === 'time') { const _embed = new Discord.MessageEmbed() .setDescription(`${options.loseMessage}`) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { _embed.setTimestamp(); } options.message.inlineReply(_embed); btn1 = new disbut.MessageButton() .setStyle('red') .setLabel(options.buttonText) .setDisabled() .setID(id); await think.edit({ embed, components: [{ type: 1, components: [btn1] }], }); data.delete(options.message.author.id); } }); const gameCollector = think.createButtonCollector((fn) => fn); gameCollector.on('collect', (button) => { if (button.clicker.user.id !== options.message.author.id) { return button.reply.send( options.othersMessage.replace('{{author}}', options.message.author.id), true, ); } btn1 = new disbut.MessageButton() .setStyle('red') .setLabel(options.buttonText) .setDisabled() .setID(id); think.edit({ embed: embed, components: [{ type: 1, components: [btn1] }], }); button.reply.send(options.cancelMessage, true); gameCollector.stop(); data.delete(options.message.author.id); return collector.stop(); }); };