From 63ceb3de84eb50d89b34f541c99e0a32661b487d Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Fri, 26 Aug 2022 14:11:48 -0700 Subject: [PATCH] reset cmd/improve money cmd/fix settings cmd options --- commands/inventory.js | 2 +- commands/money.js | 125 +++++++++++++--------------------------- commands/reset.js | 125 ++++++++++++++++++++++++++++++++++++++++ commands/settings.js | 20 +++---- structures/inventory.js | 54 ++++++++--------- 5 files changed, 200 insertions(+), 126 deletions(-) create mode 100644 commands/reset.js diff --git a/commands/inventory.js b/commands/inventory.js index 43c6045..f70cd72 100644 --- a/commands/inventory.js +++ b/commands/inventory.js @@ -540,7 +540,7 @@ module.exports = { interaction.send({ embeds: [prompt], components: [opt], flags: (1 << 6) }); - client.on("interactionCreate", ButtonInteraction => { + client.once("interactionCreate", ButtonInteraction => { if(!ButtonInteraction.isButton()) return; const queryString = ButtonInteraction.customId; if (!queryString.endsWith(ButtonInteraction.member.user.id)) { diff --git a/commands/money.js b/commands/money.js index d9527c3..d8e6f4e 100644 --- a/commands/money.js +++ b/commands/money.js @@ -12,51 +12,30 @@ module.exports = { }, options: [ { - name: "add", - description: "Add money to user", - value: "add", - type: 1, - options: [ - { - name: "user", - description: "User to give money", - value: "user", - type: 6, - required: true, - }, - { - name: "amount", - description: "The amount to add to balance", - value: "amount", - type: 5, - min_value: 0.01, - required: true, - } - ], + name: "add_or_remove", + description: "Add or remove money from a user", + value: "add_or_remove", + type: 3, + required: true, + choices: [ + { name: "add", value: "add" }, { name: "remove", value: "remove" }, + ] }, { - name: "remove", - description: "Remove money from user", - value: "remove", - type: 1, - options: [ - { - name: "user", - description: "User to remove money from", - value: "user", - type: 6, - required: true, - }, - { - name: "amount", - description: "The amount to add to balance", - value: "amount", - type: 5, - min_value: 0.01, - required: true, - } - ], - } + name: "amount", + description: "The amount to add to balance", + value: "amount", + type: 5, + min_value: 0.01, + required: true, + }, + { + name: "from", + description: "User to alter balance", + value: "from", + type: 6, + required: true, + }, ], SlashCommand: { /** @@ -78,7 +57,7 @@ module.exports = { if (client.exists(GuildDB.botAdmin) && interaction.member.roles.includes(GuildDB.botAdmin)) canUseCommand = true; if (!canUseCommand) return interaction.send('You don\'t have the permissions to use this command.'); - let targetUserID = args[0].options[0].value.replace('<@!', '').replace('>', ''); + const targetUserID = args[2].value.replace('<@!', '').replace('>', ''); let banking = await client.dbo.collection("banks").findOne({"account.userID": targetUserID}).then(banking => banking); if (!banking) { @@ -122,51 +101,27 @@ module.exports = { return interaction.send({ embeds: [embed] }); } } - - if (args[0].name == "add") { - // add money to user - - let newBalance = banking.guilds[GuildDB.serverID].account.balance + args[0].options[1].value; + + const newBalance = args[0] == 'add' + ? banking.guilds[GuildDB.serverID].account.balance + args[1].value + : banking.guilds[GuildDB.serverID].account.balance - args[1].value; - client.dbo.collection("banks").updateOne({"account.userID":targetUserID},{$set:{[`banking.guilds.${GuildDB.serverID}.account.balance`]:newBalance}}, function(err, res) { - if (err) { - client.log(err); - const embed = new EmbedBuilder() - .setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`) - .setColor(client.config.Colors.Red) - - return interaction.send({ embeds: [embed] }); - } - }); - + client.dbo.collection("banks").updateOne({"account.userID":targetUserID},{$set:{[`banking.guilds.${GuildDB.serverID}.account.balance`]:newBalance}}, function(err, res) { + if (err) { + client.log(err); + const embed = new EmbedBuilder() + .setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`) + .setColor(client.config.Colors.Red) - let successEmbed = new EmbedBuilder() - .setDescription(`Successfully added $${args[0].options[1].value.toFixed(2)} to <@${targetUserID}>'s balance`) - .setColor('GREEN'); - - return interaction.send({ embeds: [successEmbed] }); - } else if (args[0].name == "remove") { - // remove money from user - - let newBalance = banking.guilds[GuildDB.serverID].account.balance - args[0].options[1].value; + return interaction.send({ embeds: [embed] }); + } + }); - client.dbo.collection("banks").updateOne({"account.userID":targetUserID},{$set:{[`banking.guilds.${GuildDB.serverID}.account.balance`]:newBalance}}, function(err, res) { - if (err) { - client.log(err); - const embed = new EmbedBuilder() - .setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`) - .setColor(client.config.Colors.Red) - - return interaction.send({ embeds: [embed] }); - } - }); - - let successEmbed = new EmbedBuilder() - .setDescription(`Successfully removed $${args[0].options[1].value.toFixed(2)} from <@${targetUserID}>'s balance`) - .setColor('GREEN'); + const successEmbed = new EmbedBuilder() + .setDescription(`Successfully ${args[0] == 'add' ? 'added' : 'removed'} $${args[0].options[1].value.toFixed(2)} ${args[0] == 'add' ? 'to' : 'from'} <@${targetUserID}>'s balance`) + .setColor('GREEN'); - return interaction.send({ embeds: [successEmbed] }); - } + return interaction.send({ embeds: [successEmbed] }); }, }, } \ No newline at end of file diff --git a/commands/reset.js b/commands/reset.js new file mode 100644 index 0000000..6f21cf6 --- /dev/null +++ b/commands/reset.js @@ -0,0 +1,125 @@ +const { EmbedBuilder } = require('discord.js'); +const { addInventory } = require('../structures/inventory'); +const { addBank } = require('../structures/bank'); + +module.exports = { + name: "", + debug: false, + description: "", + usage: "", + permissions: { + channel: ["VIEW_CHANNEL", "SEND_MESSAGES", "EMBED_LINKS"], + member: [], + }, + options: [{ + name: "user", + description: "User to reset", + value: "user", + type: 6, + required: true, + }], + SlashCommand: { + /** + * + * @param {require("../structures/QuarksBot")} client + * @param {import("discord.js").MessageCreate} message + * @param {string[]} args + * @param {*} param3 + */ + run: async (client, interaction, args, { GuildDB }) => { + if (GuildDB.customChannelStatus==true&&!GuildDB.allowedChannels.includes(interaction.channel_id)) { + return interaction.send(`You are not allowed to use the bot in this channel.`); + } + + const permissions = bitfieldCalculator.permissions(interaction.member.permissions); + let canUseCommand = false; + + if (permissions.includes("MANAGE_GUILD")) canUseCommand = true; + if (client.exists(GuildDB.botAdmin) && interaction.member.roles.includes(GuildDB.botAdmin)) canUseCommand = true; + if (!canUseCommand) return interaction.send('You don\'t have the permissions to use this command.'); + + const targetUserID = args[0].value.replace('<@!', '').replace('>', ''); + + const prompt = new EmbedBuilder() + .setTitle(`Are you sure you want to reset this user?`) + .setDescription('**Notice:** This will reset this users, cash, balance, and invnetory back to defaults.') + .setColor(client.config.Colors.Yellow) + + const opt = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId(`yes-${interaction.member.user.id}`) + .setLabel("Yes") + .setStyle(ButtonStyle.Danger), + new ButtonBuilder() + .setCustomId(`no-${interaction.member.user.id}`) + .setLabel("No") + .setStyle(ButtonStyle.Success) + ) + + interaction.send({ embeds: [prompt], components: [opt], flags: (1 << 6) }); + + client.once("interactionCreate", ButtonInteraction => { + if(!ButtonInteraction.isButton()) return; + const queryString = ButtonInteraction.customId; + if (!queryString.endsWith(ButtonInteraction.member.user.id)) { + return ButtonInteraction.reply({ + content: "This button is not for you", + flags: (1 << 6) + }) + } + if (queryString==`yes-${interaction.member.user.id}`) { + const successEmbed = new EmbedBuilder() + .setColor(client.config.Colors.Green) + .setTitle('Successfully reset user\'s data') + + let inventory = await client.dbo.collection("inventories").findOne({"inventory.userID": interaction.member.user.id}).then(inventory => inventory); + let banking = await client.dbo.collection("banks").findOne({"account.userID": interaction.member.user.id}).then(banking => banking); + + let inventoryReset = false; + let bankingReset = false; + if (!inventory) inventoryReset = true + else inventory = inventory.inventory + if (!banking) bankingReset = true + else banking = banking.banking + + if (inventoryReset && bankingReset) return ButtonInteraction.update({ embeds: [successEmbed], components: [] }); + + if (!inventoryReset) { + const success = addInventory(inventory.guilds, GuildDB.serverID, targetUserID, client); + if (!success) { + client.log(err); + const embed = new EmbedBuilder() + .setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`) + .setColor(client.config.Colors.Red) + + return ButtonInteraction.update({ embeds: [embed], components: [] }); + } + } + + if (!bankingReset) { + const success = addBank(banking.guilds, GuildDB.serverID, targetUserID, client); + if (!success) { + client.log(err); + const embed = new EmbedBuilder() + .setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`) + .setColor(client.config.Colors.Red) + + return ButtonInteraction.update({ embeds: [embed], components: [] }); + } + } + + return ButtonInteraction.update({ embeds: [successEmbed], components: [] }); + + } else if (queryString==`no-${interaction.member.user.id}`) { + const successEmbed = new EmbedBuilder() + .setColor(client.config.Colors.Green) + .setTitle(`The User was not reset`); + + return ButtonInteraction.update({ embeds: [successEmbed], components: [] }); + } + }); + return; + }, + }, +} \ No newline at end of file diff --git a/commands/settings.js b/commands/settings.js index 086607a..f12452b 100644 --- a/commands/settings.js +++ b/commands/settings.js @@ -413,7 +413,7 @@ module.exports = { interaction.send({ embeds: [prompt], components: [opt], flags: (1 << 6) }); - client.on("interactionCreate", ButtonInteraction => { + client.once("interactionCreate", ButtonInteraction => { if(!ButtonInteraction.isButton()) return; const queryString = ButtonInteraction.customId; if (!queryString.endsWith(ButtonInteraction.member.user.id)) { @@ -491,7 +491,7 @@ module.exports = { interaction.send({ embeds: [prompt], components: [opt], flags: (1 << 6) }); - client.on("interactionCreate", ButtonInteraction => { + client.once("interactionCreate", ButtonInteraction => { if(!ButtonInteraction.isButton()) return; const queryString = ButtonInteraction.customId; if (!queryString.endsWith(ButtonInteraction.member.user.id)) { @@ -569,7 +569,7 @@ module.exports = { interaction.send({ embeds: [prompt], components: [opt], flags: (1 << 6) }); - client.on("interactionCreate", ButtonInteraction => { + client.once("interactionCreate", ButtonInteraction => { if(!ButtonInteraction.isButton()) return; const queryString = ButtonInteraction.customId; if (!queryString.endsWith(ButtonInteraction.member.user.id)) { @@ -647,7 +647,7 @@ module.exports = { interaction.send({ embeds: [prompt], components: [opt], flags: (1 << 6) }); - client.on("interactionCreate", ButtonInteraction => { + client.once("interactionCreate", ButtonInteraction => { if(!ButtonInteraction.isButton()) return; const queryString = ButtonInteraction.customId; if (!queryString.endsWith(ButtonInteraction.member.user.id)) { @@ -726,7 +726,7 @@ module.exports = { interaction.send({ embeds: [prompt], components: [opt], flags: (1 << 6) }); - client.on("interactionCreate", ButtonInteraction => { + client.once("interactionCreate", ButtonInteraction => { if(!ButtonInteraction.isButton()) return; const queryString = ButtonInteraction.customId; if (!queryString.endsWith(ButtonInteraction.member.user.id)) { @@ -854,7 +854,7 @@ module.exports = { interaction.send({ embeds: [prompt], components: [opt], flags: (1 << 6) }); - client.on("interactionCreate", ButtonInteraction => { + client.once("interactionCreate", ButtonInteraction => { if(!ButtonInteraction.isButton()) return; const queryString = ButtonInteraction.customId; if (!queryString.endsWith(ButtonInteraction.member.user.id)) { @@ -928,7 +928,7 @@ module.exports = { interaction.send({ embeds: [prompt], components: [opt], flags: (1 << 6) }); - client.on("interactionCreate", ButtonInteraction => { + client.once("interactionCreate", ButtonInteraction => { if(!ButtonInteraction.isButton()) return; const queryString = ButtonInteraction.customId; if (!queryString.endsWith(ButtonInteraction.member.user.id)) { @@ -1002,7 +1002,7 @@ module.exports = { interaction.send({ embeds: [prompt], components: [opt], flags: (1 << 6) }); - client.on("interactionCreate", ButtonInteraction => { + client.once("interactionCreate", ButtonInteraction => { if(!ButtonInteraction.isButton()) return; const queryString = ButtonInteraction.customId; if (!queryString.endsWith(ButtonInteraction.member.user.id)) { @@ -1076,7 +1076,7 @@ module.exports = { interaction.send({ embeds: [prompt], components: [opt], flags: (1 << 6) }); - client.on("interactionCreate", ButtonInteraction => { + client.once("interactionCreate", ButtonInteraction => { if(!ButtonInteraction.isButton()) return; const queryString = ButtonInteraction.customId; if (!queryString.endsWith(ButtonInteraction.member.user.id)) { @@ -1150,7 +1150,7 @@ module.exports = { interaction.send({ embeds: [prompt], components: [opt], flags: (1 << 6) }); - client.on("interactionCreate", ButtonInteraction => { + client.once("interactionCreate", ButtonInteraction => { if(!ButtonInteraction.isButton()) return; const queryString = ButtonInteraction.customId; if (!queryString.endsWith(ButtonInteraction.member.user.id)) { diff --git a/structures/inventory.js b/structures/inventory.js index c9ec412..fd0e741 100644 --- a/structures/inventory.js +++ b/structures/inventory.js @@ -1,5 +1,21 @@ const mongoose = require('mongoose'); +const defaultGuildData = { + items: [{ + name: 'Wallet', + type: 'wallet', + description: '`/wallet view` to view wallet', + cash: 0.00, + ID: { + firstname: '', + lastname: '', + dob: '', + addr: '', + } + }], + lastWork: new Date('2000-01-01T00:00:00') // garantee's they can work right after inventory create, +} + let inventorySchema = mongoose.Schema({ inventory: { userID: String, @@ -9,45 +25,23 @@ let inventorySchema = mongoose.Schema({ inventorySchema.methods.createInventory = function (userID, guildID) { this.inventory.userID = userID; - const newGuild = { - items: [{ - name: 'Wallet', - type: 'wallet', - description: '`/wallet view` to view wallet', - cash: 0.00, - ID: { - firstname: '', - lastname: '', - dob: '' - } - }], - lastWork: new Date('2000-01-01T00:00:00') // garantee's they can work right after inventory create, - }; this.inventory.guilds = {}; - this.inventory.guilds[guildID] = newGuild + this.inventory.guilds[guildID] = defaultGuildData; }; inventorySchema.methods.addItem = function (guildID, item) { this.inventory.guilds[guildID].items.push(item); }; -// This function is to add a new guild specific inventory to an already existing -// inventory document +/* + This function is to add a new guild specific inventory to an already existing + inventory document + or + can be used to reset a data back to default +*/ function addInventory(guilds, guildID, userID, client) { let updatedGuilds = guilds; - updatedGuilds[guildID] = { - items: [{ - name: 'Wallet', - type: 'wallet', - description: '`/wallet view` to view wallet', - cash: 0.00, - ID: { - firstname: '', - lastname: '', - dob: '' - } - }], - } + updatedGuilds[guildID] = defaultGuildData; client.dbo.collection("inventories").updateOne({"inventory.userID":userID}, {$set: {"inventory.guilds": updatedGuilds}}, function(err, res) { if (err) return false