aboutsummaryrefslogtreecommitdiff
path: root/src/commands/utilities/price.ts
blob: 6f08aaa3dd516dc20889cf3caaee54a7bffa0e32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { ArgType, BotCommand, colors, emojis, format, formatList, type CommandMessage } from '#lib';
import assert from 'assert/strict';
import { ApplicationCommandOptionType, AutocompleteInteraction, EmbedBuilder } from 'discord.js';
import { default as Fuse } from 'fuse.js';

assert(Fuse);

export default class PriceCommand extends BotCommand {
	public static cachedItemList: string[] = [];
	public static readonly urls = [
		{ url: 'https://api.hypixel.net/skyblock/bazaar', error: 'bazaar' },
		{ url: 'https://moulberry.codes/lowestbin.json', error: 'current lowest BIN' },
		{ url: 'https://moulberry.codes/auction_averages_lbin/3day.json', error: 'average Lowest BIN' },
		{ url: 'https://moulberry.codes/auction_averages/3day.json', error: 'auction average' }
	] as const;

	public constructor() {
		super('price', {
			aliases: ['price'],
			category: 'utilities',
			description: 'Finds the price information of an item.',
			usage: ['price <item> [--strict]'],
			examples: ['price ASPECT_OF_THE_END'],
			args: [
				{
					id: 'item',
					description: 'The item that you would you like to find the price of.',
					type: 'string',
					match: 'content',
					prompt: 'What item would you like to find the price of?',
					retry: '{error} Choose a valid item.',
					slashType: ApplicationCommandOptionType.String,
					autocomplete: true
				},
				{
					id: 'strict',
					description: 'Whether or not to bypass the fuzzy search.',
					match: 'flag',
					flag: '--strict',
					prompt: 'Would you like to bypass the fuzzy search?',
					optional: true,
					slashType: ApplicationCommandOptionType.Boolean
				}
			],
			slash: true,
			clientPermissions: ['EmbedLinks'],
			clientCheckChannel: true,
			userPermissions: [],
			typing: true
		});
	}

