Change Pun to use Joke API too

This commit is contained in:
Dragon Fire
2021-04-24 17:38:28 -04:00
parent fb110a9dea
commit 70b8526795
2 changed files with 28 additions and 241 deletions
+28 -4
View File
@@ -1,5 +1,8 @@
const Command = require('../../structures/Command');
const puns = require('../../assets/json/pun');
const request = require('node-superfetch');
const { stripIndents } = require('common-tags');
const blacklistFlags = ['religious', 'racist', 'sexist'];
const nsfw = ['nsfw', 'explicit'];
module.exports = class PunCommand extends Command {
constructor(client) {
@@ -7,11 +10,32 @@ module.exports = class PunCommand extends Command {
name: 'pun',
group: 'random-res',
memberName: 'pun',
description: 'Responds with a random pun.'
description: 'Responds with a random pun.',
credit: [
{
name: 'JokeAPI',
url: 'https://v2.jokeapi.dev/',
reason: 'API'
}
]
});
}
run(msg) {
return msg.say(puns[Math.floor(Math.random() * puns.length)]);
async run(msg) {
const blacklist = msg.channel.nsfw ? blacklistFlags : blacklistFlags.concat(nsfw);
try {
const { body } = await request
.get('https://v2.jokeapi.dev/joke/Pun')
.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!`);
}
}
};