Use Joke API in joke

This commit is contained in:
Dragon Fire
2021-04-24 17:35:29 -04:00
parent d321ecaac3
commit a86edec79b
2 changed files with 28 additions and 107 deletions
+28 -4
View File
@@ -1,5 +1,8 @@
const Command = require('../../structures/Command');
const jokes = require('../../assets/json/joke');
const request = require('node-superfetch');
const { stripIndents } = require('common-tags');
const blacklistFlags = ['religious', 'racist', 'sexist'];
const nsfw = ['nsfw', 'explicit'];
module.exports = class JokeCommand extends Command {
constructor(client) {
@@ -7,11 +10,32 @@ module.exports = class JokeCommand extends Command {
name: 'joke',
group: 'random-res',
memberName: 'joke',
description: 'Responds with a random joke.'
description: 'Responds with a random joke.',
credit: [
{
name: 'JokeAPI',
url: 'https://v2.jokeapi.dev/',
reason: 'API'
}
]
});
}
run(msg) {
return msg.say(jokes[Math.floor(Math.random() * jokes.length)]);
async run(msg) {
const blacklist = msg.channel.nsfw ? blacklistFlags : blacklistFlags.concat(nsfw);
try {
const { body } = await request
.get('https://v2.jokeapi.dev/joke/Any')
.query({ blacklistFlags: blacklist.join(',') });
if (body.type === 'twopart') {
return msg.say(stripIndents`
_${body.setup}_
${body.delivery}
`);
}
return msg.say(body.joke);
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};