mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-22 10:19:11 +02:00
Split fetchStocks into its own function
This commit is contained in:
+31
-17
@@ -39,26 +39,18 @@ module.exports = class StocksCommand extends Command {
|
|||||||
try {
|
try {
|
||||||
const company = await this.search(query);
|
const company = await this.search(query);
|
||||||
if (!company) return msg.say('Could not find any results.');
|
if (!company) return msg.say('Could not find any results.');
|
||||||
const { body } = await request
|
const stocks = await this.fetchStocks(company.symbol);
|
||||||
.get('https://www.alphavantage.co/query')
|
if (!stocks) return msg.say('Could not find any results.');
|
||||||
.query({
|
|
||||||
function: 'TIME_SERIES_INTRADAY',
|
|
||||||
symbol: company.symbol,
|
|
||||||
interval: '1min',
|
|
||||||
apikey: ALPHA_VANTAGE_KEY
|
|
||||||
});
|
|
||||||
if (body['Error Message']) return msg.say('Could not find any results.');
|
|
||||||
const data = Object.values(body['Time Series (1min)'])[0];
|
|
||||||
const embed = new MessageEmbed()
|
const embed = new MessageEmbed()
|
||||||
.setTitle(`Stocks for ${company.name} (${company.symbol.toUpperCase()})`)
|
.setTitle(`Stocks for ${company.name} (${stocks.symbol.toUpperCase()})`)
|
||||||
.setColor(0x9797FF)
|
.setColor(0x9797FF)
|
||||||
.setFooter('Last Updated')
|
.setFooter('Last Updated')
|
||||||
.setTimestamp(new Date(body['Meta Data']['3. Last Refreshed']))
|
.setTimestamp(stocks.lastRefresh)
|
||||||
.addField('❯ Open', `$${formatNumber(data['1. open'])}`, true)
|
.addField('❯ Open', `$${formatNumber(stocks.open)}`, true)
|
||||||
.addField('❯ Close', `$${formatNumber(data['4. close'])}`, true)
|
.addField('❯ Close', `$${formatNumber(stocks.close)}`, true)
|
||||||
.addField('❯ Volume', formatNumber(data['5. volume']), true)
|
.addField('❯ Volume', formatNumber(stocks.volume), true)
|
||||||
.addField('❯ High', `$${formatNumber(data['2. high'])}`, true)
|
.addField('❯ High', `$${formatNumber(stocks.high)}`, true)
|
||||||
.addField('❯ Low', `$${formatNumber(data['3. low'])}`, true)
|
.addField('❯ Low', `$${formatNumber(stocks.low)}`, true)
|
||||||
.addField('\u200B', '\u200B', true);
|
.addField('\u200B', '\u200B', true);
|
||||||
return msg.embed(embed);
|
return msg.embed(embed);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -77,4 +69,26 @@ module.exports = class StocksCommand extends Command {
|
|||||||
if (!body.ResultSet.Result.length) return null;
|
if (!body.ResultSet.Result.length) return null;
|
||||||
return body.ResultSet.Result[0];
|
return body.ResultSet.Result[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fetchStocks(symbol) {
|
||||||
|
const { body } = await request
|
||||||
|
.get('https://www.alphavantage.co/query')
|
||||||
|
.query({
|
||||||
|
function: 'TIME_SERIES_INTRADAY',
|
||||||
|
symbol,
|
||||||
|
interval: '1min',
|
||||||
|
apikey: ALPHA_VANTAGE_KEY
|
||||||
|
});
|
||||||
|
if (body['Error Message']) return null;
|
||||||
|
const data = Object.values(body['Time Series (1min)'])[0];
|
||||||
|
return {
|
||||||
|
symbol,
|
||||||
|
open: data['1. open'],
|
||||||
|
high: data['2. high'],
|
||||||
|
low: data['3. low'],
|
||||||
|
close: data['4. close'],
|
||||||
|
volume: data['5. volume'],
|
||||||
|
lastRefresh: new Date(body['Meta Data']['3. Last Refreshed'])
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user