Skip to content

Commit

Permalink
Merge pull request #7 from adamdavies001/develop
Browse files Browse the repository at this point in the history
Add !ignoredsellers, game title search, and !deals embed improvements
  • Loading branch information
acdvs authored Jul 17, 2020
2 parents 2b66cf3 + 47983de commit 783187f
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 53 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ Help me help you!
- `!deals [game]`
Gets a list of current deals for the specified game including an ITAD link, store names, discount percentage, and current and original prices, and metacritic user score.
Replace `[game]` with a full game name. Lookup relies on spelling, so misspellings may return nothing.
- `!wishlist [game list]`
Gets a list of current deals for the multiple specified games including an ITAD link, store names, discount percentage, and current and original prices, and metacritic user score.
If a game title has more than one word, seperate the words with hyphens `-`
- `!ignoredsellers`
Lists all ignored sellers
- `!ignoredsellers [add|remove] [seller]`
Adds or removes an ignored seller

![Look at these deals](resources/readme/deals-example.gif)

## Features-in-Progress
Server wishlist
- `!wishlist [game list]`
Gets a list of current deals for the multiple specified games including an ITAD link, store names, discount percentage, and current and original prices, and metacritic user score.
For multiple games, use a comma-sperated list

## License

Expand Down
35 changes: 26 additions & 9 deletions lib/commands/deals.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@ module.exports = async (msg, game, ignoredSellers) => {
const gameId = await apiUtil.getGameId(game);

if (!gameId) {
msg.channel.send(`Could not look up ${game}. Did you spell it correctly?`);
const gameList = await apiUtil.search(game);

if (gameList.length > 0) {
// Map titles, remove duplicates, sort alphabetically
const titles = gameList
.map(x => x.title)
.filter((v, i, a) => a.indexOf(v) === i)
.sort();

msg.channel.send(`Could not look up "${game}".\n\n**Here are some suggestions:**\n${titles.join('\n')}`);
} else {
msg.channel.send(`Could not look up "${game}". Did you spell it correctly?`);
}

return;
}

// Can't rely on user input for the formal game name.
// Formal name also isn't returned with game data
// in the next step.
const gameName = await apiUtil.getGameName(gameId);
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 @@ -36,14 +50,15 @@ module.exports = async (msg, game, ignoredSellers) => {
const histLowData = await apiUtil.getHistoricalLow(gameId);
const historicalLow = histLowData ? `${histLowData.shop.name} @ ${toCurrency(histLowData.price)} (-${histLowData.cut}%)` : 'None';

const metacriticScoreData = await apiUtil.getMetacriticData(gameId);
const metacriticScore = metacriticScoreData.metacritic ? metacriticScoreData.metacritic.user_score : 'None';
const metacriticUrl = metacriticScoreData.metacritic && metacriticScoreData.urls.metacritic;
const steamReview = gameInfo.reviews && gameInfo.reviews.steam.text;

const embed = new Discord.MessageEmbed({
author: {
name: 'Deal Lookup'
},
image: {
url: gameInfo.image
},
title: gameName || gameId,
url: gameData.urls.game,
color: 0x23B2D5,
Expand All @@ -66,14 +81,16 @@ module.exports = async (msg, game, ignoredSellers) => {
{
name: 'Historical Low',
value: historicalLow
},
{
name: 'Metacritic User Score',
value: metacriticUrl ? `[${metacriticScore}](${metacriticUrl})` : metacriticScore
}

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

msg.channel.send({ embed });
} catch (e) {
Expand Down
13 changes: 4 additions & 9 deletions lib/commands/help.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const PREFIX = require('./index').COMMAND_PREFIX;

/**
* !help
* @param {Object} msg Discord message
Expand All @@ -10,16 +8,13 @@ module.exports = (msg) => {
msg.channel.send([
'```',
'Commands: help, deals, wishlist, ignoredsellers\n',
`- ${PREFIX}help`,
`- !help`,
' This command',
`- ${PREFIX}deals [game name]`,
`- !deals [game name]`,
' Gets current deals on a specific game',
`- ${PREFIX}wishlist [game names]`,
' Gets current deals on multiple games, add a \'-\' for games',
' with multiple words',
`- ${PREFIX}ignoredsellers`,
`- !ignoredsellers`,
' Lists all ignored sellers',
`- ${PREFIX}ignoredsellers [add|remove] [seller]`,
`- !ignoredsellers [add|remove] [seller]`,
' Adds or removes an ignored seller',
'```'
].join('\n'));
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/wishlist.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const getDeals = require('./getDeals.js');
const getDeals = require('./deals.js');

/**
* !wishlist -deals
Expand All @@ -10,7 +10,7 @@ const getDeals = require('./getDeals.js');
*/
module.exports = (msg, gameList, getDealsFlag) => {
// Right now this will return deals for multiple games, need to save the list
gameList = gameList.slice().split(' ');
gameList = gameList.slice().split(',');

for (let game of gameList) {
getDeals(msg, game);
Expand Down
60 changes: 31 additions & 29 deletions lib/util/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,63 @@ const axios = require('axios');
const ITAD_KEY = process.env.ITAD_KEY;
const BASE_URL = 'https://api.isthereanydeal.com';

/**
* Search for game by name
* @param {string} game
* @returns {Array}
*/
const search = async (game) => {
const res = await axios.get(`${BASE_URL}/v01/search/search/`, {
params: {
key: ITAD_KEY,
q: game,
region: 'us',
country: 'US'
}
});

const searchData = res.data.data;
const list = searchData && searchData.list.sort((a, b) => a.title.length - b.title.length);

return list;
};

/**
* Get ITAD game ID
* @param {string} game
* @returns {string}
*/
const getGameId = async (game) => {
const plainRes = await axios.get(`${BASE_URL}/v02/game/plain/`, {
const res = await axios.get(`${BASE_URL}/v02/game/plain/`, {
params: {
key: ITAD_KEY,
title: game
}
});

const plainData = plainRes.data.data;
const plainData = res.data.data;
const gameId = plainData && plainData.plain;

return gameId;
};

/**
* Get formal game name from ID
* Get game info from ID
* @param {string} gameId ITAD internal game ID
* @returns {string}
* @returns {Object}
*/
const getGameName = async (gameId) => {
const getGameInfo = async (gameId) => {
const res = await axios.get(`${BASE_URL}/v01/game/info/`, {
params: {
key: ITAD_KEY,
plains: gameId
plains: gameId,
optional: 'metacritic'
}
});

const infoData = res.data.data && res.data.data[gameId];
const gameName = infoData.title;

return gameName;
return infoData;
};

/**
Expand Down Expand Up @@ -84,25 +105,6 @@ const getHistoricalLow = async (gameId) => {
return gameData;
};

/**
* Get Metacritic game summary and reviews
* @param {string} gameId ITAD internal game ID
* @returns {Object}
*/
const getMetacriticData = async (gameId) => {
const res = await axios.get(`${BASE_URL}/v01/game/info/`, {
params: {
key: ITAD_KEY,
plains: gameId,
optional: 'metacritic'
}
});

const gameData = res.data.data && res.data.data[gameId];

return gameData;
};

/**
* Get Metacritic game summary and reviews
* @param {string} seller Full seller name
Expand All @@ -124,10 +126,10 @@ const getSellerId = async (seller) => {
};

module.exports = {
search,
getGameId,
getGameName,
getGameInfo,
getGameData,
getHistoricalLow,
getMetacriticData,
getSellerId
};
Binary file added resources/readme/deals-example.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 783187f

Please sign in to comment.