From 58a5f13ffc13436acc98c1c76a2775a2a4802b21 Mon Sep 17 00:00:00 2001 From: dragonfire535 Date: Sat, 7 Oct 2017 12:26:21 -0400 Subject: [PATCH] Make Steam Command Better --- commands/search/steam.js | 35 +++++++++++++++++++++++++++-------- package.json | 2 +- structures/Util.js | 13 +++++++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/commands/search/steam.js b/commands/search/steam.js index bd430469..d8df360a 100644 --- a/commands/search/steam.js +++ b/commands/search/steam.js @@ -1,6 +1,7 @@ const { Command } = require('discord.js-commando'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); +const { cleanHTML } = require('../../structures/Util'); module.exports = class SteamCommand extends Command { constructor(client) { @@ -23,28 +24,46 @@ module.exports = class SteamCommand extends Command { async run(msg, { query }) { try { - const { body } = await snekfetch + const search = await snekfetch .get('https://store.steampowered.com/api/storesearch') .query({ cc: 'us', l: 'en', term: query }); - if (!body.total) return msg.say('Could not find any results.'); - const data = body.items[0]; - const current = data.price ? data.price.final / 100 : 0.00; - const original = data.price ? data.price.initial / 100 : 0.00; + if (!search.body.total) return msg.say('Could not find any results.'); + const { body } = await snekfetch + .get('http://store.steampowered.com/api/appdetails') + .query({ appids: search.body.items[0].id }); + const { data } = body[search.body.items[0].id.toString()]; + const current = data.price_overview ? data.price_overview.final / 100 : 0; + const original = data.price_overview ? data.price_overview.initial / 100 : 0; const price = current === original ? `$${current}` : `~~$${original}~~ $${current}`; + const platforms = []; + if (data.platforms) { + if (data.platforms.windows) platforms.push('Windows'); + if (data.platforms.mac) platforms.push('Mac'); + if (data.platforms.linux) platforms.push('Linux'); + } const embed = new MessageEmbed() .setColor(0x101D2F) .setAuthor('Steam', 'https://i.imgur.com/xxr2UBZ.png') .setTitle(data.name) - .setURL(`http://store.steampowered.com/app/${data.id}`) - .setImage(data.tiny_image) + .setDescription(cleanHTML(data.short_description)) + .setURL(`http://store.steampowered.com/app/${data.steam_appid}`) + .setImage(data.header_image) .addField('❯ Price', price, true) .addField('❯ Metascore', - data.metascore || 'N/A', true); + data.metacritic ? data.metacritic.score : 'N/A', true) + .addField('❯ Recommendations', + data.recommendations ? data.recommendations.total : 'N/A', true) + .addField('❯ Platforms', + platforms.join(', ') || 'None') + .addField('❯ Developers', + data.developers.join(', ') || 'N/A') + .addField('❯ Publishers', + data.publishers.join(', ') || 'N/A'); return msg.embed(embed); } catch (err) { return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); diff --git a/package.json b/package.json index b0a6ea6b..6ae37966 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "45.0.1", + "version": "45.0.2", "description": "Your personal server companion.", "main": "Shard.js", "scripts": { diff --git a/structures/Util.js b/structures/Util.js index 7c44b859..efee40cb 100644 --- a/structures/Util.js +++ b/structures/Util.js @@ -56,6 +56,19 @@ class Util { format: () => `${hrs < 10 ? `0${hrs}` : hrs}:${min < 10 ? `0${min}` : min}:${sec < 10 ? `0${sec}` : sec}` }; } + + static cleanHTML(text) { + return text + .replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"') + .replace(/–/g, '–') + .replace(/—/g, '—') + .replace(/©/g, '©') + .replace(/™/g, '™') + .replace(/®/g, '®'); + } } module.exports = Util;