Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Add: Joke module #53

Merged
merged 9 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config.default.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions src/modules/joke.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @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) => {
const fetchedJoke = await fetchJoke();
util.replyToInteraction(interaction, {
embeds: [util.embed.infoEmbed(`${fetchedJoke}`)],
});
}
);

async function fetchJoke(): Promise<string> {
try {
Zephira58 marked this conversation as resolved.
Show resolved Hide resolved
const response = await fetch(jokeApiUrl);

if (response.ok) {
const data = await response.json();
return data.joke;
} else {
throw new Error('Failed to fetch joke');
}
} catch (error) {
throw new Error('Failed to fetch joke');
}
}

export default joke;