2020-12-15 01:44:28 +00:00
|
|
|
import Command from "../../../core/command";
|
|
|
|
import $ from "../../../core/lib";
|
|
|
|
import {Storage, getPrefix} from "../../../core/structures";
|
|
|
|
import {isAuthorized} from "./eco-utils";
|
|
|
|
import {ShopItems, ShopItem} from "./eco-shop-items";
|
|
|
|
import {EmbedField} from "discord.js";
|
2020-11-04 08:04:07 +00:00
|
|
|
|
2020-12-14 07:13:16 +00:00
|
|
|
export const ShopCommand = new Command({
|
2020-12-15 01:44:28 +00:00
|
|
|
description: "Displays the list of items you can buy in the shop.",
|
|
|
|
async run({guild, channel, author}) {
|
|
|
|
if (isAuthorized(guild, channel)) {
|
|
|
|
function getShopEmbed(selection: ShopItem[], title = "Shop") {
|
|
|
|
const fields: EmbedField[] = [];
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
for (const item of selection)
|
|
|
|
fields.push({
|
|
|
|
name: `**${item.title}** (${getPrefix(guild)}eco buy ${
|
|
|
|
item.usage
|
|
|
|
})`,
|
|
|
|
value: `${item.description} Costs ${$(
|
|
|
|
item.cost
|
|
|
|
).pluralise("Mon", "s")}.`,
|
|
|
|
inline: false
|
|
|
|
});
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
return {
|
|
|
|
embed: {
|
|
|
|
color: 0xf1c40f,
|
|
|
|
title: title,
|
|
|
|
fields: fields,
|
|
|
|
footer: {
|
|
|
|
text: "Mon Shop | TravBot Services"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
// In case there's just one page, omit unnecessary details.
|
|
|
|
if (ShopItems.length <= 5) channel.send(getShopEmbed(ShopItems));
|
|
|
|
else {
|
|
|
|
const shopPages = $(ShopItems).split(5);
|
|
|
|
const pageAmount = shopPages.length;
|
|
|
|
const msg = await channel.send(
|
|
|
|
getShopEmbed(shopPages[0], `Shop (Page 1 of ${pageAmount})`)
|
|
|
|
);
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
$.paginate(msg, author.id, pageAmount, (page) => {
|
|
|
|
msg.edit(
|
|
|
|
getShopEmbed(
|
|
|
|
shopPages[page],
|
|
|
|
`Shop (Page ${page + 1} of ${pageAmount})`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-11-04 08:04:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-12-14 07:13:16 +00:00
|
|
|
export const BuyCommand = new Command({
|
2020-12-15 01:44:28 +00:00
|
|
|
description: "Buys an item from the shop.",
|
|
|
|
usage: "<item>",
|
|
|
|
async run({guild, channel, args, message, author}) {
|
|
|
|
if (isAuthorized(guild, channel)) {
|
|
|
|
let found = false;
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
let amount = 1; // The amount the user is buying.
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
// For now, no shop items support being bought multiple times. Uncomment these 2 lines when it's supported/needed.
|
|
|
|
//if (/\d+/g.test(args[args.length - 1]))
|
|
|
|
//amount = parseInt(args.pop());
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
let requested = args.join(" "); // The item the user is buying.
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
for (let item of ShopItems) {
|
|
|
|
if (item.usage === requested) {
|
|
|
|
const user = Storage.getUser(author.id);
|
|
|
|
const cost = item.cost * amount;
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
if (cost > user.money) {
|
|
|
|
channel.send("Not enough Mons!");
|
|
|
|
} else {
|
|
|
|
user.money -= cost;
|
|
|
|
Storage.save();
|
|
|
|
item.run(message, cost, amount);
|
|
|
|
}
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
if (!found)
|
|
|
|
channel.send(
|
|
|
|
`There's no item in the shop that goes by \`${requested}\`!`
|
|
|
|
);
|
|
|
|
}
|
2020-11-04 08:04:07 +00:00
|
|
|
}
|
|
|
|
});
|