	public override async exec(message: CommandMessage, args: { item: ArgType<'string'>; strict: ArgType<'flag'> }) {
		if (message.util.isSlashMessage(message)) await message.interaction.deferReply();
		const errors: string[] = [];

		const [bazaar, currentLowestBIN, averageLowestBIN, auctionAverages] = (await Promise.all(
			PriceCommand.urls.map(({ url, error }) =>
				fetch(url)
					.then((p) => (p.ok ? p.json() : undefined))
					.catch(() => (errors.push(error), undefined))
			)
		)) as [Bazaar?, LowestBIN?, LowestBIN?, AuctionAverages?];

		let parsedItem = args.item.toString().toUpperCase().replace(/ /g, '_').replace(/'S/g, '');
		const priceEmbed = new EmbedBuilder().setColor(errors?.length ? colors.warn : colors.success).setTimestamp();

		if (bazaar?.success === false) errors.push('bazaar');

		if (errors.length) {
			priceEmbed.setFooter({ text: `Could not fetch data for ${formatList(errors, 'and')}` });
		}

		// create a set from all the item names so that there are no duplicates for the fuzzy search
		const itemNames = new Set([
			...Object.keys(averageLowestBIN ?? {}),
			...Object.keys(currentLowestBIN ?? {}),
			...Object.keys(auctionAverages ?? {}),
			...Object.keys(bazaar?.products ?? {})
		]);

		// fuzzy search
		if (!args.strict) {
			parsedItem = new Fuse([...itemNames], {
				isCaseSensitive: false,
				findAllMatches: true,
				threshold: 0.7,
				ignoreLocation: true
			})?.search(parsedItem)[0]?.item;
		}

		// if its a bazaar item then it there should not be any ah data
		if (bazaar?.products?.[parsedItem]) {
			priceEmbed.setTitle(`Bazaar Information for ${format.input(parsedItem)}`).addFields(
				{ name: 'Sell Price', value: addBazaarInformation('sellPrice', 2, true) },
				{ name: 'Buy Price', value: addBazaarInformation('buyPrice', 2, true) },
				{
					name: 'Margin',
					value: (
						Number(addBazaarInformation('buyPrice', 2, false)) - Number(addBazaarInformation('sellPrice', 2, false))
					).toLocaleString()
				},
				{ name: 'Current Sell Orders', value: addBazaarInformation('sellOrders', 0, true) },
				{ name: 'Current Buy Orders', value: addBazaarInformation('buyOrders', 0, true) }
			);
			return await message.util.reply({ embeds: [priceEmbed] });
		}

		// checks if the item exists in any of the action information otherwise it is not a valid item
		if (currentLowestBIN?.[parsedItem] || averageLowestBIN?.[parsedItem] || auctionAverages?.[parsedItem]) {
			priceEmbed.setTitle(`Price Information for ${format.input(parsedItem)}`).setFooter({
				text: `${
					priceEmbed.data.footer?.text ? `${priceEmbed.data.footer.text} | ` : ''
				}All information is based on the last 3 days.`
			});
		} else {
			const errorEmbed = new EmbedBuilder();
			errorEmbed
				.setColor(colors.error)
				.setDescription(`${emojis.error} ${format.input(parsedItem)} is not a valid item id, or it has no auction data.`);
			return await message.util.reply({ embeds: [errorEmbed] });
		}

		addPrice('Current Lowest BIN', currentLowestBIN?.[parsedItem]);
		addPrice('Average Lowest BIN', averageLowestBIN?.[parsedItem]);
		addPrice('Average Auction Price', auctionAverages?.[parsedItem]?.price);
		addPrice('Average Auction Count', auctionAverages?.[parsedItem]?.count);
		addPrice('Average Auction Sales', auctionAverages?.[parsedItem]?.sales);
		addPrice('Average Auction Clean Price', auctionAverages?.[parsedItem]?.clean_price);
		addPrice('Average Auction Clean Sales', auctionAverages?.[parsedItem]?.clean_sales);

		return await message.util.reply({ embeds: [priceEmbed] });

		function addBazaarInformation(Information: keyof BazarProductQuickStatus, digits: number, commas: boolean): string {
			const price = bazaar?.products?.[parsedItem]?.quick_status?.[Information];
			return commas
				? Number(price)?.toLocaleString(undefined, { minimumFractionDigits: digits, maximumFractionDigits: digits })
				: Number(price)?.toFixed(digits);
		}
		function addPrice(name: string, price: number | undefined) {
			if (price)
				priceEmbed.addFields({
					name: name,
					value: price.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })
				});
		}
	}

	public override autocomplete(interaction: AutocompleteInteraction) {
		const fuzzy = new Fuse(PriceCommand.cachedItemList, {
			threshold: 0.5,
			isCaseSensitive: false,
			findAllMatches: true
		}).search(interaction.options.getFocused().toString());

		const res = fuzzy.slice(0, fuzzy.length >= 25 ? 25 : undefined).map((v) => ({ name: v.item, value: v.item }));

		void interaction.respond(res);
	}
}

export type ItemID = string;

export interface Bazaar {
	success: boolean;
	lastUpdated: number;
	products: {
		[key: ItemID]: BazarProduct;
	};
}

export interface BazarProduct {
	product_id: string;
	sell_summary: BazarSummary[];
	buy_summary: BazarSummary[];
	quick_status: BazarProductQuickStatus;
}

export interface BazarSummary {
	amount: number;
	pricePerUnit: number;
	orders: number;
}

export interface BazarProductQuickStatus {
	productId: ItemID;
	sellPrice: number;
	sellVolume: number;
	sellMovingWeek: number;
	sellOrders: number;
	buyPrice: number;
	buyVolume: number;
	buyMovingWeek: number;
	buyOrders: number;
}

export interface LowestBIN {
	[key: ItemID]: number;
}

export interface AuctionAverages {
	[key: ItemID]: {
		price?: number;
		count?: number;
		sales?: number;
		clean_price?: number;
		clean_sales?: number;
	};
}