UNPKG

azury-djs

Version:

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

643 lines (614 loc) 19.8 kB
const data = new Set(); const db = require('quick.db'); const Discord = require('discord.js'); const { MessageButton, MessageActionRow } = require('discord-buttons'); const { randomHexColor, checkForUpdates, getRandomString, } = 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.opponent) { throw new Error('⚠️ AZURY-DJS ERROR:\n→ opponent argument was not specified.'); } if (typeof options.opponent !== 'object') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ Invalid Discord User 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 = 'Fight • Azury Devs'; } if (typeof options.embed.title !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ embed title 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 = '©️ Weky Development'; } 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→ setTimestamp must be a boolean.'); } if (!options.buttons) options.buttons = {}; if (typeof options.buttons !== 'object') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ buttons must be an object.'); } if (!options.buttons.hit) { options.buttons.hit = 'Hit'; } if (typeof options.buttons.hit !== 'string') { throw new Error('⚠️ AZURY-DJS ERROR:\n→ hit button text must be a string.'); } if (!options.buttons.heal) { options.buttons.heal = 'Heal'; } if (typeof options.buttons.heal !== 'string') { throw new Error('⚠️ AZURY-DJS ERROR:\n→ heal button text must be a string.'); } if (!options.buttons.cancel) { options.buttons.cancel = 'Stop'; } if (typeof options.buttons.cancel !== 'string') { throw new Error('⚠️ AZURY-DJS ERROR:\n→ cancel button text must be a string.'); } if (!options.buttons.accept) { options.buttons.accept = 'Accept'; } if (typeof options.buttons.accept !== 'string') { throw new Error('⚠️ AZURY-DJS ERROR:\n→ accept button text must be a string.'); } if (!options.buttons.deny) { options.buttons.deny = 'Deny'; } if (typeof options.buttons.deny !== 'string') { throw new Error('⚠️ AZURY-DJS ERROR:\n→ deny button text must be a string.'); } if (!options.acceptMessage) { options.acceptMessage = '<@{{challenger}}> has challenged <@{{opponent}}> for a fight!'; } if (typeof options.acceptMessage !== 'string') { throw new Error('⚠️ AZURY-DJS ERROR:\n→ acceptMessage must be a string.'); } if (!options.winMessage) { options.winMessage = 'GG, <@{{winner}}> won the fight!'; } if (typeof options.winMessage !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ winMessage must be a string.'); } if (!options.endMessage) { options.endMessage = '<@{{opponent}}> didn\'t answer in time. So, I dropped the game!'; } if (typeof options.endMessage !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ endMessage must be a string.'); } if (!options.cancelMessage) { options.cancelMessage = '<@{{opponent}}> refused to have a fight with you!'; } if (typeof options.cancelMessage !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ cancelMessage must be a string.'); } if (!options.fightMessage) { options.fightMessage = '{{player}} you go first!'; } if (typeof options.fightMessage !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ fightMessage 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.opponentsTurnMessage) { options.opponentsTurnMessage = 'Please wait for your opponents move!'; } if (typeof options.opponentsTurnMessage !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ opponentsTurnMessage must be a string.'); } if (!options.highHealthMessage) { options.highHealthMessage = 'You cannot heal if your HP is above 80!'; } if (typeof options.highHealthMessage !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ highHealthMessage must be a string.'); } if (!options.lowHealthMessage) { options.lowHealthMessage = 'You cannot cancel the fight if your HP is below 50!'; } if (typeof options.lowHealthMessage !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ lowHealthMessage must be a string.'); } if (!options.returnWinner) options.returnWinner = false; if (typeof options.returnWinner !== 'boolean') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ buttonText must be a boolean.'); } if (data.has(options.message.author.id) || data.has(options.opponent.id)) { return; } data.add(options.message.author.id); data.add(options.opponent.id); const id1 = getRandomString(20) + '-' + getRandomString(20) + '-' + getRandomString(20) + '-' + getRandomString(20); const id2 = getRandomString(20) + '-' + getRandomString(20) + '-' + getRandomString(20) + '-' + getRandomString(20); const id3 = getRandomString(20) + '-' + getRandomString(20) + '-' + getRandomString(20) + '-' + getRandomString(20); const oppenent = options.opponent; const challenger = options.message.author; if (oppenent.bot || oppenent.id === challenger.id) return; let acceptbutton = new MessageButton() .setStyle('green') .setLabel(options.buttons.accept) .setID('accept'); let denybutton = new MessageButton() .setStyle('red') .setLabel(options.buttons.deny) .setID('deny'); let component = new MessageActionRow().addComponent([ acceptbutton, denybutton, ]); const embed = new Discord.MessageEmbed() .setTitle(options.embed.title) .setDescription( options.acceptMessage .replace('{{challenger}}', challenger.id) .replace('{{opponent}}', oppenent.id), ) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { embed.setTimestamp(); } const question = await options.message.inlineReply({ embed, }); question.edit({ embed, component, }); const Collector = await question.createButtonCollector((fn) => fn, { time: 60000, }); Collector.on('collect', async (_btn) => { if (_btn.clicker.member.id !== oppenent.id) { return _btn.reply.send( options.othersMessage.replace('{{author}}', `<@${oppenent.id}>`), true, ); } _btn.reply.defer(); if (_btn.id === 'deny') { acceptbutton = new MessageButton() .setDisabled() .setStyle('green') .setLabel(options.buttons.accept) .setID('accept'); denybutton = new MessageButton() .setDisabled() .setStyle('red') .setLabel(options.buttons.deny) .setID('deny'); component = new MessageActionRow().addComponent([ acceptbutton, denybutton, ]); const emd = new Discord.MessageEmbed() .setTitle(options.embed.title) .setDescription( options.cancelMessage.replace('{{opponent}}', oppenent.id), ) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { emd.setTimestamp(); } Collector.stop(); data.delete(options.opponent.id); data.delete(options.message.author.id); return question.edit({ embed: emd, component, }); } else if (_btn.id === 'accept') { Collector.stop(); const challengerHealth = 100; const oppenentHealth = 100; const challengerLastAttack = 'heal'; const oppenentLastAttack = 'heal'; const gameData = [ { member: challenger, health: challengerHealth, lastAttack: challengerLastAttack, }, { member: oppenent, health: oppenentHealth, lastAttack: oppenentLastAttack, }, ]; let player = Math.floor(Math.random() * gameData.length); let btn1 = new MessageButton() .setLabel(options.buttons.hit) .setID(id1) .setStyle('red'); let btn2 = new MessageButton() .setLabel(options.buttons.heal) .setID(id2) .setStyle('green'); let btn3 = new MessageButton() .setLabel(options.buttons.cancel) .setID(id3) .setStyle('grey'); let row = new MessageActionRow() .addComponent(btn1) .addComponent(btn2) .addComponent(btn3); const _embed = new Discord.MessageEmbed() .setTitle(options.embed.title) .setDescription( options.fightMessage.replace('{{player}}', gameData[player].member), ) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { _embed.setTimestamp(); } question.edit({ embed: _embed, components: row, }); const checkHealth = (member) => { if (gameData[member].health <= 0) return true; else return false; }; const gameCollector = question.createButtonCollector((fn) => fn); gameCollector.on('collect', async (msg) => { if (gameData.some((x) => x.member.id === msg.clicker.member.id)) { if (!checkHealth(player)) { const btn = msg.clicker.member; if (msg.id === id1) { if (btn.id !== gameData[player].member.id) { return msg.reply.send(options.opponentsTurnMessage, true); } msg.reply.defer(); let randNumb = Math.floor(Math.random() * parseInt(options.dmgMax)) + parseInt(options.dmgMin) || Math.floor(Math.random() * 15) + 4; const tempPlayer = (player + 1) % 2; if (gameData[tempPlayer].lastAttack === 'heal') { randNumb = Math.floor(randNumb / 2); } gameData[tempPlayer].health -= randNumb; gameData[player].lastAttack = 'attack'; if (gameData[player].member.id == options.message.author.id) { const __embed = new Discord.MessageEmbed() .setTitle(options.embed.title) .setDescription( `(:punch:) ${gameData[player].member.username} — ${gameData[player].health} HP - **versus** - **${gameData[tempPlayer].member.username}** — ${gameData[tempPlayer].health} HP`, ) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { __embed.setTimestamp(); } question.edit({ embed: __embed, components: row, }); } else if (gameData[player].member.id == options.opponent.id) { const __embed = new Discord.MessageEmbed() .setTitle(options.embed.title) .setDescription( `**${gameData[tempPlayer].member.username}** — ${gameData[tempPlayer].health} HP - **versus** - ${gameData[player].member.username} — ${gameData[player].health} HP (:punch:)`, ) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { __embed.setTimestamp(); } question.edit({ embed: __embed, components: row, }); } player = (player + 1) % 2; } else if (msg.id === id2) { if (btn.id !== gameData[player].member.id) { return msg.reply.send(options.opponentsTurnMessage, true); } if (gameData[player].health > 80) { return msg.reply.send(options.highHealthMessage, true); } else { msg.reply.defer(); let randNumb = Math.floor(Math.random() * parseInt(options.healMax)) + parseInt(options.healMin) || Math.floor(Math.random() * 10) + 4; const tempPlayer = (player + 1) % 2; if (gameData[tempPlayer].lastAttack === 'heal') { randNumb = Math.floor(randNumb / 2); } gameData[player].health += randNumb; gameData[player].lastAttack = 'heal'; if (gameData[player].member.id == options.message.author.id) { const __embed = new Discord.MessageEmbed() .setTitle(options.embed.title) .setDescription( `(:hearts:) ${gameData[player].member.username} — ${gameData[player].health} HP - **versus** - **${gameData[tempPlayer].member.username}** — ${gameData[tempPlayer].health} HP`, ) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { __embed.setTimestamp(); } question.edit({ embed: __embed, components: row, }); } else if (gameData[player].member.id == options.opponent.id) { const __embed = new Discord.MessageEmbed() .setTitle(options.embed.title) .setDescription( `**${gameData[tempPlayer].member.username}** — ${gameData[tempPlayer].health} HP - **versus** - ${gameData[player].member.username} — ${gameData[player].health} HP (:hearts:)`, ) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { __embed.setTimestamp(); } question.edit({ embed: __embed, components: row, }); } player = (player + 1) % 2; } } else if (msg.id === id3) { if (btn.id !== gameData[player].member.id) { return msg.reply.send(options.opponentsTurnMessage, true); } if (gameData[player].health < 50) { return msg.reply.send(options.lowHealthMessage, true); } else { msg.reply.defer(); btn1 = new MessageButton() .setLabel(options.buttons.hit) .setID(id1) .setStyle('red') .setDisabled(); btn2 = new MessageButton() .setLabel(options.buttons.heal) .setID(id2) .setStyle('green') .setDisabled(); btn3 = new MessageButton() .setLabel(options.buttons.cancel) .setID(id3) .setStyle('grey') .setDisabled(); row = new MessageActionRow() .addComponent(btn1) .addComponent(btn2) .addComponent(btn3); gameCollector.stop(); data.delete(options.opponent.id); data.delete(options.message.author.id); const __embed = new Discord.MessageEmbed() .setTitle(options.embed.title) .setDescription( options.cancelMessage.replace( '{{opponent}}', gameData[player].member.id, ), ) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { __embed.setTimestamp(); } question.edit({ embed: __embed, components: row, }); } } if (checkHealth(player)) { btn1 = new MessageButton() .setLabel(options.buttons.hit) .setID(id1) .setStyle('red') .setDisabled(); btn2 = new MessageButton() .setLabel(options.buttons.heal) .setID(id2) .setStyle('green') .setDisabled(); btn3 = new MessageButton() .setLabel(options.buttons.cancel) .setID(id3) .setStyle('grey') .setDisabled(); row = new MessageActionRow() .addComponent(btn1) .addComponent(btn2) .addComponent(btn3); gameCollector.stop(); data.delete(options.opponent.id); data.delete(options.message.author.id); const tempPlayer = (player + 1) % 2; const __embed = new Discord.MessageEmbed() .setTitle(options.embed.title) .setDescription( options.winMessage.replace( '{{winner}}', gameData[tempPlayer].member.id, ), ) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { __embed.setTimestamp(); } if (options.returnWinner) { if (!options.gameID) { throw new Error( '⚠️ AZURY-DJS ERROR:\n→ gameID argument was not specified.', ); } if ( typeof options.gameID !== 'string' ) { throw new TypeError( '⚠️ AZURY-DJS ERROR:\n→ gameID must be a string.', ); } db.set( `Fight_${options.message.guild.id}_${options.gameID}`, gameData[tempPlayer].member.id, ); } question.edit({ embed: __embed, components: row, }); } } else { btn1 = new MessageButton() .setLabel(options.buttons.hit) .setID(id1) .setStyle('red') .setDisabled(); btn2 = new MessageButton() .setLabel(options.buttons.heal) .setID(id2) .setStyle('green') .setDisabled(); btn3 = new MessageButton() .setLabel(options.buttons.cancel) .setID(id3) .setStyle('grey') .setDisabled(); gameCollector.stop(); data.delete(options.opponent.id); data.delete(options.message.author.id); const tempPlayer = (player + 1) % 2; const __embed = new Discord.MessageEmbed() .setTitle(options.embed.title) .setDescription( options.winMessage.replace( '{{winner}}', gameData[tempPlayer].member.id, ), ) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { __embed.setTimestamp(); } if (options.returnWinner) { if (!options.gameID) { throw new Error( '⚠️ AZURY-DJS ERROR:\n→ gameID argument was not specified.', ); } if (typeof options.gameID !== 'string') { throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ gameID must be a string.'); } db.set( `Fight_${options.message.guild.id}_${options.gameID}`, gameData[tempPlayer].member.id, ); } question.edit({ embed: __embed, components: row, }); } } else { return msg.reply.send( options.othersMessage.replace( '{{author}}', `<@${challenger.id}> and <@${oppenent.id}>`, ), true, ); } }); } }); Collector.on('end', async (msg, reason) => { if (reason === 'time') { acceptbutton = new MessageButton() .setDisabled() .setStyle('green') .setLabel(options.buttons.accept) .setID('accept'); denybutton = new MessageButton() .setDisabled() .setStyle('red') .setLabel(options.buttons.deny) .setID('deny'); component = new MessageActionRow().addComponent([ acceptbutton, denybutton, ]); const _embed = new Discord.MessageEmbed() .setTitle(options.embed.title) .setDescription(options.endMessage.replace('{{opponent}}', oppenent.id)) .setFooter(options.embed.footer) .setColor(options.embed.color); if (options.embed.timestamp) { _embed.setTimestamp(); } data.delete(options.opponent.id); data.delete(options.message.author.id); return question.edit({ embed: _embed, component, }); } }); };