From b99e1de2574282d687972de49c12cb5eb413ca7f Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sun, 31 Mar 2024 00:47:08 -0400 Subject: [PATCH] Filter NSFW chuck norris --- commands/random-res/chuck-norris.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/commands/random-res/chuck-norris.js b/commands/random-res/chuck-norris.js index e4ff9761..c8396562 100644 --- a/commands/random-res/chuck-norris.js +++ b/commands/random-res/chuck-norris.js @@ -1,5 +1,6 @@ const Command = require('../../framework/Command'); const request = require('node-superfetch'); +const nsfwCategories = ['explicit']; module.exports = class ChuckNorrisCommand extends Command { constructor(client) { @@ -23,10 +24,25 @@ module.exports = class ChuckNorrisCommand extends Command { } ] }); + + this.categories = null; } async run(msg) { - const { body } = await request.get('https://api.chucknorris.io/jokes/random'); + if (!this.categories) await this.fetchCategories(); + const categories = msg.channel.nsfw + ? this.categories + : this.categories.filter(cat => !nsfwCategories.includes(cat)); + const { body } = await request + .get('https://api.chucknorris.io/jokes/random') + .query({ category: categories.join(',') }); return msg.say(body.value); } + + async fetchCategories() { + if (this.categories) return this.categories; + const { body } = await request.get('https://api.chucknorris.io/jokes/categories'); + this.categories = body; + return this.categories; + } };