TravBot-v3/src/commands/fun/modules/eco-shop.ts

86 lines
3.2 KiB
TypeScript
Raw Normal View History

import {Command, NamedCommand, paginate, RestCommand} from "onion-lasers";
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, MessageEmbedOptions} from "discord.js";
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)) {
function getShopEmbed(selection: ShopItem[], title: string): MessageEmbedOptions {
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})`,
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 {
color: ECO_EMBED_COLOR,
title: title,
fields: fields,
footer: {
text: "Mon Shop | TravBot Services"
2020-12-15 01:44:28 +00:00
}
};
}
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
paginate(send, author.id, pageAmount, (page, hasMultiplePages) => {
return {
embeds: [
getShopEmbed(
shopPages[page],
hasMultiplePages ? `Shop (Page ${page + 1} of ${pageAmount})` : "Shop"
)
]
};
});
2020-12-15 01:44:28 +00:00
}
}
});
export const BuyCommand = new NamedCommand({
2020-12-15 01:44:28 +00:00
description: "Buys an item from the shop.",
usage: "<item>",
run: "You need to specify an item to buy.",
any: new RestCommand({
async run({send, guild, channel, message, author, combined}) {
if (isAuthorized(guild, channel)) {
let found = false;
let amount = 1; // The amount the user is buying.
2020-12-14 07:13:16 +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
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
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
found = true;
break;
2020-12-15 01:44:28 +00:00
}
}
2020-12-14 07:13:16 +00:00
if (!found) send(`There's no item in the shop that goes by \`${combined}\`!`);
}
2020-12-15 01:44:28 +00:00
}
})
});