2021-04-13 12:38:52 +00:00
|
|
|
import {Command, NamedCommand, paginate, RestCommand} from "onion-lasers";
|
2021-04-05 04:35:12 +00:00
|
|
|
import {pluralise, split} from "../../../lib";
|
|
|
|
import {Storage, getPrefix} from "../../../structures";
|
2020-12-15 11:15:28 +00:00
|
|
|
import {isAuthorized, ECO_EMBED_COLOR} from "./eco-utils";
|
2020-12-15 01:44:28 +00:00
|
|
|
import {ShopItems, ShopItem} from "./eco-shop-items";
|
|
|
|
import {EmbedField} from "discord.js";
|
2020-11-04 08:04:07 +00:00
|
|
|
|
2021-04-05 04:35:12 +00:00
|
|
|
export const ShopCommand = new NamedCommand({
|
2020-12-15 01:44:28 +00:00
|
|
|
description: "Displays the list of items you can buy in the shop.",
|
2021-04-10 13:34:55 +00:00
|
|
|
async run({send, guild, channel, author}) {
|
2020-12-15 01:44:28 +00:00
|
|
|
if (isAuthorized(guild, channel)) {
|
2021-03-30 12:16:31 +00:00
|
|
|
function getShopEmbed(selection: ShopItem[], title: string) {
|
2020-12-15 01:44:28 +00:00
|
|
|
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({
|
2020-12-15 07:56:09 +00:00
|
|
|
name: `**${item.title}** (${getPrefix(guild)}eco buy ${item.usage})`,
|
2021-03-30 10:25:07 +00:00
|
|
|
value: `${item.description} Costs ${pluralise(item.cost, "Mon", "s")}.`,
|
2020-12-15 01:44:28 +00:00
|
|
|
inline: false
|
|
|
|
});
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
return {
|
|
|
|
embed: {
|
2020-12-15 11:15:28 +00:00
|
|
|
color: ECO_EMBED_COLOR,
|
2020-12-15 01:44:28 +00:00
|
|
|
title: title,
|
|
|
|
fields: fields,
|
|
|
|
footer: {
|
|
|
|
text: "Mon Shop | TravBot Services"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2021-03-30 12:16:31 +00:00
|
|
|
const shopPages = split(ShopItems, 5);
|
|
|
|
const pageAmount = shopPages.length;
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2021-04-11 10:45:50 +00:00
|
|
|
paginate(send, author.id, pageAmount, (page, hasMultiplePages) => {
|
|
|
|
return getShopEmbed(
|
|
|
|
shopPages[page],
|
|
|
|
hasMultiplePages ? `Shop (Page ${page + 1} of ${pageAmount})` : "Shop"
|
|
|
|
);
|
|
|
|
});
|
2020-12-15 01:44:28 +00:00
|
|
|
}
|
2020-11-04 08:04:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-05 04:35:12 +00:00
|
|
|
export const BuyCommand = new NamedCommand({
|
2020-12-15 01:44:28 +00:00
|
|
|
description: "Buys an item from the shop.",
|
|
|
|
usage: "<item>",
|
2021-04-10 17:07:55 +00:00
|
|
|
run: "You need to specify an item to buy.",
|
|
|
|
any: new RestCommand({
|
2021-04-12 07:44:23 +00:00
|
|
|
async run({send, guild, channel, message, author, combined}) {
|
2021-04-10 17:07:55 +00:00
|
|
|
if (isAuthorized(guild, channel)) {
|
|
|
|
let found = false;
|
|
|
|
let amount = 1; // The amount the user is buying.
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2021-04-10 17:07:55 +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
|
|
|
|
2021-04-10 17:07:55 +00:00
|
|
|
for (let item of ShopItems) {
|
|
|
|
if (item.usage === combined) {
|
|
|
|
const user = Storage.getUser(author.id);
|
|
|
|
const cost = item.cost * amount;
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2021-04-10 17:07:55 +00:00
|
|
|
if (cost > user.money) {
|
|
|
|
send("Not enough Mons!");
|
|
|
|
} else {
|
|
|
|
user.money -= cost;
|
|
|
|
Storage.save();
|
|
|
|
item.run(message, cost, amount);
|
|
|
|
}
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2021-04-10 17:07:55 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
2020-12-15 01:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-14 07:13:16 +00:00
|
|
|
|
2021-04-10 17:07:55 +00:00
|
|
|
if (!found) send(`There's no item in the shop that goes by \`${combined}\`!`);
|
|
|
|
}
|
2020-12-15 01:44:28 +00:00
|
|
|
}
|
2021-04-10 17:07:55 +00:00
|
|
|
})
|
2020-11-04 08:04:07 +00:00
|
|
|
});
|