azury-djs
Version:
Azury-djs, the best NPM Package you can have, with fun Minigames, button interactions, and more! Fun for your bot!
292 lines (259 loc) • 8.5 kB
JavaScript
const currentGames = new Object();
const fetch = require('node-fetch');
const Discord = require('discord.js');
const functions = require('../../functions/function');
module.exports = async (options) => {
functions.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 = 'Quick Find';
}
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 = functions.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.backgroundhex) {
options.backgroundhex = '000000';
}
if (typeof options.backgroundhex !== 'string') {
throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ backgroundhex must be a string.');
}
if(!options.texthex) {
options.texthex = options.embed.color.slice(1);
}
if(typeof options.texthex !== 'string') {
throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ texthex must be a string.');
}
if(!options.textlength) {
options.textlength = 7;
}
if(typeof options.textlength !== 'number') {
throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ textlength must be a number.');
}
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.waitMessage) {
options.waitMessage = 'The buttons may appear anytime now!';
}
if (typeof options.waitMessage !== 'string') {
throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ waitMessage must be a string.');
}
if (!options.startMessage) {
options.startMessage =
'First person to press the correct button will win. You have **{{time}}**!';
}
if (typeof options.startMessage !== 'string') {
throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ startMessage must be a string.');
}
if (!options.winMessage) {
options.winMessage =
'GG, <@{{winner}}> pressed the button in **{{time}} seconds**.';
}
if (typeof options.winMessage !== 'string') {
throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ startMessage must be a string.');
}
if (!options.loseMessage) {
options.loseMessage =
'No one pressed the button in time. So, I dropped the game!';
}
if (typeof options.loseMessage !== 'string') {
throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ startMessage must be a string.');
}
if (!options.emoji) options.emoji = '👆';
if (typeof options.emoji !== 'string') {
throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ emoji must be a string.');
}
if (!options.ongoingMessage) {
options.ongoingMessage =
'A game is already runnning in <#{{channel}}>. You can\'t start a new one!';
}
if (typeof options.ongoingMessage !== 'string') {
throw new TypeError('⚠️ AZURY-DJS ERROR:\n→ ongoingMessage must be a string.');
}
if (currentGames[options.message.guild.id]) {
const embed = new Discord.MessageEmbed()
.setTitle(options.embed.title)
.setColor(options.embed.color)
.setFooter(options.embed.footer)
.setDescription(
options.ongoingMessage.replace(
'{{channel}}',
currentGames[`${options.message.guild.id}_channel`],
),
);
if (options.embed.timestamp) {
embed.setTimestamp();
}
return options.message.reply({ embeds: [embed] });
}
if(options.backgroundhex.startsWith('#')) {
options.backgroundhex.slice(1);
}
if(options.texthex.startsWith('#')) {
options.texthex.slice(1);
}
const data = await fetch(`https://fun-api.sujalgoel.engineer/captcha?TextHex=${options.texthex}&BackgroundHex=${options.backgroundhex}&length=${options.textlength}`).then(res => res.json());
const embed = new Discord.MessageEmbed()
.setTitle(options.embed.title)
.setColor(options.embed.color)
.setFooter(options.embed.footer)
.setDescription(options.waitMessage);
if (options.embed.timestamp) {
embed.setTimestamp();
}
const msg = await options.message.reply({ embeds: [embed] });
currentGames[options.message.guild.id] = true;
currentGames[`${options.message.guild.id}_channel`] =
options.message.channel.id;
setTimeout(async function() {
const rows = [];
const buttons = [];
const gameCreatedAt = Date.now();
for (let i = 0; i < 24; i++) {
buttons.push(
new Discord.MessageButton()
.setLabel(data.othertext[i])
.setStyle('PRIMARY')
.setCustomId(functions.getRandomString(20)),
);
}
buttons.push(
new Discord.MessageButton()
.setStyle('PRIMARY')
.setLabel(data.text)
.setCustomId('CORRECT'),
);
functions.shuffleArray(buttons);
for (let i = 0; i < 5; i++) {
rows.push(new Discord.MessageActionRow());
}
rows.forEach((row, i) => {
row.addComponents(buttons.slice(0 + i * 5, 5 + i * 5));
});
const _embed = new Discord.MessageEmbed()
.setTitle(options.embed.title)
.setColor(options.embed.color)
.setFooter(options.embed.footer)
.setImage(data.url)
.setDescription(
options.startMessage.replace(
'{{time}}',
functions.convertTime(options.time),
),
);
if (options.embed.timestamp) {
_embed.setTimestamp();
}
await msg.edit({
embeds: [_embed],
components: rows,
});
const Collector = msg.createMessageComponentCollector({
filter: (fn) => fn,
time: options.time,
});
Collector.on('collect', async (button) => {
if (button.customId === 'CORRECT') {
await button.deferUpdate();
Collector.stop();
buttons.forEach((element) => {
element.setDisabled();
if(element.customId === 'CORRECT') {
element.setStyle('SUCCESS');
} else {
element.setStyle('DANGER');
}
});
rows.length = 0;
for (let i = 0; i < 5; i++) {
rows.push(new Discord.MessageActionRow());
}
rows.forEach((row, i) => {
row.addComponents(buttons.slice(0 + i * 5, 5 + i * 5));
});
const __embed = new Discord.MessageEmbed()
.setTitle(options.embed.title)
.setDescription(
options.winMessage
.replace('{{winner}}', button.user.id)
.replace('{{time}}', (Date.now() - gameCreatedAt) / 1000),
)
.setImage(data.url)
.setColor(options.embed.color)
.setFooter(options.embed.footer);
if (options.embed.timestamp) {
__embed.setTimestamp();
}
await msg.edit({
embeds: [__embed],
components: rows,
});
} else {
await button.deferUpdate();
}
return delete currentGames[options.message.guild.id];
});
Collector.on('end', async (_msg, reason) => {
if (reason === 'time') {
buttons.forEach((element) => {
element.setDisabled();
if(element.customId === 'CORRECT') {
element.setStyle('SUCCESS');
} else {
element.setStyle('SECONDARY');
}
});
rows.length = 0;
for (let i = 0; i < 5; i++) {
rows.push(new Discord.MessageActionRow());
}
rows.forEach((row, i) => {
row.addComponents(buttons.slice(0 + i * 5, 5 + i * 5));
});
const __embed = new Discord.MessageEmbed()
.setTitle(options.embed.title)
.setColor(options.embed.color)
.setFooter(options.embed.footer)
.setImage(data.url)
.setDescription(options.loseMessage);
if (options.embed.timestamp) {
__embed.setTimestamp();
}
await msg.edit({
embeds: [__embed],
components: rows,
});
return delete currentGames[options.message.guild.id];
}
});
}, Math.floor(Math.random() * 5000) + 1000);
};