Skip to content

Commit

Permalink
More item updates + global-blacklist fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMonDon committed Jul 27, 2024
1 parent 72a8bb6 commit 931bdc6
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 32 deletions.
22 changes: 12 additions & 10 deletions commands/Bot Support/global-blacklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class GlobalBlacklist extends Command {
name: 'global-blacklist',
description: 'Blacklist someone from using the bot',
usage: 'global-blacklist <Add | Remove | Check> <User> <Reason>',
requiredArgs: 1,
examples: ['global-blacklist check bunny', 'global-blacklist add bunny Being naughty'],
requiredArgs: 2,
category: 'Bot Support',
permLevel: 'Bot Support',
aliases: ['gbl', 'g-blacklist', 'gblacklist'],
Expand All @@ -26,6 +27,7 @@ class GlobalBlacklist extends Command {
return this.client.util.errorEmbed(msg, msg.settings.prefix + this.help.usage, 'Incorrect Usage');
} else {
type = args[0].toLowerCase();
mem = await this.client.util.getMember(msg, args[1]);
}
} else if (args[0]) {
// Check if the first argument is a user mention
Expand Down Expand Up @@ -59,13 +61,15 @@ class GlobalBlacklist extends Command {
if (!mem) {
return this.client.util.errorEmbed(msg, msg.settings.prefix + this.help.usage, 'User not found');
}
mem = mem.user ? mem.user : mem;

args.shift();
args.shift();
const reason = args.join(' ') || false;

const blacklist = await db.get(`users.${mem.id}.blacklist`);

console.log(mem);
const embed = new EmbedBuilder()
.setAuthor({ name: mem.tag, iconURL: mem.displayAvatarURL() })
.setColor(msg.settings.embedColor)
Expand All @@ -84,8 +88,8 @@ class GlobalBlacklist extends Command {
await db.set(`users.${mem.id}.blacklistReason`, reason);

embed.setTitle(`${mem.tag} has been added to the global blacklist.`).addFields([
{ name: 'Reason:', value: reason },
{ name: 'User:', value: `${mem.tag} \n(${mem.id})` },
{ name: 'Reason:', value: reason },
]);

msg.channel.send({ embeds: [embed] });
Expand All @@ -105,8 +109,8 @@ class GlobalBlacklist extends Command {
await db.set(`users.${mem.id}.blacklistReason`, reason);

embed.setTitle(`${mem.tag} has been removed from the global blacklist.`).addFields([
{ name: 'Reason:', value: reason },
{ name: 'User:', value: `${mem.tag} \n(${mem.id})` },
{ name: 'Reason:', value: reason },
]);

msg.channel.send({ embeds: [embed] });
Expand All @@ -117,13 +121,11 @@ class GlobalBlacklist extends Command {
case 'check': {
const reason = (await db.get(`users.${mem.id}.blacklistReason`)) || 'No reason specified';

embed
.setTitle(`${mem.tag} blacklist check`)
.addFields([
{ name: 'User:', value: `${mem.tag} (${mem.id})`, inline: true },
{ name: 'Is Blacklisted?', value: blacklist ? 'True' : 'False', inline: true },
])
.addField('Reason:', reason);
embed.setTitle(`${mem.tag} blacklist check`).addFields([
{ name: 'User:', value: `${mem.tag} (${mem.id})`, inline: true },
{ name: 'Is Blacklisted?', value: blacklist ? 'True' : 'False', inline: true },
{ name: 'Reason', value: reason, inline: true },
]);

msg.channel.send({ embeds: [embed] });
break;
Expand Down
44 changes: 24 additions & 20 deletions commands/Items/buy-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class BuyItem extends Command {

// Find the item in the store regardless of case
const itemKey = Object.keys(store).find((key) => key.toLowerCase() === itemName);

if (!itemKey) return msg.reply('That item does not exist in the store.');

const item = store[itemKey];
Expand Down Expand Up @@ -61,16 +60,15 @@ class BuyItem extends Command {
userCash = userCash - itemCost;
await db.set(`servers.${msg.guild.id}.users.${msg.member.id}.economy.cash`, userCash.toString());

if (item.roleGiven) {
const role = this.client.util.getRole(msg, item.roleGiven);
await msg.member.roles.add(role).catch((error) => msg.channel.send(error));
}
if (item.roleRemoved) {
const role = this.client.util.getRole(msg, item.roleRemoved);
await msg.member.roles.remove(role).catch((error) => msg.channel.send(error));
}

if (!item.inventory) {
if (item.roleGiven) {
const role = this.client.util.getRole(msg, item.roleGiven);
await msg.member.roles.add(role).catch((error) => msg.channel.send(error));
}
if (item.roleRemoved) {
const role = this.client.util.getRole(msg, item.roleRemoved);
await msg.member.roles.remove(role).catch((error) => msg.channel.send(error));
}
if (!item.replyMessage) {
return msg.channel.send('👍');
}
Expand Down Expand Up @@ -104,7 +102,6 @@ class BuyItem extends Command {
(await this.client.util.getRole(msg, item.roleRemoved)) ||
(await this.client.util.getRole(msg, item.roleRequired));

console.log(role);
if (role) {
const roleCreatedAt = moment(role.createdAt);
const roleCreated = roleCreatedAt.format('D MM YY');
Expand All @@ -123,14 +120,19 @@ class BuyItem extends Command {

const userInventory = (await db.get(`servers.${msg.guild.id}.users.${msg.member.id}.economy.inventory`)) || [];

// Check if the user already owns the item
const alreadyOwned = userInventory.find((inventoryItem) => inventoryItem.name.toLowerCase() === itemName);
if (alreadyOwned) return msg.reply('You already own this item.');

// Add the item to the user's inventory
userInventory.push({ name: itemKey, ...item });

await db.set(`servers.${msg.guild.id}.users.${msg.member.id}.economy.inventory`, userInventory);
// Find the index of the item in the user's inventory
const itemIndex = userInventory.findIndex((inventoryItem) => inventoryItem?.name?.toLowerCase() === itemName);
if (itemIndex !== -1) {
// If the item is found, increment the quantity
userInventory[itemIndex].quantity += 1;
await db.set(`servers.${msg.guild.id}.users.${msg.member.id}.economy.inventory`, userInventory);
} else {
// Add the item to the user's inventory
item.quantity = 1;
userInventory.push({ name: itemKey, ...item });

await db.set(`servers.${msg.guild.id}.users.${msg.member.id}.economy.inventory`, userInventory);
}

const currencySymbol = (await db.get(`servers.${msg.guild.id}.economy.symbol`)) || '$';
const csCost =
Expand All @@ -140,7 +142,9 @@ class BuyItem extends Command {

const embed = new EmbedBuilder()
.setTitle('Purchase Successful')
.setDescription(`You have successfully bought **${itemKey}** for ${csCost}.`)
.setDescription(
`You have bought ${itemKey} for ${csCost}! This is now in your inventory. \nUse this item with the \`use-item\` command.`,
)
.setColor(msg.settings.embedColor)
.setTimestamp();

Expand Down
5 changes: 3 additions & 2 deletions commands/Items/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ class Inventory extends Command {
const end = start + itemsPerPage;
const paginatedInventory = userInventory.slice(start, end);

const currencySymbol = (await db.get(`servers.${msg.guild.id}.economy.symbol`)) || '$';

Check failure on line 45 in commands/Items/inventory.js

View workflow job for this annotation

GitHub Actions / eslint

'currencySymbol' is assigned a value but never used
const inventoryDetails = paginatedInventory
.map((item) => {
return `**${item?.name}** - ${BigInt(item?.cost || 0)?.toLocaleString()}\n${item?.description}`;
return `**${item?.quantity || 1} - ${item?.name}**\n${item?.description}`;
})
.join('\n');

Expand Down Expand Up @@ -88,7 +89,7 @@ class Inventory extends Command {

const newInventoryDetails = newPaginatedInventory
.map((item) => {
return `**${item.name}** - ${BigInt(item.cost || 0)?.toLocaleString()}\n${item.description}`;
return `**${item?.quantity || 1} - ${item?.name}**\n${item?.description}`;
})
.join('\n');

Expand Down
104 changes: 104 additions & 0 deletions commands/Items/use-item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const Command = require('../../base/Command.js');
const { QuickDB } = require('quick.db');
require('moment-duration-format');
const moment = require('moment');
const db = new QuickDB();

class UseItem extends Command {
constructor(client) {
super(client, {
name: 'use-item',
category: 'Items',
description: 'Use an item from your inventory.',
usage: 'use-item <item name>',
aliases: ['useitem', 'use'],
guildOnly: true,
requiredArgs: 1,
});
}

async run(msg, args) {
const itemName = args.join(' ').toLowerCase();

// Fetch user's inventory from the database
const userInventory = (await db.get(`servers.${msg.guild.id}.users.${msg.author.id}.economy.inventory`)) || [];

// Find the item in the store regardless of case
const itemIndex = userInventory.findIndex((inventoryItem) => inventoryItem?.name?.toLowerCase() === itemName);

// Check if the item exists in the user's inventory
if (!userInventory[itemIndex]) {
return msg.reply('You do not have this item in your inventory.');
}

const item = userInventory[itemIndex];
userInventory[itemIndex].quantity -= 1;
let filteredInventory;

if (!item.quantity || item.quantity < 1) {
delete userInventory[itemIndex];
// Remove null values
filteredInventory = await userInventory.filter((item) => item != null);
}

await db.set(`servers.${msg.guild.id}.users.${msg.author.id}.economy.inventory`, filteredInventory);

if (item.roleGiven) {
const role = this.client.util.getRole(msg, item.roleGiven);
await msg.member.roles.add(role).catch((error) => msg.channel.send(error));
}
if (item.roleRemoved) {
const role = this.client.util.getRole(msg, item.roleRemoved);
await msg.member.roles.remove(role).catch((error) => msg.channel.send(error));
}
if (!item.replyMessage) {
return msg.channel.send('👍');
}

// Replace Member
const memberCreatedAt = moment(msg.author.createdAt);
const memberCreated = memberCreatedAt.format('D MM YY');
const memberCreatedDuration = memberCreatedAt.from(moment(), true);
let replyMessage = item.replyMessage
.replace('{member.id}', msg.author.id)
.replace('{member.username}', msg.author.username)
.replace('{member.tag}', msg.author.tag)
.replace('{member.mention}', msg.author)
.replace('{member.created}', memberCreated)
.replace('{member.created.duration}', memberCreatedDuration);

// Replace Server
const guildCreatedAt = moment(msg.guild.createdAt);
const serverCreated = guildCreatedAt.format('D MM YY');
const serverCreatedDuration = guildCreatedAt.from(moment(), true);

replyMessage = replyMessage
.replace('{server.id}', msg.guild.id)
.replace('{server.name}', msg.guild.name)
.replace('{server.members}', msg.guild.memberCount.toLocaleString())
.replace('{server.created}', serverCreated)
.replace('{servers.created.duration', serverCreatedDuration);

const role =
(await this.client.util.getRole(msg, item.roleGiven)) ||
(await this.client.util.getRole(msg, item.roleRemoved)) ||
(await this.client.util.getRole(msg, item.roleRequired));

if (role) {
const roleCreatedAt = moment(role.createdAt);
const roleCreated = roleCreatedAt.format('D MM YY');
const roleCreatedDuration = roleCreatedAt.from(moment(), true);

replyMessage = replyMessage
.replace('{role.id}', role.id)
.replace('{role.name}', role.name)
.replace('{role.mention}', role)
.replace('{role.members}', role.members.size.toLocaleString())
.replace('{role.created}', roleCreated)
.replace('{role.created.duration}', roleCreatedDuration);
}
return msg.channel.send(replyMessage);
}
}

module.exports = UseItem;

0 comments on commit 931bdc6

Please sign in to comment.