From 4a19bff4f0af123585a0d7029f17c3a89f2e36c5 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Tue, 4 Jan 2022 16:34:26 -0700 Subject: [PATCH] chest claiming --- bot.js | 7 ++++++- commands/leveling/chests.js | 4 ++-- commands/leveling/claim.js | 29 +++++++++++++++++++++++++++++ util/cache.js | 2 +- util/cache/chest.js | 4 ++-- util/cache/lxp.js | 2 +- util/lxp/spawnchest.js | 14 ++++++++++---- 7 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 commands/leveling/claim.js diff --git a/bot.js b/bot.js index 2edc42d..8bba88a 100644 --- a/bot.js +++ b/bot.js @@ -36,6 +36,11 @@ client.misc = { }, chests: [], chestsTimeout: new Map(), + chests: { + enabled: [], + timeout: new Map(), + waiting: new Map() + }, monit: {}, monitEnabled: [], inVC: [], @@ -128,7 +133,7 @@ async function init() { client.utils.logch = async () => {return client.guilds.cache.get('762707532417335296').channels.cache.get('762732961753595915');}; client.utils.s = num => num === 1 ? '' : 's'; client.utils.as = (num, text) => `${text}${client.utils.s(num)}`; - client.utils.an = (text, caps) => `${caps ? 'A' : 'a'}${['a', 'e', 'i', 'o'].includes(text.toLowerCase().trim().slice(0, 1)) ? 'n' : ''} ${text}`; + client.utils.an = (text, caps) => `${caps ? 'A' : 'a'}${['a', 'e', 'i', 'o', 'u'].includes(text.toLowerCase().trim().slice(0, 1)) ? 'n' : ''} ${text}`; client.guildconfig = {}; client.guildconfig.prefixes = new Map(); diff --git a/commands/leveling/chests.js b/commands/leveling/chests.js index 2626b49..b290ac6 100644 --- a/commands/leveling/chests.js +++ b/commands/leveling/chests.js @@ -24,7 +24,7 @@ module.exports = { if (!args.length) {args[0] = 'enable';} if (['e', 'enable'].includes(args[0].toLowerCase())) { - if (client.misc.cache.chests.includes(message.guild.id)) {return message.channel.send("This server already has chest spawning enabled.")}; + if (client.misc.cache.chests.enabled.includes(message.guild.id)) {return message.channel.send("This server already has chest spawning enabled.")}; try { am = await message.channel.send("Would you like to have me send chests to a specific channel?"); await am.react('👍'); @@ -43,7 +43,7 @@ module.exports = { } let c = new Chests({gid: message.guild.id, channel: chestCh}); c.save(); - client.misc.cache.chests.push(message.guild.id); + client.misc.cache.chests.enabled.push(message.guild.id); return message.channel.send({embeds: [new Discord.MessageEmbed() .setTitle("Chest Spawning Enabled!") .setThumbnail(message.guild.iconURL({size: 2048})) diff --git a/commands/leveling/claim.js b/commands/leveling/claim.js new file mode 100644 index 0000000..18b6a91 --- /dev/null +++ b/commands/leveling/claim.js @@ -0,0 +1,29 @@ +const Discord = require('discord.js'); + +module.exports = { + name: "claim", + aliases: [], + meta: { + category: 'Leveling', + description: "Claim a chest that has spawned in the channel", + syntax: '`claim [specialText]`', + extra: null, + guildOnly: true + }, + help: "Claim a chest that has spawned in the channel. You must be in the same channel as the chest in order to claim it.", + async execute(message, msg, args, cmd, prefix, mention, client) { + if (!client.misc.cache.chests.enabled.includes(message.guild.id)) {return message.channel.send("Chests aren't enabled in this server!");} + if (!client.misc.cache.chests.waiting.has(message.channel.id)) {return message.channel.send("There are no chests to claim in this channel.");} + if (!client.misc.cache.monners[message.author.id]) {return message.channel.send("There was an issue on my side with claiming your chest. This happened because I don't have your Monners info cached, so send a message anywhere and then try again. Sorry!");} + + let chest = client.misc.cache.chests.waiting.get(message.channel.id); + client.misc.cache.monners[message.author.id] += chest.amount; + client.misc.cache.chests.waiting.delete(message.channel.id); + chest.message.delete().catch(() => {}); + return message.channel.send({embeds: [new Discord.MessageEmbed() + .setAuthor({name: message.member.displayName, iconURL: message.member.displayAvatarURL()}) + .setDescription(`You've claimed ${client.utils.an(chest.rarity.name, true)} Chest with **${chest.amount} Monners<:monners:926736756047495218>**`) + .setColor(chest.rarity.color) + ]}).catch(() => {}); + } +}; \ No newline at end of file diff --git a/util/cache.js b/util/cache.js index 979ae29..34e2f3c 100644 --- a/util/cache.js +++ b/util/cache.js @@ -42,5 +42,5 @@ module.exports = async (client) => { let ora_ctCache = ora("Caching Chests...").start(); await require('./cache/chest')(client); ora_ctCache.stop(); ora_ctCache.clear(); - console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.chests.length}`)} ${chalk.blueBright("guilds that spawn chests.")}`); + console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.chests.enabled.length}`)} ${chalk.blueBright("guilds that spawn chests.")}`); }; \ No newline at end of file diff --git a/util/cache/chest.js b/util/cache/chest.js index 8e9ec65..4ab945c 100644 --- a/util/cache/chest.js +++ b/util/cache/chest.js @@ -1,9 +1,9 @@ const Chests = require('../../models/chests'); module.exports = async client => { - client.misc.cache.chests = []; + client.misc.cache.chests.enabled = []; for await (const chest of Chests.find()) { - client.misc.cache.chests.push(chest.gid); + client.misc.cache.chests.enabled.push(chest.gid); } }; \ No newline at end of file diff --git a/util/cache/lxp.js b/util/cache/lxp.js index 754d7c2..3dc45c8 100644 --- a/util/cache/lxp.js +++ b/util/cache/lxp.js @@ -3,7 +3,7 @@ const LXP = require('../../models/localxp'); module.exports = async client => { client.misc.cache.lxp.enabled = []; client.misc.cache.lxp.disabledChannels = new Map(); - client.misc.cache.chests = new Map(); + client.misc.cache.chests.enabled = new Map(); for await (const xp of LXP.find()) { client.misc.cache.lxp.enabled.push(xp.gid); diff --git a/util/lxp/spawnchest.js b/util/lxp/spawnchest.js index 58680f4..1324066 100644 --- a/util/lxp/spawnchest.js +++ b/util/lxp/spawnchest.js @@ -4,7 +4,7 @@ const manyitems = require('manyitems'); const Discord = require('discord.js'); module.exports = async (client, member, channel, prefix) => { - if (client.misc.cache.chestsTimeout.has(member.guild.id) && new Date().getTime() - client.misc.cache.chestsTimeout.get(member.guild.id) < (1000 * 60 * 2)) {return;} + if (client.misc.cache.chests.timeout.has(member.guild.id) && new Date().getTime() - client.misc.cache.chests.timeout.get(member.guild.id) < (1000 * 60 * 2)) {return;} //let rand = Math.floor(Math.random() * 100); //if (rand !== 69) {return;} //decide if it even continues @@ -62,7 +62,7 @@ module.exports = async (client, member, channel, prefix) => { if (!chests) {return;} let spawnChannel = chests.channel && chests.channel.length ? chests.channel : channel; - client.misc.cache.chestsTimeout.set(member.guild.id, new Date().getTime()); + client.misc.cache.chests.timeout.set(member.guild.id, new Date().getTime()); let chestEmbed = new Discord.MessageEmbed() .setTitle(`${client.utils.an(rarity.name, true)} Chest has spawned!`) @@ -70,10 +70,16 @@ module.exports = async (client, member, channel, prefix) => { .setFooter({text: `Type \`${prefix}claim\` to claim it!`}) .setColor(rarity.color) //create the chest message - if (spawnChannel === channel) {return channel.send({embeds: [chestEmbed]}).catch(() => {});} + if (spawnChannel === channel) { + return channel.send({embeds: [chestEmbed]}) + .then(m => {client.misc.cache.chests.waiting.set(m.channel.id, {amount: amount, rarity: rarity, message: m});}) + .catch(() => {}); + } else { member.guild.channels.fetch(spawnChannel) - .then(ch => ch.send({embeds: [chestEmbed]}).catch(() => {})) + .then(ch => ch.send({embeds: [chestEmbed]}) + .then(m => {client.misc.cache.chests.waiting.set(m.channel.id, {amount: amount, rarity: rarity, message: m});}) + .catch(() => {})) .catch(() => {}) } return spawnChannel.send({embeds: [chestEmbed]}); //spawn the chest