This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |