Skip to content

Commit

Permalink
Add store links, add review stats, improve fields
Browse files Browse the repository at this point in the history
  • Loading branch information
acdvs committed Jul 26, 2020
1 parent 5498d7f commit d0ac3f5
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions lib/commands/deals.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ module.exports = async (msg, game, ignoredSellers) => {
}

// Can't rely on user input for the formal game name.
// Formal name also isn't returned with game data
// in the next step.
// Formal name also isn't returned with game data in the next step.
const gameInfo = await apiUtil.getGameInfo(gameId);
const gameName = gameInfo.title;
const gameData = await apiUtil.getGameData(gameId, ignoredSellers);
const list = gameData.list.filter(x => x.price_new < x.price_old);

Expand All @@ -43,14 +41,11 @@ module.exports = async (msg, game, ignoredSellers) => {
return;
}

const sellers = list.map(x => x.shop.name);
const sellers = list.map(x => `[${x.shop.name}](${x.url})`);
const newPrices = list.map(x => `${toCurrency(x.price_new)} (-${x.price_cut}%)`);
const oldPrices = list.map(x => toCurrency(x.price_old));

const histLowData = await apiUtil.getHistoricalLow(gameId);
const historicalLow = histLowData ? `${histLowData.shop.name} @ ${toCurrency(histLowData.price)} (-${histLowData.cut}%)` : 'None';

const steamReview = gameInfo.reviews && gameInfo.reviews.steam.text;

const embed = new Discord.MessageEmbed({
author: {
Expand All @@ -59,7 +54,7 @@ module.exports = async (msg, game, ignoredSellers) => {
image: {
url: gameInfo.image
},
title: gameName || gameId,
title: gameInfo.title || gameId,
url: gameData.urls.game,
color: 0x23B2D5,
fields: [
Expand All @@ -77,20 +72,26 @@ module.exports = async (msg, game, ignoredSellers) => {
name: 'Old Price',
value: oldPrices.join('\n'),
inline: true
},
{
name: 'Historical Low',
value: historicalLow
}

],
timestamp: new Date()
});
steamReview && embed.addFields(
{
name: 'Steam Review',
value: steamReview

if (histLowData) {
embed.addFields({
name: 'Historical Low',
value: `${toCurrency(histLowData.price)} (-${histLowData.cut}%) from ${histLowData.shop.name}`
});
}

if (gameInfo.reviews && gameInfo.reviews.steam) {
const steamReview = gameInfo.reviews.steam;

embed.addFields({
name: 'Steam User Review',
value: `${steamReview.text} (${steamReview.perc_positive}% from ${steamReview.total} users)`
});
}

msg.channel.send({ embed });
} catch (e) {
Expand All @@ -108,5 +109,7 @@ module.exports = async (msg, game, ignoredSellers) => {
* @returns {string}
*/
function toCurrency(num) {
return `$${Number.parseFloat(num).toFixed(2)}`;
const price = Number.parseFloat(num).toFixed(2);

return price > 0 ? `$${price}` : 'FREE';
}

0 comments on commit d0ac3f5

Please sign in to comment.