From eed39c5ec176208fbde513946d0528b813470497 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Sun, 27 Aug 2017 22:04:58 +0000 Subject: [PATCH] Catch every API --- commands/avatar-edit/3000-years.js | 2 +- commands/avatar-edit/approved.js | 2 +- commands/avatar-edit/beautiful.js | 2 +- commands/avatar-edit/bob-ross.js | 2 +- commands/avatar-edit/card.js | 2 +- commands/avatar-edit/challenger.js | 2 +- commands/avatar-edit/dexter.js | 2 +- commands/avatar-edit/greyscale.js | 2 +- commands/avatar-edit/invert.js | 2 +- commands/avatar-edit/rejected.js | 2 +- commands/avatar-edit/rip.js | 2 +- commands/avatar-edit/simba.js | 2 +- commands/avatar-edit/steam-card.js | 2 +- commands/avatar-edit/thug-life.js | 2 +- commands/avatar-edit/triggered.js | 2 +- commands/avatar-edit/wanted.js | 2 +- commands/games/quiz.js | 58 ++++++------ commands/games/whos-that-pokemon.js | 38 ++++---- commands/image-edit/achievement.js | 20 ++-- commands/image-edit/meme.js | 15 ++- commands/num-edit/currency.js | 18 ++-- commands/num-edit/math.js | 4 +- commands/random-img/cat.js | 10 +- commands/random-img/dog.js | 10 +- commands/random-res/chuck-norris.js | 18 ++-- commands/random-res/fortune.js | 20 ++-- commands/random-res/joke.js | 12 ++- commands/random-res/name.js | 20 ++-- commands/random-res/quote.js | 10 +- commands/random-res/today.js | 2 +- commands/random-res/would-you-rather.js | 20 ++-- commands/random/horoscope.js | 34 ++++--- commands/random/strawpoll.js | 18 ++-- commands/random/xkcd.js | 54 ++++++----- commands/search/anime.js | 2 +- commands/search/bot-info.js | 2 +- commands/search/bulbapedia.js | 42 +++++---- commands/search/define.js | 32 ++++--- commands/search/forecast.js | 116 ++++++++++++------------ commands/search/giphy.js | 22 +++-- commands/search/github.js | 4 +- commands/search/manga.js | 2 +- commands/search/map.js | 22 +++-- commands/search/movie.js | 58 ++++++------ commands/search/neopet.js | 24 +++-- commands/search/npm.js | 2 +- commands/search/osu.js | 78 ++++++++-------- commands/search/pokedex.js | 2 +- commands/search/reddit.js | 4 +- commands/search/soundcloud.js | 52 ++++++----- commands/search/steam.js | 48 +++++----- commands/search/tv-show.js | 66 +++++++------- commands/search/urban.js | 30 +++--- commands/search/vocaloid.js | 56 ++++++------ commands/search/wattpad.js | 60 ++++++------ commands/search/weather.js | 78 ++++++++-------- commands/search/wikipedia.js | 42 +++++---- commands/search/youtube.js | 40 ++++---- commands/search/yu-gi-oh.js | 50 +++++----- commands/text-edit/translate.js | 34 ++++--- commands/text-edit/webhook.js | 12 ++- commands/util/upvote.js | 21 +++-- package.json | 2 +- 63 files changed, 785 insertions(+), 631 deletions(-) diff --git a/commands/avatar-edit/3000-years.js b/commands/avatar-edit/3000-years.js index 9c2180ae..8078a731 100644 --- a/commands/avatar-edit/3000-years.js +++ b/commands/avatar-edit/3000-years.js @@ -43,7 +43,7 @@ module.exports = class YearsCommand extends Command { ctx.drawImage(avatar, 461, 127, 200, 200); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: '3000-years.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/approved.js b/commands/avatar-edit/approved.js index b2bb84ce..a1fb056c 100644 --- a/commands/avatar-edit/approved.js +++ b/commands/avatar-edit/approved.js @@ -43,7 +43,7 @@ module.exports = class ApprovedCommand extends Command { ctx.drawImage(base, 0, 0, 256, 256); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'approved.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/beautiful.js b/commands/avatar-edit/beautiful.js index 40fffbb2..d7f2fa5f 100644 --- a/commands/avatar-edit/beautiful.js +++ b/commands/avatar-edit/beautiful.js @@ -44,7 +44,7 @@ module.exports = class BeautifulCommand extends Command { ctx.drawImage(avatar, 343, 301, 117, 135); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'beautiful.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/bob-ross.js b/commands/avatar-edit/bob-ross.js index a790e42b..844e465f 100644 --- a/commands/avatar-edit/bob-ross.js +++ b/commands/avatar-edit/bob-ross.js @@ -47,7 +47,7 @@ module.exports = class BobRossCommand extends Command { ctx.drawImage(base, 0, 0); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'bob-ross.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/card.js b/commands/avatar-edit/card.js index 475d412e..b7d56a07 100644 --- a/commands/avatar-edit/card.js +++ b/commands/avatar-edit/card.js @@ -67,7 +67,7 @@ module.exports = class CardCommand extends Command { ctx.fillText(`#${member.user.discriminator}`, 313, 355); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'card.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/challenger.js b/commands/avatar-edit/challenger.js index b7653a7b..864280fc 100644 --- a/commands/avatar-edit/challenger.js +++ b/commands/avatar-edit/challenger.js @@ -42,7 +42,7 @@ module.exports = class ChallengerCommand extends Command { ctx.drawImage(avatar, 484, 98, 256, 256); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'challenger.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/dexter.js b/commands/avatar-edit/dexter.js index 06c93ff2..71c4a5e9 100644 --- a/commands/avatar-edit/dexter.js +++ b/commands/avatar-edit/dexter.js @@ -44,7 +44,7 @@ module.exports = class DexterCommand extends Command { ctx.rotate(11 * Math.PI / 180); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'dexter.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/greyscale.js b/commands/avatar-edit/greyscale.js index 21c53166..e5b93df1 100644 --- a/commands/avatar-edit/greyscale.js +++ b/commands/avatar-edit/greyscale.js @@ -49,7 +49,7 @@ module.exports = class GreyscaleCommand extends Command { ctx.putImageData(imgData, 0, 0); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'greyscale.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/invert.js b/commands/avatar-edit/invert.js index 9c7827ae..3066df6e 100644 --- a/commands/avatar-edit/invert.js +++ b/commands/avatar-edit/invert.js @@ -47,7 +47,7 @@ module.exports = class InvertCommand extends Command { ctx.putImageData(imgData, 0, 0); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'invert.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/rejected.js b/commands/avatar-edit/rejected.js index 2391ca06..356852a4 100644 --- a/commands/avatar-edit/rejected.js +++ b/commands/avatar-edit/rejected.js @@ -43,7 +43,7 @@ module.exports = class RejctedCommand extends Command { ctx.drawImage(base, 0, 0, 256, 256); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'rejected.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/rip.js b/commands/avatar-edit/rip.js index cea418a8..13c3f757 100644 --- a/commands/avatar-edit/rip.js +++ b/commands/avatar-edit/rip.js @@ -52,7 +52,7 @@ module.exports = class RIPCommand extends Command { ctx.putImageData(imgData, 158, 51); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'rip.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/simba.js b/commands/avatar-edit/simba.js index 6a4144cd..2cffc399 100644 --- a/commands/avatar-edit/simba.js +++ b/commands/avatar-edit/simba.js @@ -44,7 +44,7 @@ module.exports = class SimbaCommand extends Command { ctx.rotate(24 * Math.PI / 180); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'simba.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/steam-card.js b/commands/avatar-edit/steam-card.js index 1c064073..37c65591 100644 --- a/commands/avatar-edit/steam-card.js +++ b/commands/avatar-edit/steam-card.js @@ -48,7 +48,7 @@ module.exports = class SteamCardCommand extends Command { ctx.fillText(member.displayName, 35, 48); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'steam-card.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/thug-life.js b/commands/avatar-edit/thug-life.js index 4c40ca1c..0dfa04ae 100644 --- a/commands/avatar-edit/thug-life.js +++ b/commands/avatar-edit/thug-life.js @@ -51,7 +51,7 @@ module.exports = class ThugLifeCommand extends Command { ctx.putImageData(imgData, 0, 0); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'thug-life.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/triggered.js b/commands/avatar-edit/triggered.js index 937cf3bf..f87529cd 100644 --- a/commands/avatar-edit/triggered.js +++ b/commands/avatar-edit/triggered.js @@ -48,7 +48,7 @@ module.exports = class TriggeredCommand extends Command { ctx.drawImage(base, 0, 0); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'triggered.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/avatar-edit/wanted.js b/commands/avatar-edit/wanted.js index f59e31f5..9e1dc870 100644 --- a/commands/avatar-edit/wanted.js +++ b/commands/avatar-edit/wanted.js @@ -42,7 +42,7 @@ module.exports = class WantedCommand extends Command { ctx.drawImage(avatar, 150, 360, 430, 430); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'wanted.png' }] }); } catch (err) { - return msg.say(`Oh no, the image generation failed: \`${err.message}\`. Try again later!`); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/games/quiz.js b/commands/games/quiz.js index 1408858d..3b9c9d2e 100644 --- a/commands/games/quiz.js +++ b/commands/games/quiz.js @@ -43,33 +43,37 @@ module.exports = class QuizCommand extends Command { async run(msg, args) { const { type, difficulty } = args; - const { body } = await snekfetch - .get('https://opentdb.com/api.php') - .query({ - amount: 1, - type, - encode: 'url3986', - difficulty + try { + const { body } = await snekfetch + .get('https://opentdb.com/api.php') + .query({ + amount: 1, + type, + encode: 'url3986', + difficulty + }); + if (!body.results) return msg.say('Oh no, a question could not be fetched. Try again later!'); + const answers = body.results[0].incorrect_answers.map(answer => decodeURIComponent(answer.toLowerCase())); + const correct = decodeURIComponent(body.results[0].correct_answer.toLowerCase()); + answers.push(correct); + const embed = new MessageEmbed() + .setTitle('You have 15 seconds to answer this question:') + .setColor(0x9797FF) + .setDescription(stripIndents` + **${decodeURIComponent(body.results[0].category)}** + ${type === 'boolean' ? '**True or False:** ' : ''}${decodeURIComponent(body.results[0].question)} + ${type === 'multiple' ? `**Choices:** ${list(shuffle(answers), 'or')}` : ''} + `); + await msg.embed(embed); + const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, { + max: 1, + time: 15000 }); - if (!body.results) return msg.say('Oh no, a question could not be fetched. Try again later!'); - const answers = body.results[0].incorrect_answers.map(answer => decodeURIComponent(answer.toLowerCase())); - const correct = decodeURIComponent(body.results[0].correct_answer.toLowerCase()); - answers.push(correct); - const embed = new MessageEmbed() - .setTitle('You have 15 seconds to answer this question:') - .setColor(0x9797FF) - .setDescription(stripIndents` - **${decodeURIComponent(body.results[0].category)}** - ${type === 'boolean' ? '**True or False:** ' : ''}${decodeURIComponent(body.results[0].question)} - ${type === 'multiple' ? `**Choices:** ${list(shuffle(answers), 'or')}` : ''} - `); - await msg.embed(embed); - const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, { - max: 1, - time: 15000 - }); - if (!msgs.size) return msg.say(`Time! It was ${correct}, sorry!`); - if (msgs.first().content.toLowerCase() !== correct) return msg.say(`Nope, sorry, it's ${correct}.`); - return msg.say('Nice job! 10/10! You deserve some cake!'); + if (!msgs.size) return msg.say(`Time! It was ${correct}, sorry!`); + if (msgs.first().content.toLowerCase() !== correct) return msg.say(`Nope, sorry, it's ${correct}.`); + return msg.say('Nice job! 10/10! You deserve some cake!'); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/games/whos-that-pokemon.js b/commands/games/whos-that-pokemon.js index c0419987..8cf8ddb2 100644 --- a/commands/games/whos-that-pokemon.js +++ b/commands/games/whos-that-pokemon.js @@ -17,22 +17,26 @@ module.exports = class WhosThatPokemonCommand extends Command { async run(msg) { const pokemon = Math.floor(Math.random() * 721) + 1; - const { body } = await snekfetch - .get(`https://pokeapi.co/api/v2/pokemon-species/${pokemon}`); - const names = body.names.map(name => name.name.toLowerCase()); - const name = filterPkmn(body.names).name; - const id = `${'000'.slice(body.id.toString().length)}${body.id}`; - const embed = new MessageEmbed() - .setTitle('You have 15 seconds, who\'s that Pokémon?') - .setColor(0xED1C24) - .setImage(`https://www.serebii.net/sunmoon/pokemon/${id}.png`); - await msg.embed(embed); - const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, { - max: 1, - time: 15000 - }); - if (!msgs.size) return msg.say(`Time! It was ${name}, sorry!`); - if (!names.includes(msgs.first().content.toLowerCase())) return msg.say(`Nope, sorry, it's ${name}.`); - return msg.say('Nice job! 10/10! You deserve some cake!'); + try { + const { body } = await snekfetch + .get(`https://pokeapi.co/api/v2/pokemon-species/${pokemon}`); + const names = body.names.map(name => name.name.toLowerCase()); + const name = filterPkmn(body.names).name; + const id = `${'000'.slice(body.id.toString().length)}${body.id}`; + const embed = new MessageEmbed() + .setTitle('You have 15 seconds, who\'s that Pokémon?') + .setColor(0xED1C24) + .setImage(`https://www.serebii.net/sunmoon/pokemon/${id}.png`); + await msg.embed(embed); + const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, { + max: 1, + time: 15000 + }); + if (!msgs.size) return msg.say(`Time! It was ${name}, sorry!`); + if (!names.includes(msgs.first().content.toLowerCase())) return msg.say(`Nope, sorry, it's ${name}.`); + return msg.say('Nice job! 10/10! You deserve some cake!'); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/image-edit/achievement.js b/commands/image-edit/achievement.js index a07a45ae..4b4b92a9 100644 --- a/commands/image-edit/achievement.js +++ b/commands/image-edit/achievement.js @@ -25,14 +25,18 @@ module.exports = class AchievementCommand extends Command { async run(msg, args) { const { text } = args; - const { body } = await snekfetch - .get('https://www.minecraftskinstealer.com/achievement/a.php') - .query({ - i: 1, - h: 'Achievement Get!', - t: text - }); - return msg.say({ files: [{ attachment: body, name: 'achievement.png' }] }); + try { + const { body } = await snekfetch + .get('https://www.minecraftskinstealer.com/achievement/a.php') + .query({ + i: 1, + h: 'Achievement Get!', + t: text + }); + return msg.say({ files: [{ attachment: body, name: 'achievement.png' }] }); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/image-edit/meme.js b/commands/image-edit/meme.js index 0908755e..1bb61bca 100644 --- a/commands/image-edit/meme.js +++ b/commands/image-edit/meme.js @@ -1,4 +1,5 @@ const Command = require('../../structures/Command'); +const snekfetch = require('snekfetch'); const { list } = require('../../structures/Util'); const codes = require('../../assets/json/meme'); @@ -30,7 +31,7 @@ module.exports = class MemeCommand extends Command { if (top.length < 200) return true; return 'Please keep the top text under 200 characters.'; }, - parse: top => encodeURIComponent(top.replace(/ /g, '-')) + parse: top => encodeURIComponent(top) }, { key: 'bottom', @@ -40,14 +41,20 @@ module.exports = class MemeCommand extends Command { if (bottom.length < 200) return true; return 'Please keep the bottom text under 200 characters.'; }, - parse: bottom => encodeURIComponent(bottom.replace(/ /g, '-')) + parse: bottom => encodeURIComponent(bottom) } ] }); } - run(msg, args) { + async run(msg, args) { const { type, top, bottom } = args; - return msg.say({ files: [`https://memegen.link/${type}/${top}/${bottom}.jpg`] }); + try { + const { body } = await snekfetch + .get(`https://memegen.link/api/templates/${type}/${top}/${bottom}`); + return msg.say({ files: [body.direct.visible] }); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/num-edit/currency.js b/commands/num-edit/currency.js index 448e8bbc..91515079 100644 --- a/commands/num-edit/currency.js +++ b/commands/num-edit/currency.js @@ -44,12 +44,16 @@ module.exports = class CurrencyCommand extends Command { async run(msg, args) { const { base, target, amount } = args; if (base === target) return msg.say(`Converting ${base} to ${target} is the same value, dummy.`); - const { body } = await snekfetch - .get('http://api.fixer.io/latest') - .query({ - base, - symbols: target - }); - return msg.say(`${amount} ${base} is ${amount * body.rates[target]} ${target}.`); + try { + const { body } = await snekfetch + .get('http://api.fixer.io/latest') + .query({ + base, + symbols: target + }); + return msg.say(`${amount} ${base} is ${amount * body.rates[target]} ${target}.`); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/num-edit/math.js b/commands/num-edit/math.js index d6ff5590..be0678c9 100644 --- a/commands/num-edit/math.js +++ b/commands/num-edit/math.js @@ -22,9 +22,9 @@ module.exports = class MathCommand extends Command { const { expression } = args; try { const solved = math.eval(expression).toString(); - return msg.say(solved).catch(() => msg.say('Invalid Statement')); + return msg.say(solved).catch(() => msg.say('Invalid statement.')); } catch (err) { - return msg.say('Invalid Statement'); + return msg.say('Invalid statement.'); } } }; diff --git a/commands/random-img/cat.js b/commands/random-img/cat.js index 9361d6c8..d81a4deb 100644 --- a/commands/random-img/cat.js +++ b/commands/random-img/cat.js @@ -13,8 +13,12 @@ module.exports = class CatCommand extends Command { } async run(msg) { - const { body } = await snekfetch - .get('http://random.cat/meow'); - return msg.say(body.file); + try { + const { body } = await snekfetch + .get('http://random.cat/meow'); + return msg.say(body.file); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/random-img/dog.js b/commands/random-img/dog.js index 7ae5abf1..9fcb82ac 100644 --- a/commands/random-img/dog.js +++ b/commands/random-img/dog.js @@ -12,8 +12,12 @@ module.exports = class DogCommand extends Command { } async run(msg) { - const { body } = await snekfetch - .get('https://random.dog/woof.json'); - return msg.say(body.url); + try { + const { body } = await snekfetch + .get('https://random.dog/woof.json'); + return msg.say(body.url); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/random-res/chuck-norris.js b/commands/random-res/chuck-norris.js index 6acb6834..99cdfef8 100644 --- a/commands/random-res/chuck-norris.js +++ b/commands/random-res/chuck-norris.js @@ -22,12 +22,16 @@ module.exports = class ChuckNorrisCommand extends Command { async run(msg, args) { const { name } = args; - const { body } = await snekfetch - .get('http://api.icndb.com/jokes/random') - .query({ - escape: 'javascript', - firstName: name - }); - return msg.say(body.value.joke); + try { + const { body } = await snekfetch + .get('http://api.icndb.com/jokes/random') + .query({ + escape: 'javascript', + firstName: name + }); + return msg.say(body.value.joke); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/random-res/fortune.js b/commands/random-res/fortune.js index b060a1d8..cff2bc12 100644 --- a/commands/random-res/fortune.js +++ b/commands/random-res/fortune.js @@ -14,13 +14,17 @@ module.exports = class FortuneCommand extends Command { } async run(msg) { - const { body } = await snekfetch - .get('http://fortunecookieapi.herokuapp.com/v1/cookie') - .query({ limit: 1 }); - return msg.say(stripIndents` - ${body[0].fortune.message} - ${body[0].lotto.numbers.join(', ')} - ${body[0].lesson.chinese} (${body[0].lesson.pronunciation}): ${body[0].lesson.english} - `); + try { + const { body } = await snekfetch + .get('http://fortunecookieapi.herokuapp.com/v1/cookie') + .query({ limit: 1 }); + return msg.say(stripIndents` + ${body[0].fortune.message} + ${body[0].lotto.numbers.join(', ')} + ${body[0].lesson.chinese} (${body[0].lesson.pronunciation}): ${body[0].lesson.english} + `); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/random-res/joke.js b/commands/random-res/joke.js index 5ccd028d..9517a246 100644 --- a/commands/random-res/joke.js +++ b/commands/random-res/joke.js @@ -12,9 +12,13 @@ module.exports = class JokeCommand extends Command { } async run(msg) { - const { body } = await snekfetch - .get('https://icanhazdadjoke.com/') - .set({ Accept: 'application/json' }); - return msg.say(body.joke); + try { + const { body } = await snekfetch + .get('https://icanhazdadjoke.com/') + .set({ Accept: 'application/json' }); + return msg.say(body.joke); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/random-res/name.js b/commands/random-res/name.js index 6b7c231c..c16083e5 100644 --- a/commands/random-res/name.js +++ b/commands/random-res/name.js @@ -28,13 +28,17 @@ module.exports = class NameCommand extends Command { async run(msg, args) { const { gender } = args; - const { body } = await snekfetch - .get('http://namey.muffinlabs.com/name.json') - .query({ - with_surname: true, - type: gender, - frequency: 'all' - }); - return msg.say(body[0]); + try { + const { body } = await snekfetch + .get('http://namey.muffinlabs.com/name.json') + .query({ + with_surname: true, + type: gender, + frequency: 'all' + }); + return msg.say(body[0]); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/random-res/quote.js b/commands/random-res/quote.js index ae4484cb..b8de8cc1 100644 --- a/commands/random-res/quote.js +++ b/commands/random-res/quote.js @@ -12,8 +12,12 @@ module.exports = class QuoteCommand extends Command { } async run(msg) { - const { body } = await snekfetch - .get('https://talaikis.com/api/quotes/random/'); - return msg.say(`${body.quote} - _${body.author}_`); + try { + const { body } = await snekfetch + .get('https://talaikis.com/api/quotes/random/'); + return msg.say(`${body.quote} - _${body.author}_`); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/random-res/today.js b/commands/random-res/today.js index 95b190cf..f77b05dc 100644 --- a/commands/random-res/today.js +++ b/commands/random-res/today.js @@ -39,7 +39,7 @@ module.exports = class TodayCommand extends Command { return msg.embed(embed); } catch (err) { if (err.status === 404 || err.status === 500) return msg.say('Invalid date.'); - throw err; + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/random-res/would-you-rather.js b/commands/random-res/would-you-rather.js index 072bcc08..25a3a83b 100644 --- a/commands/random-res/would-you-rather.js +++ b/commands/random-res/would-you-rather.js @@ -15,13 +15,17 @@ module.exports = class WouldYouRatherCommand extends Command { } async run(msg) { - const { body } = await snekfetch - .get('http://www.rrrather.com/botapi'); - const embed = new MessageEmbed() - .setTitle(`${body.title}...`) - .setURL(body.link) - .setColor(0x9797FF) - .setDescription(`${body.choicea} OR ${body.choiceb}?`); - return msg.embed(embed); + try { + const { body } = await snekfetch + .get('http://www.rrrather.com/botapi'); + const embed = new MessageEmbed() + .setTitle(`${body.title}...`) + .setURL(body.link) + .setColor(0x9797FF) + .setDescription(`${body.choicea} OR ${body.choiceb}?`); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/random/horoscope.js b/commands/random/horoscope.js index e0102668..db29093a 100644 --- a/commands/random/horoscope.js +++ b/commands/random/horoscope.js @@ -30,20 +30,24 @@ module.exports = class HoroscopeCommand extends Command { async run(msg, args) { const { sign } = args; - const { text } = await snekfetch - .get(`http://sandipbgt.com/theastrologer/api/horoscope/${sign}/today`); - const body = JSON.parse(text); - const embed = new MessageEmbed() - .setColor(0x9797FF) - .setTitle(`Horoscope for ${body.sunsign}...`) - .setTimestamp() - .setDescription(body.horoscope) - .addField('❯ Mood', - body.meta.mood, true) - .addField('❯ Intensity', - body.meta.intensity, true) - .addField('❯ Date', - body.date, true); - return msg.embed(embed); + try { + const { text } = await snekfetch + .get(`http://sandipbgt.com/theastrologer/api/horoscope/${sign}/today`); + const body = JSON.parse(text); + const embed = new MessageEmbed() + .setColor(0x9797FF) + .setTitle(`Horoscope for ${body.sunsign}...`) + .setTimestamp() + .setDescription(body.horoscope) + .addField('❯ Mood', + body.meta.mood, true) + .addField('❯ Intensity', + body.meta.intensity, true) + .addField('❯ Date', + body.date, true); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/random/strawpoll.js b/commands/random/strawpoll.js index c4fe3483..eb37af31 100644 --- a/commands/random/strawpoll.js +++ b/commands/random/strawpoll.js @@ -38,12 +38,16 @@ module.exports = class StrawpollCommand extends Command { const { title, options } = args; if (options.length < 2) return msg.say('Please provide more than one choice.'); if (options.length > 31) return msg.say('Please provide thirty or less choices.'); - const { body } = await snekfetch - .post('https://strawpoll.me/api/v2/polls') - .send({ title, options }); - return msg.say(stripIndents` - ${body.title} - http://www.strawpoll.me/${body.id} - `); + try { + const { body } = await snekfetch + .post('https://strawpoll.me/api/v2/polls') + .send({ title, options }); + return msg.say(stripIndents` + ${body.title} + http://www.strawpoll.me/${body.id} + `); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/random/xkcd.js b/commands/random/xkcd.js index 5e575434..8d28d01e 100644 --- a/commands/random/xkcd.js +++ b/commands/random/xkcd.js @@ -25,21 +25,34 @@ module.exports = class XKCDCommand extends Command { async run(msg, args) { const { type } = args; - const current = await snekfetch - .get('https://xkcd.com/info.0.json'); - if (type === 'today') { - const embed = new MessageEmbed() - .setTitle(`${current.body.num} - ${current.body.title}`) - .setColor(0x9797FF) - .setURL(`https://xkcd.com/${current.body.num}`) - .setImage(current.body.img) - .setFooter(current.body.alt); - return msg.embed(embed); - } - if (type === 'random') { - const random = Math.floor(Math.random() * current.body.num) + 1; + try { + const current = await snekfetch + .get('https://xkcd.com/info.0.json'); + if (type === 'today') { + const embed = new MessageEmbed() + .setTitle(`${current.body.num} - ${current.body.title}`) + .setColor(0x9797FF) + .setURL(`https://xkcd.com/${current.body.num}`) + .setImage(current.body.img) + .setFooter(current.body.alt); + return msg.embed(embed); + } + if (type === 'random') { + const random = Math.floor(Math.random() * current.body.num) + 1; + const { body } = await snekfetch + .get(`https://xkcd.com/${random}/info.0.json`); + const embed = new MessageEmbed() + .setTitle(`${body.num} - ${body.title}`) + .setColor(0x9797FF) + .setURL(`https://xkcd.com/${body.num}`) + .setImage(body.img) + .setFooter(body.alt); + return msg.embed(embed); + } + const choice = parseInt(type, 10); + if (isNaN(choice) || current.body.num < choice || choice < 1) return msg.say('Invalid number.'); const { body } = await snekfetch - .get(`https://xkcd.com/${random}/info.0.json`); + .get(`https://xkcd.com/${choice}/info.0.json`); const embed = new MessageEmbed() .setTitle(`${body.num} - ${body.title}`) .setColor(0x9797FF) @@ -47,17 +60,8 @@ module.exports = class XKCDCommand extends Command { .setImage(body.img) .setFooter(body.alt); return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } - const choice = parseInt(type, 10); - if (isNaN(choice) || current.body.num < choice || choice < 1) return msg.say('Invalid number.'); - const { body } = await snekfetch - .get(`https://xkcd.com/${choice}/info.0.json`); - const embed = new MessageEmbed() - .setTitle(`${body.num} - ${body.title}`) - .setColor(0x9797FF) - .setURL(`https://xkcd.com/${body.num}`) - .setImage(body.img) - .setFooter(body.alt); - return msg.embed(embed); } }; diff --git a/commands/search/anime.js b/commands/search/anime.js index 7fde0710..3f8a72b9 100644 --- a/commands/search/anime.js +++ b/commands/search/anime.js @@ -50,7 +50,7 @@ module.exports = class AnimeCommand extends Command { return msg.embed(embed); } catch (err) { if (err.message === 'Parse Error') return msg.say('Could not find any results.'); - throw err; + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/search/bot-info.js b/commands/search/bot-info.js index f1e20895..afa33963 100644 --- a/commands/search/bot-info.js +++ b/commands/search/bot-info.js @@ -42,7 +42,7 @@ module.exports = class BotSearchCommand extends Command { return msg.embed(embed); } catch (err) { if (err.status === 404) return msg.say('Could not find any results.'); - throw err; + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/search/bulbapedia.js b/commands/search/bulbapedia.js index 1cbe7c14..710f8ddd 100644 --- a/commands/search/bulbapedia.js +++ b/commands/search/bulbapedia.js @@ -24,24 +24,28 @@ module.exports = class BulbapediaCommand extends Command { async run(msg, args) { const { query } = args; - const { body } = await snekfetch - .get('http://bulbapedia.bulbagarden.net/w/api.php') - .query({ - action: 'query', - prop: 'extracts', - format: 'json', - titles: query, - exintro: '', - explaintext: '', - redirects: '', - formatversion: 2 - }); - if (body.query.pages[0].missing) return msg.say('Could not find any results.'); - const embed = new MessageEmbed() - .setColor(0x3E7614) - .setTitle(body.query.pages[0].title) - .setAuthor('Bulbapedia', 'https://i.imgur.com/09eYo5T.png') - .setDescription(shorten(body.query.pages[0].extract.replace(/\n/g, '\n\n'))); - return msg.embed(embed); + try { + const { body } = await snekfetch + .get('http://bulbapedia.bulbagarden.net/w/api.php') + .query({ + action: 'query', + prop: 'extracts', + format: 'json', + titles: query, + exintro: '', + explaintext: '', + redirects: '', + formatversion: 2 + }); + if (body.query.pages[0].missing) return msg.say('Could not find any results.'); + const embed = new MessageEmbed() + .setColor(0x3E7614) + .setTitle(body.query.pages[0].title) + .setAuthor('Bulbapedia', 'https://i.imgur.com/09eYo5T.png') + .setDescription(shorten(body.query.pages[0].extract.replace(/\n/g, '\n\n'))); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/define.js b/commands/search/define.js index 89cd215b..41f820bf 100644 --- a/commands/search/define.js +++ b/commands/search/define.js @@ -24,19 +24,23 @@ module.exports = class DefineCommand extends Command { async run(msg, args) { const { query } = args; - const { body } = await snekfetch - .get(`http://api.wordnik.com:80/v4/word.json/${query}/definitions`) - .query({ - limit: 1, - includeRelated: false, - useCanonical: false, - api_key: WORDNIK_KEY - }); - if (!body.length) return msg.say('Could not find any results.'); - const embed = new MessageEmbed() - .setColor(0x9797FF) - .setTitle(body[0].word) - .setDescription(body[0].text); - return msg.embed(embed); + try { + const { body } = await snekfetch + .get(`http://api.wordnik.com:80/v4/word.json/${query}/definitions`) + .query({ + limit: 1, + includeRelated: false, + useCanonical: false, + api_key: WORDNIK_KEY + }); + if (!body.length) return msg.say('Could not find any results.'); + const embed = new MessageEmbed() + .setColor(0x9797FF) + .setTitle(body[0].word) + .setDescription(body[0].text); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/forecast.js b/commands/search/forecast.js index 0e83619e..a8d75788 100644 --- a/commands/search/forecast.js +++ b/commands/search/forecast.js @@ -23,61 +23,65 @@ module.exports = class ForecastCommand extends Command { async run(msg, args) { const { query } = args; - const { body } = await snekfetch - .get('https://query.yahooapis.com/v1/public/yql') - .query({ - q: `select * from weather.forecast where u='f' AND woeid in (select woeid from geo.places(1) where text="${query}")`, // eslint-disable-line max-len - format: 'json' - }); - if (!body.query.count) return msg.say('Could not find any results.'); - const forecasts = body.query.results.channel.item.forecast; - const embed = new MessageEmbed() - .setColor(0x0000FF) - .setAuthor(body.query.results.channel.title, 'https://i.imgur.com/2MT0ViC.png') - .setURL(body.query.results.channel.link) - .setTimestamp() - .addField(`❯ ${forecasts[0].day} - ${forecasts[0].date}`, - stripIndents` - **High:** ${forecasts[0].high}°F - **Low:** ${forecasts[0].low}°F - **Condition:** ${forecasts[0].text} - `) - .addField(`❯ ${forecasts[1].day} - ${forecasts[1].date}`, - stripIndents` - **High:** ${forecasts[1].high}°F - **Low:** ${forecasts[1].low}°F - **Condition:** ${forecasts[1].text} - `) - .addField(`❯ ${forecasts[2].day} - ${forecasts[2].date}`, - stripIndents` - **High:** ${forecasts[2].high}°F - **Low:** ${forecasts[2].low}°F - **Condition:** ${forecasts[2].text} - `) - .addField(`❯ ${forecasts[3].day} - ${forecasts[3].date}`, - stripIndents` - **High:** ${forecasts[3].high}°F - **Low:** ${forecasts[3].low}°F - **Condition:** ${forecasts[3].text} - `) - .addField(`❯ ${forecasts[4].day} - ${forecasts[4].date}`, - stripIndents` - **High:** ${forecasts[4].high}°F - **Low:** ${forecasts[4].low}°F - **Condition:** ${forecasts[4].text} - `) - .addField(`❯ ${forecasts[5].day} - ${forecasts[5].date}`, - stripIndents` - **High:** ${forecasts[5].high}°F - **Low:** ${forecasts[5].low}°F - **Condition:** ${forecasts[5].text} - `) - .addField(`❯ ${forecasts[6].day} - ${forecasts[6].date}`, - stripIndents` - **High:** ${forecasts[6].high}°F - **Low:** ${forecasts[6].low}°F - **Condition:** ${forecasts[6].text} - `); - return msg.embed(embed); + try { + const { body } = await snekfetch + .get('https://query.yahooapis.com/v1/public/yql') + .query({ + q: `select * from weather.forecast where u='f' AND woeid in (select woeid from geo.places(1) where text="${query}")`, // eslint-disable-line max-len + format: 'json' + }); + if (!body.query.count) return msg.say('Could not find any results.'); + const forecasts = body.query.results.channel.item.forecast; + const embed = new MessageEmbed() + .setColor(0x0000FF) + .setAuthor(body.query.results.channel.title, 'https://i.imgur.com/2MT0ViC.png') + .setURL(body.query.results.channel.link) + .setTimestamp() + .addField(`❯ ${forecasts[0].day} - ${forecasts[0].date}`, + stripIndents` + **High:** ${forecasts[0].high}°F + **Low:** ${forecasts[0].low}°F + **Condition:** ${forecasts[0].text} + `) + .addField(`❯ ${forecasts[1].day} - ${forecasts[1].date}`, + stripIndents` + **High:** ${forecasts[1].high}°F + **Low:** ${forecasts[1].low}°F + **Condition:** ${forecasts[1].text} + `) + .addField(`❯ ${forecasts[2].day} - ${forecasts[2].date}`, + stripIndents` + **High:** ${forecasts[2].high}°F + **Low:** ${forecasts[2].low}°F + **Condition:** ${forecasts[2].text} + `) + .addField(`❯ ${forecasts[3].day} - ${forecasts[3].date}`, + stripIndents` + **High:** ${forecasts[3].high}°F + **Low:** ${forecasts[3].low}°F + **Condition:** ${forecasts[3].text} + `) + .addField(`❯ ${forecasts[4].day} - ${forecasts[4].date}`, + stripIndents` + **High:** ${forecasts[4].high}°F + **Low:** ${forecasts[4].low}°F + **Condition:** ${forecasts[4].text} + `) + .addField(`❯ ${forecasts[5].day} - ${forecasts[5].date}`, + stripIndents` + **High:** ${forecasts[5].high}°F + **Low:** ${forecasts[5].low}°F + **Condition:** ${forecasts[5].text} + `) + .addField(`❯ ${forecasts[6].day} - ${forecasts[6].date}`, + stripIndents` + **High:** ${forecasts[6].high}°F + **Low:** ${forecasts[6].low}°F + **Condition:** ${forecasts[6].text} + `); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/giphy.js b/commands/search/giphy.js index c793fbc2..b264bdcb 100644 --- a/commands/search/giphy.js +++ b/commands/search/giphy.js @@ -21,14 +21,18 @@ module.exports = class GiphyCommand extends Command { async run(msg, args) { const { query } = args; - const { body } = await snekfetch - .get('http://api.giphy.com/v1/gifs/search') - .query({ - q: query, - api_key: GIPHY_KEY, - rating: msg.channel.nsfw ? 'r' : 'pg' - }); - if (!body.data.length) return msg.say('No Results.'); - return msg.say(body.data[Math.floor(Math.random() * body.data.length)].images.original.url); + try { + const { body } = await snekfetch + .get('http://api.giphy.com/v1/gifs/search') + .query({ + q: query, + api_key: GIPHY_KEY, + rating: msg.channel.nsfw ? 'r' : 'pg' + }); + if (!body.data.length) return msg.say('Could not find any results.'); + return msg.say(body.data[Math.floor(Math.random() * body.data.length)].images.original.url); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/github.js b/commands/search/github.js index f347b27b..4889aef1 100644 --- a/commands/search/github.js +++ b/commands/search/github.js @@ -53,8 +53,8 @@ module.exports = class GitHubCommand extends Command { moment(body.updated_at).format('MMMM Do YYYY'), true); return msg.embed(embed); } catch (err) { - if (err.status === 404) return msg.say('Could not find the repository.'); - throw err; + if (err.status === 404) return msg.say('Could not find any results.'); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/search/manga.js b/commands/search/manga.js index 3b425fa3..72856e9b 100644 --- a/commands/search/manga.js +++ b/commands/search/manga.js @@ -50,7 +50,7 @@ module.exports = class MangaCommand extends Command { return msg.embed(embed); } catch (err) { if (err.message === 'Parse Error') return msg.say('Could not find any results.'); - throw err; + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/search/map.js b/commands/search/map.js index 159de62b..a5755962 100644 --- a/commands/search/map.js +++ b/commands/search/map.js @@ -32,14 +32,18 @@ module.exports = class MapCommand extends Command { async run(msg, args) { const { zoom, query } = args; - const { body } = await snekfetch - .get('https://maps.googleapis.com/maps/api/staticmap') - .query({ - center: query, - zoom, - size: '500x500', - key: GOOGLE_KEY - }); - return msg.say({ files: [{ attachment: body, name: 'map.png' }] }); + try { + const { body } = await snekfetch + .get('https://maps.googleapis.com/maps/api/staticmap') + .query({ + center: query, + zoom, + size: '500x500', + key: GOOGLE_KEY + }); + return msg.say({ files: [{ attachment: body, name: 'map.png' }] }); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/movie.js b/commands/search/movie.js index 2140687b..b06c0af4 100644 --- a/commands/search/movie.js +++ b/commands/search/movie.js @@ -25,32 +25,36 @@ module.exports = class MovieCommand extends Command { async run(msg, args) { const { query } = args; - const search = await snekfetch - .get('http://api.themoviedb.org/3/search/movie') - .query({ - api_key: TMDB_KEY, - include_adult: msg.channel.nsfw || false, - query - }); - if (!search.body.results.length) return msg.say('Could not find any results.'); - const { body } = await snekfetch - .get(`https://api.themoviedb.org/3/movie/${search.body.results[0].id}`) - .query({ api_key: TMDB_KEY }); - const embed = new MessageEmbed() - .setColor(0x00D474) - .setTitle(body.title) - .setURL(`https://www.themoviedb.org/movie/${body.id}`) - .setAuthor('TMDB', 'https://i.imgur.com/G9q4DF1.png') - .setDescription(body.overview ? shorten(body.overview) : 'No description available.') - .setThumbnail(body.poster_path ? `https://image.tmdb.org/t/p/w500${body.poster_path}` : null) - .addField('❯ Runtime', - body.runtime ? `${body.runtime} mins.` : 'N/A', true) - .addField('❯ Release Date', - body.release_date || 'N/A', true) - .addField('❯ Genres', - body.genres.length ? body.genres.map(genre => genre.name).join(', ') : 'N/A') - .addField('❯ Production Companies', - body.production_companies.length ? body.production_companies.map(company => company.name).join(', ') : 'N/A'); - return msg.embed(embed); + try { + const search = await snekfetch + .get('http://api.themoviedb.org/3/search/movie') + .query({ + api_key: TMDB_KEY, + include_adult: msg.channel.nsfw || false, + query + }); + if (!search.body.results.length) return msg.say('Could not find any results.'); + const { body } = await snekfetch + .get(`https://api.themoviedb.org/3/movie/${search.body.results[0].id}`) + .query({ api_key: TMDB_KEY }); + const embed = new MessageEmbed() + .setColor(0x00D474) + .setTitle(body.title) + .setURL(`https://www.themoviedb.org/movie/${body.id}`) + .setAuthor('TMDB', 'https://i.imgur.com/G9q4DF1.png') + .setDescription(body.overview ? shorten(body.overview) : 'No description available.') + .setThumbnail(body.poster_path ? `https://image.tmdb.org/t/p/w500${body.poster_path}` : null) + .addField('❯ Runtime', + body.runtime ? `${body.runtime} mins.` : 'N/A', true) + .addField('❯ Release Date', + body.release_date || 'N/A', true) + .addField('❯ Genres', + body.genres.length ? body.genres.map(genre => genre.name).join(', ') : 'N/A') + .addField('❯ Production Companies', + body.production_companies.length ? body.production_companies.map(c => c.name).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/commands/search/neopet.js b/commands/search/neopet.js index 1b69c7b6..d218355b 100644 --- a/commands/search/neopet.js +++ b/commands/search/neopet.js @@ -20,15 +20,19 @@ module.exports = class NeopetCommand extends Command { async run(msg, args) { const { query } = args; - const { text } = await snekfetch - .get('http://www.sunnyneo.com/petimagefinder.php') - .query({ - name: query, - size: 5, - mood: 1 - }); - const link = text.match(/http:\/\/pets\.neopets\.com\/cp\/.+\.png/); - if (!link) return msg.say('Could not find any results.'); - return msg.say(link[0]); + try { + const { text } = await snekfetch + .get('http://www.sunnyneo.com/petimagefinder.php') + .query({ + name: query, + size: 5, + mood: 1 + }); + const link = text.match(/http:\/\/pets\.neopets\.com\/cp\/.+\.png/); + if (!link) return msg.say('Could not find any results.'); + return msg.say(link[0]); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/npm.js b/commands/search/npm.js index aa687e6d..30d7e0d2 100644 --- a/commands/search/npm.js +++ b/commands/search/npm.js @@ -52,7 +52,7 @@ module.exports = class NPMCommand extends Command { return msg.embed(embed); } catch (err) { if (err.status === 404) return msg.say('Could not find any results.'); - throw err; + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/search/osu.js b/commands/search/osu.js index 0bf22ca7..9a131815 100644 --- a/commands/search/osu.js +++ b/commands/search/osu.js @@ -23,42 +23,46 @@ module.exports = class OsuCommand extends Command { async run(msg, args) { const { query } = args; - const { body } = await snekfetch - .get('https://osu.ppy.sh/api/get_user') - .query({ - k: OSU_KEY, - u: query, - type: 'string' - }); - if (!body.length) return msg.say('Could not find any results.'); - const embed = new MessageEmbed() - .setColor(0xFF66AA) - .setAuthor('osu!', 'https://i.imgur.com/EmnUp00.png') - .setURL('https://osu.ppy.sh/') - .addField('❯ Username', - body[0].username, true) - .addField('❯ ID', - body[0].user_id, true) - .addField('❯ Level', - body[0].level || 'N/A', true) - .addField('❯ Accuracy', - body[0].accuracy || 'N/A', true) - .addField('❯ Rank', - body[0].pp_rank || 'N/A', true) - .addField('❯ Play Count', - body[0].playcount || 'N/A', true) - .addField('❯ Country', - body[0].country || 'N/A', true) - .addField('❯ Ranked Score', - body[0].ranked_score || 'N/A', true) - .addField('❯ Total Score', - body[0].total_score || 'N/A', true) - .addField('❯ SS', - body[0].count_rank_ss || 'N/A', true) - .addField('❯ S', - body[0].count_rank_s || 'N/A', true) - .addField('❯ A', - body[0].count_rank_a || 'N/A', true); - return msg.embed(embed); + try { + const { body } = await snekfetch + .get('https://osu.ppy.sh/api/get_user') + .query({ + k: OSU_KEY, + u: query, + type: 'string' + }); + if (!body.length) return msg.say('Could not find any results.'); + const embed = new MessageEmbed() + .setColor(0xFF66AA) + .setAuthor('osu!', 'https://i.imgur.com/EmnUp00.png') + .setURL('https://osu.ppy.sh/') + .addField('❯ Username', + body[0].username, true) + .addField('❯ ID', + body[0].user_id, true) + .addField('❯ Level', + body[0].level || 'N/A', true) + .addField('❯ Accuracy', + body[0].accuracy || 'N/A', true) + .addField('❯ Rank', + body[0].pp_rank || 'N/A', true) + .addField('❯ Play Count', + body[0].playcount || 'N/A', true) + .addField('❯ Country', + body[0].country || 'N/A', true) + .addField('❯ Ranked Score', + body[0].ranked_score || 'N/A', true) + .addField('❯ Total Score', + body[0].total_score || 'N/A', true) + .addField('❯ SS', + body[0].count_rank_ss || 'N/A', true) + .addField('❯ S', + body[0].count_rank_s || 'N/A', true) + .addField('❯ A', + body[0].count_rank_a || 'N/A', true); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/pokedex.js b/commands/search/pokedex.js index 9b1bc32c..3cbc6d73 100644 --- a/commands/search/pokedex.js +++ b/commands/search/pokedex.js @@ -42,7 +42,7 @@ module.exports = class PokedexCommand extends Command { return msg.embed(embed); } catch (err) { if (err.status === 404) return msg.say('Could not find any results.'); - throw err; + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/search/reddit.js b/commands/search/reddit.js index e626ce5b..2add3322 100644 --- a/commands/search/reddit.js +++ b/commands/search/reddit.js @@ -46,8 +46,8 @@ module.exports = class RedditCommand extends Command { return msg.embed(embed); } catch (err) { if (err.status === 403) return msg.say('This subreddit is private.'); - if (err.status === 404) return msg.say('Could not find the subreddit.'); - throw err; + if (err.status === 404) return msg.say('Could not find any results.'); + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/commands/search/soundcloud.js b/commands/search/soundcloud.js index 2cc3e365..7702c16a 100644 --- a/commands/search/soundcloud.js +++ b/commands/search/soundcloud.js @@ -23,29 +23,33 @@ module.exports = class SoundCloudCommand extends Command { async run(msg, args) { const { query } = args; - const { body } = await snekfetch - .get('https://api.soundcloud.com/tracks') - .query({ - q: query, - client_id: SOUNDCLOUD_KEY - }); - if (!body.length) return msg.say('No Results.'); - const embed = new MessageEmbed() - .setColor(0xF15A22) - .setAuthor('SoundCloud', 'https://i.imgur.com/lFIz7RU.png') - .setTitle(body[0].title) - .setURL(body[0].permalink_url) - .setThumbnail(body[0].artwork_url) - .addField('❯ Artist', - body[0].user.username) - .addField('❯ Download Count', - body[0].download_count, true) - .addField('❯ Comment Count', - body[0].comment_count, true) - .addField('❯ Playback Count', - body[0].playback_count, true) - .addField('❯ Favorited Count', - body[0].favoritings_count, true); - return msg.embed(embed); + try { + const { body } = await snekfetch + .get('https://api.soundcloud.com/tracks') + .query({ + q: query, + client_id: SOUNDCLOUD_KEY + }); + if (!body.length) return msg.say('Could not find any results.'); + const embed = new MessageEmbed() + .setColor(0xF15A22) + .setAuthor('SoundCloud', 'https://i.imgur.com/lFIz7RU.png') + .setTitle(body[0].title) + .setURL(body[0].permalink_url) + .setThumbnail(body[0].artwork_url) + .addField('❯ Artist', + body[0].user.username) + .addField('❯ Download Count', + body[0].download_count, true) + .addField('❯ Comment Count', + body[0].comment_count, true) + .addField('❯ Playback Count', + body[0].playback_count, true) + .addField('❯ Favorited Count', + body[0].favoritings_count, true); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/steam.js b/commands/search/steam.js index 174f3895..88483d3a 100644 --- a/commands/search/steam.js +++ b/commands/search/steam.js @@ -22,27 +22,31 @@ module.exports = class SteamCommand extends Command { async run(msg, args) { const { query } = args; - const { body } = 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 current = body.items[0].price ? body.items[0].price.final / 100 : 0.00; - const original = body.items[0].price ? body.items[0].price.initial / 100 : 0.00; - const price = current === original ? `$${current}` : `~~$${original}~~ $${current}`; - const embed = new MessageEmbed() - .setColor(0x101D2F) - .setAuthor('Steam', 'https://i.imgur.com/vL8b4D5.png') - .setTitle(body.items[0].name) - .setURL(`http://store.steampowered.com/app/${body.items[0].id}`) - .setImage(body.items[0].tiny_image) - .addField('❯ Price', - price, true) - .addField('❯ Metascore', - body.items[0].metascore || 'N/A', true); - return msg.embed(embed); + try { + const { body } = 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 current = body.items[0].price ? body.items[0].price.final / 100 : 0.00; + const original = body.items[0].price ? body.items[0].price.initial / 100 : 0.00; + const price = current === original ? `$${current}` : `~~$${original}~~ $${current}`; + const embed = new MessageEmbed() + .setColor(0x101D2F) + .setAuthor('Steam', 'https://i.imgur.com/vL8b4D5.png') + .setTitle(body.items[0].name) + .setURL(`http://store.steampowered.com/app/${body.items[0].id}`) + .setImage(body.items[0].tiny_image) + .addField('❯ Price', + price, true) + .addField('❯ Metascore', + body.items[0].metascore || 'N/A', true); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/tv-show.js b/commands/search/tv-show.js index 181827cb..449514e4 100644 --- a/commands/search/tv-show.js +++ b/commands/search/tv-show.js @@ -25,36 +25,40 @@ module.exports = class TVShowCommand extends Command { async run(msg, args) { const { query } = args; - const search = await snekfetch - .get('http://api.themoviedb.org/3/search/tv') - .query({ - api_key: TMDB_KEY, - include_adult: msg.channel.nsfw || false, - query - }); - if (!search.body.results.length) return msg.say('Could not find any results.'); - const { body } = await snekfetch - .get(`https://api.themoviedb.org/3/tv/${search.body.results[0].id}`) - .query({ api_key: TMDB_KEY }); - const embed = new MessageEmbed() - .setColor(0x00D474) - .setTitle(body.name) - .setURL(`https://www.themoviedb.org/tv/${body.id}`) - .setAuthor('TMDB', 'https://i.imgur.com/G9q4DF1.png') - .setDescription(body.overview ? shorten(body.overview) : 'No description available.') - .setThumbnail(body.poster_path ? `https://image.tmdb.org/t/p/w500${body.poster_path}` : null) - .addField('❯ First Air Date', - body.first_air_date || 'N/A', true) - .addField('❯ Last Air Date', - body.last_air_date || 'N/A', true) - .addField('❯ Seasons', - body.number_of_seasons || 'N/A', true) - .addField('❯ Episodes', - body.number_of_episodes || 'N/A', true) - .addField('❯ Genres', - body.genres.length ? body.genres.map(genre => genre.name).join(', ') : 'N/A') - .addField('❯ Production Companies', - body.production_companies.length ? body.production_companies.map(company => company.name).join(', ') : 'N/A'); - return msg.embed(embed); + try { + const search = await snekfetch + .get('http://api.themoviedb.org/3/search/tv') + .query({ + api_key: TMDB_KEY, + include_adult: msg.channel.nsfw || false, + query + }); + if (!search.body.results.length) return msg.say('Could not find any results.'); + const { body } = await snekfetch + .get(`https://api.themoviedb.org/3/tv/${search.body.results[0].id}`) + .query({ api_key: TMDB_KEY }); + const embed = new MessageEmbed() + .setColor(0x00D474) + .setTitle(body.name) + .setURL(`https://www.themoviedb.org/tv/${body.id}`) + .setAuthor('TMDB', 'https://i.imgur.com/G9q4DF1.png') + .setDescription(body.overview ? shorten(body.overview) : 'No description available.') + .setThumbnail(body.poster_path ? `https://image.tmdb.org/t/p/w500${body.poster_path}` : null) + .addField('❯ First Air Date', + body.first_air_date || 'N/A', true) + .addField('❯ Last Air Date', + body.last_air_date || 'N/A', true) + .addField('❯ Seasons', + body.number_of_seasons || 'N/A', true) + .addField('❯ Episodes', + body.number_of_episodes || 'N/A', true) + .addField('❯ Genres', + body.genres.length ? body.genres.map(genre => genre.name).join(', ') : 'N/A') + .addField('❯ Production Companies', + body.production_companies.length ? body.production_companies.map(c => c.name).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/commands/search/urban.js b/commands/search/urban.js index 10ef0710..c3c55c57 100644 --- a/commands/search/urban.js +++ b/commands/search/urban.js @@ -23,18 +23,22 @@ module.exports = class UrbanCommand extends Command { async run(msg, args) { const { query } = args; - const { body } = await snekfetch - .get('http://api.urbandictionary.com/v0/define') - .query({ term: query }); - if (!body.list.length) return msg.say('Could not find any results.'); - const embed = new MessageEmbed() - .setColor(0x32A8F0) - .setAuthor('Urban Dictionary', 'https://i.imgur.com/fzFuuL7.png') - .setURL(body.list[0].permalink) - .setTitle(body.list[0].word) - .setDescription(shorten(body.list[0].definition)) - .addField('❯ Example', - shorten(body.list[0].example, 1000) || 'None'); - return msg.embed(embed); + try { + const { body } = await snekfetch + .get('http://api.urbandictionary.com/v0/define') + .query({ term: query }); + if (!body.list.length) return msg.say('Could not find any results.'); + const embed = new MessageEmbed() + .setColor(0x32A8F0) + .setAuthor('Urban Dictionary', 'https://i.imgur.com/fzFuuL7.png') + .setURL(body.list[0].permalink) + .setTitle(body.list[0].word) + .setDescription(shorten(body.list[0].definition)) + .addField('❯ Example', + shorten(body.list[0].example, 1000) || 'None'); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/vocaloid.js b/commands/search/vocaloid.js index cc93cc13..0cf386d5 100644 --- a/commands/search/vocaloid.js +++ b/commands/search/vocaloid.js @@ -24,31 +24,35 @@ module.exports = class VocaloidCommand extends Command { async run(msg, args) { const { query } = args; - const { body } = await snekfetch - .get('http://vocadb.net/api/songs') - .query({ - query, - maxResults: 1, - getTotalCount: true, - sort: 'FavoritedTimes', - preferAccurateMatches: true, - nameMatchMode: 'Words', - fields: 'ThumbUrl,Lyrics' - }); - if (!body.totalCount) return msg.say('Could not find any results.'); - const embed = new MessageEmbed() - .setColor(0x86D2D0) - .setAuthor('VocaDB', 'https://i.imgur.com/9Tx9UIc.jpg') - .setTitle(body.items[0].name) - .setURL(`http://vocadb.net/S/${body.items[0].id}`) - .setDescription(body.items[0].lyrics.length ? shorten(body.items[0].lyrics[0].value) : 'No lyrics available.') - .setThumbnail(body.items[0].thumbUrl) - .addField('❯ Artist', - body.items[0].artistString) - .addField('❯ Publish Date', - moment(body.items[0].publishDate).format('MMMM Do YYYY'), true) - .addField('❯ Length', - moment.duration(body.items[0].lengthSeconds, 's').format(), true); - return msg.embed(embed); + try { + const { body } = await snekfetch + .get('http://vocadb.net/api/songs') + .query({ + query, + maxResults: 1, + getTotalCount: true, + sort: 'FavoritedTimes', + preferAccurateMatches: true, + nameMatchMode: 'Words', + fields: 'ThumbUrl,Lyrics' + }); + if (!body.totalCount) return msg.say('Could not find any results.'); + const embed = new MessageEmbed() + .setColor(0x86D2D0) + .setAuthor('VocaDB', 'https://i.imgur.com/9Tx9UIc.jpg') + .setTitle(body.items[0].name) + .setURL(`http://vocadb.net/S/${body.items[0].id}`) + .setDescription(body.items[0].lyrics.length ? shorten(body.items[0].lyrics[0].value) : 'N/A') + .setThumbnail(body.items[0].thumbUrl) + .addField('❯ Artist', + body.items[0].artistString) + .addField('❯ Publish Date', + moment(body.items[0].publishDate).format('MMMM Do YYYY'), true) + .addField('❯ Length', + moment.duration(body.items[0].lengthSeconds, 's').format(), true); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/wattpad.js b/commands/search/wattpad.js index 40f5b357..b7336449 100644 --- a/commands/search/wattpad.js +++ b/commands/search/wattpad.js @@ -25,33 +25,37 @@ module.exports = class WattpadCommand extends Command { async run(msg, args) { const { query } = args; - const { body } = await snekfetch - .get('https://api.wattpad.com:443/v4/stories') - .query({ - query, - limit: 1 - }) - .set({ Authorization: `Basic ${WATTPAD_KEY}` }); - if (!body.stories.length) return msg.say('Could not find any results.'); - const embed = new MessageEmbed() - .setColor(0xF89C34) - .setAuthor('Wattpad', 'https://i.imgur.com/Rw9vRQB.png') - .setURL(body.stories[0].url) - .setTitle(body.stories[0].title) - .setDescription(shorten(body.stories[0].description)) - .setThumbnail(body.stories[0].cover) - .addField('❯ Created On', - moment(body.stories[0].createDate).format('MMMM Do YYYY'), true) - .addField('❯ Author', - body.stories[0].user, true) - .addField('❯ Parts', - body.stories[0].numParts, true) - .addField('❯ Reads', - body.stories[0].readCount, true) - .addField('❯ Votes', - body.stories[0].voteCount, true) - .addField('❯ Comments', - body.stories[0].commentCount, true); - return msg.embed(embed); + try { + const { body } = await snekfetch + .get('https://api.wattpad.com:443/v4/stories') + .query({ + query, + limit: 1 + }) + .set({ Authorization: `Basic ${WATTPAD_KEY}` }); + if (!body.stories.length) return msg.say('Could not find any results.'); + const embed = new MessageEmbed() + .setColor(0xF89C34) + .setAuthor('Wattpad', 'https://i.imgur.com/Rw9vRQB.png') + .setURL(body.stories[0].url) + .setTitle(body.stories[0].title) + .setDescription(shorten(body.stories[0].description)) + .setThumbnail(body.stories[0].cover) + .addField('❯ Created On', + moment(body.stories[0].createDate).format('MMMM Do YYYY'), true) + .addField('❯ Author', + body.stories[0].user, true) + .addField('❯ Parts', + body.stories[0].numParts, true) + .addField('❯ Reads', + body.stories[0].readCount, true) + .addField('❯ Votes', + body.stories[0].voteCount, true) + .addField('❯ Comments', + body.stories[0].commentCount, true); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/weather.js b/commands/search/weather.js index 3948309b..6f75a1f1 100644 --- a/commands/search/weather.js +++ b/commands/search/weather.js @@ -22,42 +22,46 @@ module.exports = class WeatherCommand extends Command { async run(msg, args) { const { query } = args; - const { body } = await snekfetch - .get('https://query.yahooapis.com/v1/public/yql') - .query({ - q: `select * from weather.forecast where u='f' AND woeid in (select woeid from geo.places(1) where text="${query}")`, // eslint-disable-line max-len - format: 'json' - }); - if (!body.query.count) return msg.say('Could not find any results.'); - const embed = new MessageEmbed() - .setColor(0x0000FF) - .setAuthor(body.query.results.channel.title, 'https://i.imgur.com/2MT0ViC.png') - .setURL(body.query.results.channel.link) - .setTimestamp() - .addField('❯ City', - body.query.results.channel.location.city, true) - .addField('❯ Country', - body.query.results.channel.location.country, true) - .addField('❯ Region', - body.query.results.channel.location.region, true) - .addField('❯ Condition', - body.query.results.channel.item.condition.text, true) - .addField('❯ Temperature', - `${body.query.results.channel.item.condition.temp}°F`, true) - .addField('❯ Humidity', - body.query.results.channel.atmosphere.humidity, true) - .addField('❯ Pressure', - body.query.results.channel.atmosphere.pressure, true) - .addField('❯ Rising', - body.query.results.channel.atmosphere.rising, true) - .addField('❯ Visibility', - body.query.results.channel.atmosphere.visibility, true) - .addField('❯ Wind Chill', - body.query.results.channel.wind.chill, true) - .addField('❯ Wind Direction', - body.query.results.channel.wind.direction, true) - .addField('❯ Wind Speed', - body.query.results.channel.wind.speed, true); - return msg.embed(embed); + try { + const { body } = await snekfetch + .get('https://query.yahooapis.com/v1/public/yql') + .query({ + q: `select * from weather.forecast where u='f' AND woeid in (select woeid from geo.places(1) where text="${query}")`, // eslint-disable-line max-len + format: 'json' + }); + if (!body.query.count) return msg.say('Could not find any results.'); + const embed = new MessageEmbed() + .setColor(0x0000FF) + .setAuthor(body.query.results.channel.title, 'https://i.imgur.com/2MT0ViC.png') + .setURL(body.query.results.channel.link) + .setTimestamp() + .addField('❯ City', + body.query.results.channel.location.city, true) + .addField('❯ Country', + body.query.results.channel.location.country, true) + .addField('❯ Region', + body.query.results.channel.location.region, true) + .addField('❯ Condition', + body.query.results.channel.item.condition.text, true) + .addField('❯ Temperature', + `${body.query.results.channel.item.condition.temp}°F`, true) + .addField('❯ Humidity', + body.query.results.channel.atmosphere.humidity, true) + .addField('❯ Pressure', + body.query.results.channel.atmosphere.pressure, true) + .addField('❯ Rising', + body.query.results.channel.atmosphere.rising, true) + .addField('❯ Visibility', + body.query.results.channel.atmosphere.visibility, true) + .addField('❯ Wind Chill', + body.query.results.channel.wind.chill, true) + .addField('❯ Wind Direction', + body.query.results.channel.wind.direction, true) + .addField('❯ Wind Speed', + body.query.results.channel.wind.speed, true); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/wikipedia.js b/commands/search/wikipedia.js index a4f024bc..c8e2e1b8 100644 --- a/commands/search/wikipedia.js +++ b/commands/search/wikipedia.js @@ -23,24 +23,28 @@ module.exports = class WikipediaCommand extends Command { async run(msg, args) { const { query } = args; - const { body } = await snekfetch - .get('https://en.wikipedia.org/w/api.php') - .query({ - action: 'query', - prop: 'extracts', - format: 'json', - titles: query, - exintro: '', - explaintext: '', - redirects: '', - formatversion: 2 - }); - if (body.query.pages[0].missing) return msg.say('Could not find any results.'); - const embed = new MessageEmbed() - .setColor(0xE7E7E7) - .setTitle(body.query.pages[0].title) - .setAuthor('Wikipedia', 'https://i.imgur.com/a4eeEhh.png') - .setDescription(shorten(body.query.pages[0].extract.replace(/\n/g, '\n\n'))); - return msg.embed(embed); + try { + const { body } = await snekfetch + .get('https://en.wikipedia.org/w/api.php') + .query({ + action: 'query', + prop: 'extracts', + format: 'json', + titles: query, + exintro: '', + explaintext: '', + redirects: '', + formatversion: 2 + }); + if (body.query.pages[0].missing) return msg.say('Could not find any results.'); + const embed = new MessageEmbed() + .setColor(0xE7E7E7) + .setTitle(body.query.pages[0].title) + .setAuthor('Wikipedia', 'https://i.imgur.com/a4eeEhh.png') + .setDescription(shorten(body.query.pages[0].extract.replace(/\n/g, '\n\n'))); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/youtube.js b/commands/search/youtube.js index d9102967..d1b0c0fb 100644 --- a/commands/search/youtube.js +++ b/commands/search/youtube.js @@ -23,23 +23,27 @@ module.exports = class YouTubeCommand extends Command { async run(msg, args) { const { query } = args; - const { body } = await snekfetch - .get('https://www.googleapis.com/youtube/v3/search') - .query({ - part: 'snippet', - type: 'video', - maxResults: 1, - q: query, - key: GOOGLE_KEY - }); - if (!body.items.length) return msg.say('Could not find any results.'); - const embed = new MessageEmbed() - .setColor(0xDD2825) - .setTitle(body.items[0].snippet.title) - .setDescription(body.items[0].snippet.description) - .setAuthor(`YouTube - ${body.items[0].snippet.channelTitle}`, 'https://i.imgur.com/hkUafwu.png') - .setURL(`https://www.youtube.com/watch?v=${body.items[0].id.videoId}`) - .setThumbnail(body.items[0].snippet.thumbnails.default.url); - return msg.embed(embed); + try { + const { body } = await snekfetch + .get('https://www.googleapis.com/youtube/v3/search') + .query({ + part: 'snippet', + type: 'video', + maxResults: 1, + q: query, + key: GOOGLE_KEY + }); + if (!body.items.length) return msg.say('Could not find any results.'); + const embed = new MessageEmbed() + .setColor(0xDD2825) + .setTitle(body.items[0].snippet.title) + .setDescription(body.items[0].snippet.description) + .setAuthor(`YouTube - ${body.items[0].snippet.channelTitle}`, 'https://i.imgur.com/hkUafwu.png') + .setURL(`https://www.youtube.com/watch?v=${body.items[0].id.videoId}`) + .setThumbnail(body.items[0].snippet.thumbnails.default.url); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/search/yu-gi-oh.js b/commands/search/yu-gi-oh.js index ca7abbe6..9e59d21b 100644 --- a/commands/search/yu-gi-oh.js +++ b/commands/search/yu-gi-oh.js @@ -23,29 +23,33 @@ module.exports = class YuGiOhCommand extends Command { async run(msg, args) { const { query } = args; - const { body } = await snekfetch - .get(`http://yugiohprices.com/api/card_data/${query}`); - if (body.status === 'fail') return msg.say('Could not find any results.'); - const embed = new MessageEmbed() - .setColor(0xBE5F1F) - .setTitle(body.data.name) - .setDescription(body.data.text) - .setAuthor('Yu-Gi-Oh!', 'https://i.imgur.com/7gPm9Rr.png') - .addField('❯ Card Type', - body.data.card_type, true); - if (body.data.card_type === 'monster') { - embed - .addField('❯ Type', - body.data.type, true) - .addField('❯ Attribute', - body.data.family, true) - .addField('❯ ATK', - body.data.atk, true) - .addField('❯ DEF', - body.data.def, true) - .addField('❯ Level', - body.data.level, true); + try { + const { body } = await snekfetch + .get(`http://yugiohprices.com/api/card_data/${query}`); + if (body.status === 'fail') return msg.say('Could not find any results.'); + const embed = new MessageEmbed() + .setColor(0xBE5F1F) + .setTitle(body.data.name) + .setDescription(body.data.text) + .setAuthor('Yu-Gi-Oh!', 'https://i.imgur.com/7gPm9Rr.png') + .addField('❯ Card Type', + body.data.card_type, true); + if (body.data.card_type === 'monster') { + embed + .addField('❯ Type', + body.data.type, true) + .addField('❯ Attribute', + body.data.family, true) + .addField('❯ ATK', + body.data.atk, true) + .addField('❯ DEF', + body.data.def, true) + .addField('❯ Level', + body.data.level, true); + } + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } - return msg.embed(embed); } }; diff --git a/commands/text-edit/translate.js b/commands/text-edit/translate.js index bb7c5059..ce47ecd2 100644 --- a/commands/text-edit/translate.js +++ b/commands/text-edit/translate.js @@ -51,20 +51,24 @@ module.exports = class TranslateCommand extends Command { async run(msg, args) { const { text, target, original } = args; - const { body } = await snekfetch - .get('https://translate.yandex.net/api/v1.5/tr.json/translate') - .query({ - key: YANDEX_KEY, - text, - lang: original ? `${original}-${target}` : target - }); - const lang = body.lang.split('-'); - const embed = new MessageEmbed() - .setColor(0x00AE86) - .addField(`❯ From: ${codes[lang[0]]}`, - text) - .addField(`❯ To: ${codes[lang[1]]}`, - body.text[0]); - return msg.embed(embed); + try { + const { body } = await snekfetch + .get('https://translate.yandex.net/api/v1.5/tr.json/translate') + .query({ + key: YANDEX_KEY, + text, + lang: original ? `${original}-${target}` : target + }); + const lang = body.lang.split('-'); + const embed = new MessageEmbed() + .setColor(0x00AE86) + .addField(`❯ From: ${codes[lang[0]]}`, + text) + .addField(`❯ To: ${codes[lang[1]]}`, + body.text[0]); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/text-edit/webhook.js b/commands/text-edit/webhook.js index 26fbe342..790beef1 100644 --- a/commands/text-edit/webhook.js +++ b/commands/text-edit/webhook.js @@ -24,9 +24,13 @@ module.exports = class WebhookCommand extends Command { async run(msg, args) { const { content } = args; if (msg.guild && msg.channel.permissionsFor(this.client.user).has('MANAGE_MESSAGES')) await msg.delete(); - await snekfetch - .post(WEBHOOK_URL) - .send({ content }); - return null; + try { + await snekfetch + .post(WEBHOOK_URL) + .send({ content }); + return null; + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } } }; diff --git a/commands/util/upvote.js b/commands/util/upvote.js index 0fcd3616..7e94205a 100644 --- a/commands/util/upvote.js +++ b/commands/util/upvote.js @@ -16,12 +16,19 @@ module.exports = class UpvoteCommand extends Command { } async run(msg) { - const { body } = await snekfetch - .get(`https://discordbots.org/api/bots/${this.client.user.id}/votes`) - .set({ Authorization: DBOTSORG_KEY }); - return msg.say(stripIndents` - Upvote Xiao and get rewards while joining ${body.length} others! - - `); + try { + const { body } = await snekfetch + .get(`https://discordbots.org/api/bots/${this.client.user.id}/votes`) + .set({ Authorization: DBOTSORG_KEY }); + return msg.say(stripIndents` + Upvote Xiao and get rewards while joining ${body.length} others! + + `); + } catch (err) { + return msg.say(stripIndents` + Upvote Xiao and get rewards! + + `); + } } }; diff --git a/package.json b/package.json index a97d283e..ae668b0d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "31.2.5", + "version": "31.2.6", "description": "Your personal server companion.", "main": "Shard.js", "scripts": {