Skip to content

Commit 8edf9eb

Browse files
committed
Update buy/use with quantity
1 parent 931bdc6 commit 8edf9eb

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

commands/Items/buy-item.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class BuyItem extends Command {
1111
name: 'buy-item',
1212
category: 'Items',
1313
description: 'Buy an item from the store.',
14-
usage: 'buy-item <item>',
14+
usage: 'buy-item [quantity] <item>',
1515
examples: ['buy pizza'],
1616
aliases: ['buy'],
1717
requiredArgs: 1,
@@ -20,7 +20,20 @@ class BuyItem extends Command {
2020
}
2121

2222
async run(msg, args) {
23-
const itemName = args.join(' ').toLowerCase();
23+
let quantity = 1;
24+
let itemName = args.join(' ').toLowerCase();
25+
if (!isNaN(args[0])) {
26+
quantity = parseInt(args[0]);
27+
if (quantity <= 0) {
28+
return msg.reply('Invalid `[quantity]` argument given. Cannot be less than 1');
29+
}
30+
args.shift();
31+
itemName = args.join(' ').toLowerCase();
32+
if (!itemName) {
33+
return msg.reply('Invalid `<item name>` argument given');
34+
}
35+
}
36+
2437
if (!itemName) return msg.reply('Please specify an item to buy.');
2538

2639
const store = (await db.get(`servers.${msg.guild.id}.economy.store`)) || {};
@@ -57,7 +70,7 @@ class BuyItem extends Command {
5770
}
5871

5972
// Deduct the cost from the user's cash
60-
userCash = userCash - itemCost;
73+
userCash = userCash - itemCost * BigInt(quantity);
6174
await db.set(`servers.${msg.guild.id}.users.${msg.member.id}.economy.cash`, userCash.toString());
6275

6376
if (!item.inventory) {
@@ -124,26 +137,29 @@ class BuyItem extends Command {
124137
const itemIndex = userInventory.findIndex((inventoryItem) => inventoryItem?.name?.toLowerCase() === itemName);
125138
if (itemIndex !== -1) {
126139
// If the item is found, increment the quantity
127-
userInventory[itemIndex].quantity += 1;
140+
userInventory[itemIndex].quantity += quantity;
128141
await db.set(`servers.${msg.guild.id}.users.${msg.member.id}.economy.inventory`, userInventory);
129142
} else {
130143
// Add the item to the user's inventory
131-
item.quantity = 1;
144+
item.quantity = quantity;
132145
userInventory.push({ name: itemKey, ...item });
133146

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

137150
const currencySymbol = (await db.get(`servers.${msg.guild.id}.economy.symbol`)) || '$';
151+
const itemCostQuantity = (itemCost * BigInt(quantity)).toLocaleString();
138152
const csCost =
139-
itemCost.toLocaleString().length > 700
140-
? currencySymbol + itemCost.toLocaleString().slice(0, 700) + '...'
141-
: currencySymbol + itemCost.toLocaleString();
153+
itemCostQuantity.length > 700
154+
? currencySymbol + itemCostQuantity.slice(0, 700) + '...'
155+
: currencySymbol + itemCostQuantity;
142156

143157
const embed = new EmbedBuilder()
144158
.setTitle('Purchase Successful')
145159
.setDescription(
146-
`You have bought ${itemKey} for ${csCost}! This is now in your inventory. \nUse this item with the \`use-item\` command.`,
160+
`You have bought ${quantity} ${itemKey}${
161+
quantity > 1 ? 's' : ''
162+
} for ${csCost}! This is now in your inventory. \nUse this item with the \`use-item\` command.`,
147163
)
148164
.setColor(msg.settings.embedColor)
149165
.setTimestamp();

commands/Items/inventory.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class Inventory extends Command {
4242
const end = start + itemsPerPage;
4343
const paginatedInventory = userInventory.slice(start, end);
4444

45-
const currencySymbol = (await db.get(`servers.${msg.guild.id}.economy.symbol`)) || '$';
4645
const inventoryDetails = paginatedInventory
4746
.map((item) => {
4847
return `**${item?.quantity || 1} - ${item?.name}**\n${item?.description}`;

commands/Items/use-item.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,27 @@ class UseItem extends Command {
1010
name: 'use-item',
1111
category: 'Items',
1212
description: 'Use an item from your inventory.',
13-
usage: 'use-item <item name>',
13+
usage: 'use-item [quantity] <item name>',
1414
aliases: ['useitem', 'use'],
1515
guildOnly: true,
1616
requiredArgs: 1,
1717
});
1818
}
1919

2020
async run(msg, args) {
21-
const itemName = args.join(' ').toLowerCase();
21+
let quantity = 1;
22+
let itemName = args.join(' ').toLowerCase();
23+
if (!isNaN(args[0])) {
24+
quantity = parseInt(args[0]);
25+
if (quantity <= 0) {
26+
return msg.reply('Invalid `[quantity]` argument given. Cannot be less than 1');
27+
}
28+
args.shift();
29+
itemName = args.join(' ').toLowerCase();
30+
if (!itemName) {
31+
return msg.reply('Invalid `<item name>` argument given');
32+
}
33+
}
2234

2335
// Fetch user's inventory from the database
2436
const userInventory = (await db.get(`servers.${msg.guild.id}.users.${msg.author.id}.economy.inventory`)) || [];
@@ -32,8 +44,8 @@ class UseItem extends Command {
3244
}
3345

3446
const item = userInventory[itemIndex];
35-
userInventory[itemIndex].quantity -= 1;
36-
let filteredInventory;
47+
userInventory[itemIndex].quantity -= quantity;
48+
let filteredInventory = userInventory;
3749

3850
if (!item.quantity || item.quantity < 1) {
3951
delete userInventory[itemIndex];

0 commit comments

Comments
 (0)