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

Commit

Permalink
Add: Joke module (#53)
Browse files Browse the repository at this point in the history
* Add: basic joke module

* Add: Core functionality to joke module

* fix: remove random mistype

* Add: Better error handling

* Add: NSFW config flag

* Fix: debug log

* fix!: added suggestions

Now i'm running into an issue where sometimes it fetches the joke perfectly with no issues, but other times i get a `Invalid Form Body` error.

* Fix: Removed undefined errors

I feel so dumb, there was a tiny typo that broke the entire thing, and i forgot to format the response as a string 😭

* Fix: Remove try catch

@zleyyij wanted it so i implemented it
  • Loading branch information
Zephira58 authored Nov 15, 2023
1 parent 32af217 commit d6ba09c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config.default.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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": {
// restrict this command to people with the `administrator` perm
"permissions": {
Expand Down
65 changes: 65 additions & 0 deletions src/modules/joke.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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;

0 comments on commit d6ba09c

Please sign in to comment.