diff --git a/config.default.jsonc b/config.default.jsonc index 870d89f..8ff9d5a 100644 --- a/config.default.jsonc +++ b/config.default.jsonc @@ -36,6 +36,11 @@ // if the `enabled` key is missing, the extension will be disabled by default, and a warning issued // permissions are explained in the production environment doc "modules": { + "joke": { + "enabled": true, + // Boolean for if nsfw jokes should be displayed or not + "nsfw": false + }, "logging": { "enabled": true, // each channel ID paired with a respecting logging channel, if it's not specified here diff --git a/src/modules/joke.ts b/src/modules/joke.ts new file mode 100644 index 0000000..ec75dce --- /dev/null +++ b/src/modules/joke.ts @@ -0,0 +1,65 @@ +/** + * @file + * Modules: + * - {@link joke} + * Description: + * Uses the joke api provided by arc to parse a response and send it as a discord embed + * @throws will throw an error if the api call fails + */ + +import * as util from '../core/util.js'; +import {botConfig} from '../core/config.js'; + +// Config manager for nsfw detection +const jokeConfig = botConfig.modules.joke; +let jokeApiUrl = 'https://v2.jokeapi.dev/joke/Any'; + +const blacklistFlags = [ + 'religious', + 'political', + 'racist', + 'sexist', + 'explicit', +]; + +if (!jokeConfig.nsfw) { + blacklistFlags.push('nsfw'); +} + +jokeApiUrl += `?blacklistFlags=${blacklistFlags.join(',')}&type=single`; + +const joke = new util.RootModule( + 'joke', + 'Get a funny joke from the bot', + [], + [], + + async (args, interaction) => { + let errorValue = false; + const fetchedJoke = await fetchJoke().catch(() => { + errorValue = true; + }); + if (errorValue) { + util.replyToInteraction(interaction, { + embeds: [util.embed.errorEmbed('Failed to fetch joke')], + }); + } else { + util.replyToInteraction(interaction, { + embeds: [util.embed.infoEmbed(`${fetchedJoke}`)], + }); + } + } +); + +async function fetchJoke(): Promise { + const response = await fetch(jokeApiUrl); + + if (response.ok) { + const data = await response.json(); + return data.joke; + } else { + throw new Error('Failed to fetch joke'); + } +} + +export default joke;