From fe9bc52decf4e7242f85e7ce7f6db7681c4dccb1 Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Tue, 5 Jul 2022 23:30:33 -0700 Subject: [PATCH] add bal command --- commands/bal.js | 102 ++++++++++++++++++++++++++++++++++++++ commands/inventory-add.js | 7 ++- structures/QuarksBot.js | 23 +++++---- 3 files changed, 121 insertions(+), 11 deletions(-) create mode 100644 commands/bal.js diff --git a/commands/bal.js b/commands/bal.js new file mode 100644 index 0000000..6cd0c9b --- /dev/null +++ b/commands/bal.js @@ -0,0 +1,102 @@ +const { MessageEmbed, Guild } = require('discord.js'); + +module.exports = { + name: "bal", + description: "View balance", + usage: "[user]", + permissions: { + channel: ["VIEW_CHANNEL", "SEND_MESSAGES", "EMBED_LINKS"], + member: [], + }, + SlashCommand: { + options: [ + { + name: "user", + description: "user to view ballance", + value: "user", + type: 6, + required: false, + } + ], + /** + * + * @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.`); + } + + let balanceEmbed = new MessageEmbed() + .setColor(client.config.EmbedColor); + + if (args&&args[0]) { + // Show target users balance + + let targetUserID = args[0].value.replace('<@!', '').replace('>', ''); + let targetUserBanking = await client.dbo.collection("banking").findOne({"account.userID":targetUserID}).then(inventory => inventory); + + if (!targetUserBanking) { + targetUserBanking = { + account: { + userID: interaction.member.user.id, + ballance: GuildDB.startingBalance, + cash: 0.00, + } + } + + client.dbo.collection("banking").insertOne(targetUserBanking, function(err, res) { + if (err) { + client.log(err); + let embed = new MessageEmbed() + .setTitle(err) + .setColor("RED") + + return interaction.send({ embeds: [embed] }); + } + }); + } + + // This lame line of code to get username without ping on discord + const User = client.users.cache.get(targetUserID); + + balanceEmbed.setTitle(`${User.tag}'s Balance`); + balanceEmbed.setDescription(`**Bank:** $${targetUserBanking.account.ballance}\n**Cash:** $${targetUserBanking.account.cash}`); + + } else { + // Show command authors balance + + let banking = await client.dbo.collection("banking").findOne({"account.userID": interaction.member.user.id}).then(banking => banking); + + if (!banking) { + banking = { + account: { + userID: interaction.member.user.id, + ballance: GuildDB.startingBalance, + cash: 0.00, + } + } + + client.dbo.collection("banking").insertOne(banking, function(err, res) { + if (err) { + client.log(err); + let embed = new MessageEmbed() + .setTitle(err) + .setColor("RED") + + return interaction.send({ embeds: [embed] }); + } + }); + } + + balanceEmbed.setTitle('Your Balance'); + balanceEmbed.setDescription(`**Bank:** $${banking.account.ballance}\n**Cash:** $${banking.account.cash}`); + } + + return interaction.send({ embeds: [balanceEmbed] }); + }, + }, +} \ No newline at end of file diff --git a/commands/inventory-add.js b/commands/inventory-add.js index 8ba7d00..f401af0 100644 --- a/commands/inventory-add.js +++ b/commands/inventory-add.js @@ -113,8 +113,11 @@ module.exports = { .catch(err => { if (err) { client.log(err); - const channel = interaction.guild.channels.cache.find(channel => channel.id === GuildDB.tweetChannel); - return client.sendError(channel, err); + let embed = new MessageEmbed() + .setTitle(err) + .setColor("RED") + + return interaction.send({ embeds: [embed] }); } }); } else inventory = inventory.inventory; diff --git a/structures/QuarksBot.js b/structures/QuarksBot.js index 1961b3f..b22dec2 100644 --- a/structures/QuarksBot.js +++ b/structures/QuarksBot.js @@ -119,8 +119,10 @@ class QuarksBot extends Client { } async GetGuild(GuildId) { + let serverID = GuildId; let customChannelStatus, allowedChannels; let actionChannel, tweetChannel; + let startingBalance; let guild = await this.dbo.collection("guilds").findOne({"server.serverID":GuildId}).then(guild => guild); // If guild not found, generate guild default @@ -129,31 +131,34 @@ class QuarksBot extends Client { server: { serverID: GuildId, hasCustomChannels: false, + allowedChannels: [], actionChannel: null, - tweetChannel: null + tweetChannel: null, + startingBalance: 1000.00, } } this.dbo.collection("guilds").insertOne(newGuild, function(err, res) { if (err) throw err; }); customChannelStatus = newGuild.server.hasCustomChannels; - allowedChannels = null; - actionChannel = null; - tweetChannel = null; + allowedChannels = newGuild.server.allowedChannels; + actionChannel = newGuild.server.actionChannel; + tweetChannel = newGuild.server.tweetChannel; + startingBalance = newGuild.server.startingBalance; } else { customChannelStatus = guild.server.hasCustomChannels; + allowedChannels = guild.server.allowedChannels; actionChannel = guild.server.actionChannel; tweetChannel = guild.server.tweetChannel; - if (this.exists(guild.server.allowedChannels) && guild.server.allowedChannels.length>0) { - allowedChannels = guild.server.allowedChannels; - } else allowedChannels = null; + startingBalance = guild.server.startingBalance; } let guildData = { - allowedChannels: allowedChannels, + serverID: serverID, customChannelStatus: customChannelStatus, + allowedChannels: allowedChannels, actionChannel: actionChannel, tweetChannel: tweetChannel, - serverID: GuildId + startingBalance: startingBalance } return guildData; }