aboutsummaryrefslogtreecommitdiff
path: root/src/commands/utilities/price.ts
blob: 21781d16c821fb2821658182d6302b8e37a0e712 (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
210
211
import { Constants } from 'discord-akairo';
import { ColorResolvable, MessageEmbed } from 'discord.js';
import Fuse from 'fuse.js';
import fetch from 'node-fetch';
import { BushCommand, BushMessage } from '../../lib';

interface Summary {
	amount: number;
	pricePerUnit: number;
	orders: number;
}

interface Bazaar {
	success: boolean;
	lastUpdated: number;
	products: {
		[key: string]: {
			product_id: string;
			sell_summary: Summary[];
			buy_summary: Summary[];
			quick_status: {
				productId: string;
				sellPrice: number;
				sellVolume: number;
				sellMovingWeek: number;
				sellOrders: number;
				buyPrice: number;
				buyVolume: number;
				buyMovingWeek: number;
				buyOrders: number;
			};
		};
	};
}

interface LowestBIN {
	[key: string]: number;
}

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

export default class PriceCommand extends BushCommand {
	public constructor() {
		super('price', {
			aliases: ['price'],
			category: 'utilities',
			clientPermissions: ['EMBED_LINKS', 'SEND_MESSAGES'],
			description: {
				usage: 'price <item id>',
				examples: ['price ASPECT_OF_THE_END'],
				content: 'Finds the price information of an item.'
			},
			ratelimit: 4,
			cooldown: 4000,
			typing: true,
			args: [
				{
					id: 'item',
					match: Constants.ArgumentMatches.CONTENT,
					type: Constants.ArgumentTypes.STRING,
					prompt: {
						start: 'What item would you like to find the price of?',
						retry: '{error} Choose a valid item.'
					}
				},
				{
					id: 'strict',
					match: Constants.ArgumentMatches.FLAG,
					flag: '--strict',
					default: false
				}
			],
			slash: true,
			slashOptions: [
				{
					name: 'item',
					description: 'The item that you would you like to find the price of.',
					type: 'STRING',
					required: true
				},
				{
					name: 'strict',
					description: 'Whether or not to bypass the fuzzy search.',
					type: 'BOOLEAN',
					required: false
				}
			]
		});
	}

	public async exec(message: BushMessage, { item, strict }: { item: string; strict: boolean }): Promise<unknown> {
		const errors = new Array<string>();

		const [bazaar, currentLowestBIN, averageLowestBIN, auctionAverages]: [Bazaar, LowestBIN, LowestBIN, AuctionAverages] =
			await Promise.all([
				fetch('https://api.hypixel.net/skyblock/bazaar')
					.then((resp) => resp.json())
					.catch(() => errors.push('bazaar')),
				fetch('https://moulberry.codes/lowestbin.json')
					.then((resp) => resp.json())
					.catch(() => errors.push('current lowest BIN')),
				fetch('https://moulberry.codes/auction_averages_lbin/3day.json')
					.then((resp) => resp.json())
					.catch(() => errors.push('average Lowest BIN')),
				fetch('https://moulberry.codes/auction_averages/3day.json')
					.then((resp) => resp.json())
					.catch(() => errors.push('auction average'))
			]);

		let parsedItem = item.toString().toUpperCase().replace(/ /g, '_').replace(/'S/g, '');
		const priceEmbed = new MessageEmbed();

		if (errors?.length) {
			priceEmbed.setFooter(`Could not fetch data from ${this.client.util.oxford(errors, 'and', '')}`);
		}

		//combines all the item names from each
		const itemNames = Array.from(
			new Set(
				(averageLowestBIN ? Object.keys(averageLowestBIN) : []).concat(
					currentLowestBIN ? Object.keys(currentLowestBIN) : [],
					auctionAverages ? Object.keys(auctionAverages) : [],
					bazaar?.products ? Object.keys(bazaar.products) : []
				)
			)
		);

		// fuzzy search
		if (!strict) {
			parsedItem = new Fuse(itemNames)?.search(parsedItem)[0]?.item;
		}

		// If bazaar item then it there should not be any ah data
		if (bazaar['products'][parsedItem]) {
			const bazaarPriceEmbed = new MessageEmbed()
				.setColor(
					errors?.length
						? (this.client.util.emojis.warn as ColorResolvable)
						: (this.client.util.colors.success as ColorResolvable)
				)
				.setTitle(`Bazaar Information for \`${parsedItem}\``)
				.addField('Sell Price', Bazaar('sellPrice', 2, true))
				.addField('Buy Price', Bazaar('buyPrice', 2, true))
				.addField('Margin', (Number(Bazaar('buyPrice', 2, false)) - Number(Bazaar('sellPrice', 2, false))).toLocaleString())
				.addField('Current Sell Orders', Bazaar('sellOrders', 0, true))
				.addField('Current Buy Orders', Bazaar('buyOrders', 0, true));
			return await message.util.reply({ embeds: [bazaarPriceEmbed] });
		}

		// 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
				.setColor(this.client.util.colors.success)
				.setTitle(`Price Information for \`${parsedItem}\``)
				.setFooter('All information is based on the last 3 days.');
		} else {
			const errorEmbed = new MessageEmbed();
			errorEmbed
				.setColor(this.client.util.colors.error)
				.setDescription(
					`${this.client.util.emojis.error} \`${parsedItem}\` is not a valid item id, or it has no auction data.`
				);
			return await message.util.reply({ embeds: [errorEmbed] });
		}

		if (currentLowestBIN?.[parsedItem]) {
			const currentLowestBINPrice = currentLowestBIN[parsedItem].toLocaleString();
			priceEmbed.addField('Current Lowest BIN', currentLowestBINPrice);
		}
		if (averageLowestBIN?.[parsedItem]) {
			const averageLowestBINPrice = averageLowestBIN[parsedItem].toLocaleString();
			priceEmbed.addField('Average Lowest BIN', averageLowestBINPrice);
		}
		if (auctionAverages?.[parsedItem]?.price) {
			const auctionAveragesPrice = auctionAverages[parsedItem].price.toLocaleString();
			priceEmbed.addField('Average Auction Price', auctionAveragesPrice);
		}
		if (auctionAverages?.[parsedItem]?.count) {
			const auctionAveragesCountPrice = auctionAverages[parsedItem].count.toLocaleString();
			priceEmbed.addField('Average Auction Count', auctionAveragesCountPrice);
		}
		if (auctionAverages?.[parsedItem]?.sales) {
			const auctionAveragesSalesPrice = auctionAverages[parsedItem].sales.toLocaleString();
			priceEmbed.addField('Average Auction Sales', auctionAveragesSalesPrice);
		}
		if (auctionAverages?.[parsedItem]?.clean_price) {
			const auctionAveragesCleanPrice = auctionAverages[parsedItem].clean_price.toLocaleString();
			priceEmbed.addField('Average Auction Clean Price', auctionAveragesCleanPrice);
		}
		if (auctionAverages?.[parsedItem]?.clean_sales) {
			const auctionAveragesCleanSales = auctionAverages[parsedItem].clean_sales.toLocaleString();
			priceEmbed.addField('Average Auction Clean Sales', auctionAveragesCleanSales);
		}
		return await message.util.reply({ embeds: [priceEmbed] });

		//Helper functions
		function Bazaar(Information: string, digits: number, commas: boolean): string {
			const price = bazaar?.products[parsedItem]?.quick_status?.[Information];
			const a = Number(Number(price).toFixed(digits));
			return commas ? a?.toLocaleString() : a?.toString();
		}
	}
}