diff --git a/commands/311.js b/commands/311.js index f4eb532..db2968f 100644 --- a/commands/311.js +++ b/commands/311.js @@ -93,7 +93,7 @@ module.exports = { const opt = new ActionRowBuilder() .addComponents( new ButtonBuilder() - .setCustomId(`Delete311`) + .setCustomId(`Delete311`) .setLabel("Delete") .setStyle(ButtonStyle.Danger), ) @@ -118,17 +118,17 @@ module.exports = { Delete311: { run: async (client, interaction, GuildDB) => { - if (!interaction.member._roles.includes(GuildDB.dispatcherRole)) { - return interaction.reply({ - content: "This button is not for you", - flags: (1 << 6) - }) - } - const successEmbed = new EmbedBuilder() - .setColor(client.config.Colors.Green) - .setTitle(`Successfully Deleted 311 Report.`) - - return interaction.update({ embeds: [successEmbed], components: [] }); + if (!interaction.member._roles.includes(GuildDB.dispatcherRole)) { + return interaction.reply({ + content: "This button is not for you", + flags: (1 << 6) + }) + } + const successEmbed = new EmbedBuilder() + .setColor(client.config.Colors.Green) + .setTitle(`Successfully Deleted 311 Report.`) + + return interaction.update({ embeds: [successEmbed], components: [] }); } } } diff --git a/commands/config.js b/commands/config.js index ba71bb6..895faeb 100644 --- a/commands/config.js +++ b/commands/config.js @@ -352,6 +352,19 @@ module.exports = { min_value: 1, }] }, + { + name: "can_ignore_invoices", + description: "Allow users to ignore and delete invoices", + value: "can_ignore_invoices", + type: 1, + options: [{ + name: "true_or_false", + description: "Set the value for this configurations", + value: true, + type: 5, + required: true, + }] + }, { name: "reset", description: "Restore all settings to default configurations", @@ -855,6 +868,17 @@ module.exports = { return interaction.send({ embeds: [prompt], components: [opt], flags: (1 << 6) }); + } else if (args[0].name == 'can_ignore_invoices') { + client.dbo.collection("guilds").updateOne({"server.serverID":GuildDB.serverID}, {$set: {"server.canIgnoreInvoices": args[0].options[0].value}}, function(err, res) { + if (err) return client.sendInternalError(interaction, err); + }); + + const successEmbed = new EmbedBuilder() + .setColor(client.config.Colors.Green) + .setDescription(`**Success:** can ${args[0].options[0].value == true ? 'now' : 'no longer'} ignore and delete invoices.`); + + return interaction.send({ embeds: [successEmbed] }); + } else if (args[0].name == 'view') { const tweetChannel = GuildDB.tweetChannel ? `\n╚➤ <#${GuildDB.tweetChannel}>` : ''; const tweetColor = GuildDB.tweetChannel ? '+ ' : '- '; @@ -896,6 +920,7 @@ module.exports = { { name: 'Has Disptach Role?', value: `\`\`\`diff\n${dispatchRoleColor}${client.exists(GuildDB.dispatcherRole)}\`\`\`${dispatchRole}`, inline: true }, { name: 'Has Dispatch Channel?', value: `\`\`\`diff\n${dispatchChannelColor}${client.exists(GuildDB.dispatchChannel)}\`\`\`${dispatchChannel}`, inline: true }, { name: 'Has bot admin role?', value: `\`\`\`diff\n${botAdminRoleColor}${client.exists(GuildDB.botAdmin)}\`\`\`${botAdminRole}`, inline: true }, + { name: 'Can Ignore Invoices?', value: `\`\`\`diff\n${GuildDB.canIgnoreInvoices ? '+ ' : '- '}${GuildDB.canIgnoreInvoices}\`\`\``, inline: true }, ); return interaction.send({ embeds: [settingsEmbed] }); diff --git a/commands/invoice.js b/commands/invoice.js new file mode 100644 index 0000000..3f35d99 --- /dev/null +++ b/commands/invoice.js @@ -0,0 +1,329 @@ +const { EmbedBuilder, SelectMenuBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js'); +const { Invoice, addInvoiceList, addInvoice } = require('../structures/invoice'); + +module.exports = { + name: "invoice", + debug: false, + description: "handle invoices", + usage: "[cmd] [opt]", + permissions: { + channel: ["VIEW_CHANNEL", "SEND_MESSAGES", "EMBED_LINKS"], + member: [], + }, + options: [ + { + name: "list", + description: "list invoices that have been sent to you", + value: "list", + type: 1, + }, + { + name: "create", + description: "send an invoice to a user", + value: "create", + type: 1, + options: [ + { + name: "title", + description: "title of the invoice", + value: "title", + type: 3, + required: true, + }, + { + name: "description", + description: "description of the invoice", + value: "description", + type: 3, + required: true, + }, + { + name: "user", + description: "user to send invoice to", + value: "user", + type: 6, + required: true, + }, + { + name: "fee", + description: "the amount due for this invoice", + value: "fee", + type: 10, + min_value: 0.01, + 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 && !GuildDB.allowedChannels.includes(interaction.channel_id)) { + return interaction.send(`You are not allowed to use the bot in this channel.`); + } + + if (args[0].name == 'list') { + let invoices = await client.dbo.collection("invoices").findOne({"invoiceDB.userID": interaction.member.user.id}).then(invoices => invoices); + + if (!invoices) { + invoices = { + userID: interaction.member.user.id, + guilds: {} + }; + invoices.guilds[GuildDB.serverID] = { invoices: [] }; + + // Register invoiceDB for user + let newInvoice = new Invoice(); + newInvoice.createInvoiceDB(interaction.member.user.id, GuildDB.serverID); + await newInvoice.save().catch(err => { + if (err) return client.sendInternalError(interaction, err); + }); + } else invoices = invoices.invoiceDB; + + if (!client.exists(invoices.guilds[GuildDB.serverID])) { + let success = addInvoiceList(invoices.guilds, GuildDB.serverID, interaction.member.user.id, client); + if (!success) return client.sendInternalError(interaction, 'Failed to add invoice list'); + } + + if (invoices.guilds[GuildDB.serverID].invoices.length == 0) { + let embed = new EmbedBuilder() + .setTitle('No invoices') + .setDescription('You have no invoices to view') + .setColor(client.config.Colors.Default); + + return interaction.send({ embeds: [embed] }); + } + + let invoiceMenu = new SelectMenuBuilder() + .setCustomId(`ShowInvoice`) + .setPlaceholder('Select the invoice to view') + + invoices.guilds[GuildDB.serverID].invoices.map((invoice) => { + invoiceMenu.addOptions({ + label: `${invoice.id} - ${invoice.name}`, + description: invoice.description, + value: invoice.id, + }) + }) + + const invoiceSelect = new ActionRowBuilder().addComponents(invoiceMenu); + + interaction.send({ components: [invoiceSelect], flags: (1 << 6) }); + + } else if (args[0].name == 'create') { + if (!client.exists(GuildDB.commercialRole)) { + let embed = new EmbedBuilder() + .setColor(client.config.Colors.Yellow) + .setTitle("Notice:") + .setDescription("An admin needs to configure the commercial role in order for this command to be used."); + + return interaction.send({ embeds: [embed] }); + } + + if (!interaction.member.roles.includes(GuildDB.commercialRole)) { + let embed = new EmbedBuilder() + .setColor(client.config.Colors.Yellow) + .setDescription(`**Notice:** You require the <@&${GuildDB.commercialRole}> role`) + + return interaction.send({ embeds: [embed] }); + } + + if (args[0].options[2] == interaction.member.user.id) { + let embed = new EmbedBuilder() + .setColor(client.config.Colors.Yellow) + .setDescription('**Notice:** You can\'t create an invoice on yourself'); + + return interaction.send({ embeds: [embed] }); + } + + let invoices = await client.dbo.collection("invoices").findOne({"invoiceDB.userID": args[0].options[2].value}).then(invoices => invoices); + + if (!invoices) { + invoices = { + userID: args[0].options[2].value, + guilds: {} + }; + invoices.guilds[GuildDB.serverID] = { invoices: [] }; + + // Register invoiceDB for user + let newInvoice = new Invoice(); + newInvoice.createInvoiceDB(args[0].options[2].value, GuildDB.serverID); + await newInvoice.save().catch(err => { + if (err) return client.sendInternalError(interaction, err); + }); + } else invoices = invoices.invoiceDB; + + if (!client.exists(invoices.guilds[GuildDB.serverID])) { + let success = addInvoiceList(invoices.guilds, GuildDB.serverID, args[0].options[2].value, client); + if (!success) return client.sendInternalError(interaction, 'Failed to add invoice list'); + } + + let ID = Math.floor(100000 + Math.random() * 900000); + let invoice = { + id: `${ID}`, + name: args[0].options[0].value, + description: args[0].options[1].value, + amount: args[0].options[3].value, + to: args[0].options[2].value, + from: interaction.member.user.id, + paid: false + }; + + let success = addInvoice(GuildDB.serverID, args[0].options[2].value, invoice, client); + if (!success) return client.sendInternalError(interaction, 'Failed to add invoice to user'); + + let successEmbed = new EmbedBuilder() + .setTitle("Success") + .setDescription(`Successfully sent invoice **#${ID}** to <@${args[0].options[2].value}>`) + .setColor(client.config.Colors.Green); + + return interaction.send({ embeds: [successEmbed] }); + } + }, + }, + + Interactions: { + + ShowInvoice: { + run: async (client, interaction, GuildDB) => { + let invoices = await client.dbo.collection("invoices").findOne({"invoiceDB.userID": interaction.member.user.id}).then(invoices => invoices); + let invoice = invoices.invoiceDB.guilds[GuildDB.serverID].invoices.find(invoice => invoice.id == interaction.values[0]); + let invoiceData = `**ID** #${invoice.id}\n**Name** ${invoice.name}\n**Description** ${invoice.description}\n**From** <@${invoice.from}>\n**Amount Due** ${invoice.amount.toFixed(2)}`; + + let banking = await client.dbo.collection("banks").findOne({"banking.userID": interaction.member.user.id}).then(banking => banking); + + if (!banking) { + banking = { + userID: interaction.member.user.id, + guilds: { + [GuildDB.serverID]: { + account: { + balance: GuildDB.startingBalance, + cash: 0.00, + } + } + } + } + + // Register inventory for user + let newBank = new Bank(); + newBank.createBank(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance); + newBank.save().catch(err => { + if (err) return client.sendInternalError(interaction, err); + }); + + } else banking = banking.banking; + + if (!client.exists(banking.guilds[GuildDB.serverID])) { + const success = addBank(banking.guilds, GuildDB.serverID, interaction.member.user.id, client, GuildDB.startingBalance); + if (!success) return client.sendInternalError(interaction, 'Failed to add bank'); + } + + let invoiceEmbed = new EmbedBuilder() + .setColor(client.config.Colors.Default) + .setTitle(`${invoice.id} - ${invoice.name}`) + .setDescription(invoiceData); + + let payButton = new ButtonBuilder() + .setCustomId(`PayInvoice-${invoice.id}`) // pass invoice id to search for invoice in pay handler + .setLabel("Pay") + .setStyle(ButtonStyle.Primary); + + if (banking.guilds[GuildDB.serverID].account.balance < invoice.amount) payButton.setDisabled(true); + + let opt = new ActionRowBuilder() + .addComponents(payButton); + + if (GuildDB.canIgnoreInvoices) { + opt.addComponents( + new ButtonBuilder() + .setCustomId(`DeleteInvoice-${invoice.id}`) + .setLabel("Ignore and Delete") + .setStyle(ButtonStyle.Secondary) + ); + } + + return interaction.update({ embeds: [invoiceEmbed], components: [opt] }); + } + }, + + PayInvoice: { + run: async (client, interaction, GuildDB) => { + let invoices = await client.dbo.collection("invoices").findOne({"invoiceDB.userID": interaction.member.user.id}).then(invoices => invoices); + let invoice = invoices.invoiceDB.guilds[GuildDB.serverID].invoices.find(invoice => invoice.id == interaction.customId.split('-')[1]); + let banking = await client.dbo.collection("banks").findOne({"banking.userID": invoice.to}).then(banking => banking); + banking = banking.banking; // we don't have to check for bank exisiting since its handled in the above function. + + let newBalance = banking.guilds[GuildDB.serverID].account.balance - invoice.amount; + + client.dbo.collection("banks").updateOne({"banking.userID": invoice.to}, {$set: {[`banking.guilds.${GuildDB.serverID}.account.balance`]: newBalance}}, function (err, res) { + if (err) return client.sendInternalError(interaction, err); + }); + + banking = await client.dbo.collection("banks").findOne({"banking.userID": invoice.from}).then(banking => banking); + + // check if user who sent invoice has bank records + if (!banking) { + banking = { + userID: invoice.from, + guilds: { + [GuildDB.serverID]: { + account: { + balance: GuildDB.startingBalance, + cash: 0.00, + } + } + } + } + + // Register inventory for user + let newBank = new Bank(); + newBank.createBank(invoice.from, GuildDB.serverID, GuildDB.startingBalance); + newBank.save().catch(err => { + if (err) return client.sendInternalError(interaction, err); + }); + + } else banking = banking.banking; + + newBalance = banking.guilds[GuildDB.serverID].account.balance + invoice.amount; + + client.dbo.collection("banks").updateOne({"banking.userID": invoice.from}, {$set: {[`banking.guilds.${GuildDB.serverID}.account.balance`]: newBalance}}, function (err, res) { + if (err) return client.sendInternalError(interaction, err); + }); + + client.dbo.collection("invoices").updateOne({"invoiceDB.userID": invoice.to}, {$pull: {[`invoiceDB.guilds.${GuildDB.serverID}.invoices`]: invoice}}, function (err, res) { + if (err) return client.sendInternalError(interaction, err); + }); + + let successEmbed = new EmbedBuilder() + .setColor(client.config.Colors.Green) + .setTitle(`Successfully payed invoice #${invoice.id}`); + + return interaction.update({ embeds: [successEmbed], components: [] }); + } + }, + + DeleteInvoice: { + run: async (client, interaction, GuildDB) => { + let invoices = await client.dbo.collection("invoices").findOne({"invoiceDB.userID": interaction.member.user.id}).then(invoices => invoices); + let invoice = invoices.invoiceDB.guilds[GuildDB.serverID].invoices.find(invoice => invoice.id == interaction.customId.split('-')[1]); + + client.dbo.collection("invoices").updateOne({"invoiceDB.userID": invoice.to}, {$pull: {[`invoiceDB.guilds.${GuildDB.serverID}.invoices`]: invoice}}, function (err, res) { + if (err) return client.sendInternalError(interaction, err); + }); + + let successEmbed = new EmbedBuilder() + .setColor(client.config.Colors.Green) + .setTitle(`Ignored and deleted invoice #${invoice.id}`); + + return interaction.update({ embeds: [successEmbed], components: [] }); + } + } + } +} \ No newline at end of file diff --git a/config/config.js b/config/config.js index 9574db2..f39d25f 100644 --- a/config/config.js +++ b/config/config.js @@ -2,7 +2,7 @@ require('dotenv').config(); module.exports = { Dev: "PROD.", - Version: "12.8.4", // (major).(feature).(revision/bug/refactoring) + Version: "13.0.0", // (major).(feature).(revision/bug/refactoring) Admins: ["362791661274660874", "329371697570381824"], // Admins of the bot DefaultPrefix: "?", ServerID: "955668596745461830", diff --git a/config/settingsHelp.js b/config/settingsHelp.js index 0629e47..d34fb29 100644 --- a/config/settingsHelp.js +++ b/config/settingsHelp.js @@ -100,6 +100,12 @@ module.exports = { The \`/search\` command allows users to view other users inventories. If you wish to limit this command, you can do so by configuring \`only_officers_search\`. Use \`/config only_officers_search \`. This will optionally limit the \`/search\` command to users with the configured officer role. ` }, + { + name: "can_ignore_invoices", + description: ` + This configurations allows users who have been given an invoice to \`Ignore and Delete\` an invoice they choose rather than pay. This can be set to false, so users must pay their invoice.\n\n**Note:** Invoices are paid from a users bank balance, and not with their cash balance. + ` + }, { name: "view", description: ` diff --git a/structures/QuarksBot.js b/structures/QuarksBot.js index c6d7462..ec1e270 100644 --- a/structures/QuarksBot.js +++ b/structures/QuarksBot.js @@ -175,6 +175,7 @@ class QuarksBot extends Client { dispatcherRole: null, onlyOfficersSearch: false, casinoMultiplier: 2, + canIgnoreInvoices: true, } } this.dbo.collection("guilds").insertOne(guild, function(err, res) { @@ -199,6 +200,7 @@ class QuarksBot extends Client { onlyOfficersSearch: guild.server.onlyOfficersSearch, botAdmin: guild.server.botAdmin, casinoMultiplier: guild.server.casinoMultiplier, + canIgnoreInvoices: guild.server.canIgnoreInvoices, } return guildData; } diff --git a/structures/invoice.js b/structures/invoice.js new file mode 100644 index 0000000..4949efa --- /dev/null +++ b/structures/invoice.js @@ -0,0 +1,43 @@ +const mongoose = require('mongoose'); + +let invoiceSchema = mongoose.Schema({ + invoiceDB: { + userID: String, + guilds: {}, + } +}); + +invoiceSchema.methods.createInvoiceDB = function (userID, guildID) { + this.invoiceDB.userID = userID; + this.invoiceDB.guilds = {}; + this.invoiceDB.guilds[guildID] = { invoices: [] }; +}; + +/* + This function is to add a new guild specific invoice list to an already existing + invoiceDB document + or + can be used to reset a data back to default +*/ +async function addInvoiceList(guilds, guildID, userID, client) { + let updatedGuilds = guilds; + updatedGuilds[guildID] = { invoices: [] }; + + await client.dbo.collection("invoices").updateOne({"invoiceDB.userID":userID}, {$set: {"invoiceDB.guilds": updatedGuilds}}, function(err, res) { + if (err) return false + }) + return true +} + +async function addInvoice(guildID, userID, invoice, client) { + await client.dbo.collection("invoices").updateOne({"invoiceDB.userID": userID}, {$push: {[`invoiceDB.guilds.${guildID}.invoices`]: invoice}}, function(err, res) { + if (err) return false + }) + return true +} + +module.exports = { + Invoice: mongoose.model('Invoice', invoiceSchema), + addInvoiceList: addInvoiceList, + addInvoice: addInvoice, +};