Skip to content

Commit 8f06824

Browse files
feat: add joke command
1 parent 7973547 commit 8f06824

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import type { Buildable } from "@framework/commands/Command";
2+
import { Command } from "@framework/commands/Command";
3+
import type Context from "@framework/commands/Context";
4+
import { env } from "@main/env/env";
5+
import { getAxiosClient } from "@main/utils/axios";
6+
import { EmbedBuilder } from "discord.js";
7+
8+
class JokeCommand extends Command {
9+
public override readonly name = "joke";
10+
public override readonly description: string = "Fetch a random joke.";
11+
public override readonly usage = [""];
12+
public override readonly aliases = ["dadjoke"];
13+
14+
public override build(): Buildable[] {
15+
return [
16+
this.buildChatInput().addStringOption(option =>
17+
option
18+
.setName("type")
19+
.setDescription("The type of joke to fetch.")
20+
.setRequired(false)
21+
.setChoices(
22+
{
23+
name: "Default",
24+
value: "joke"
25+
},
26+
{
27+
name: "Dad Joke",
28+
value: "dad_joke"
29+
}
30+
)
31+
)
32+
];
33+
}
34+
35+
public override async execute(context: Context): Promise<void> {
36+
const isDadJoke =
37+
context.commandName === "dadjoke" ||
38+
(context.isChatInput() && context.options.getString("type") === "dad_joke");
39+
40+
if (isDadJoke && !env.API_NINJAS_JOKE_API_KEY) {
41+
await context.error("Dad jokes are disabled because the API key is not set.");
42+
return;
43+
}
44+
45+
const url: string = isDadJoke
46+
? "https://api.api-ninjas.com/v1/dadjokes?limit=1"
47+
: "https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist";
48+
49+
try {
50+
const response = await getAxiosClient().get(url, {
51+
headers: isDadJoke
52+
? {
53+
Accept: "application/json",
54+
"X-Api-Key": process.env.API_NINJAS_JOKE_API_KEY
55+
}
56+
: undefined
57+
});
58+
59+
const embed = new EmbedBuilder()
60+
.setColor("#007bff")
61+
.setTitle("Joke")
62+
.setDescription(
63+
!isDadJoke
64+
? response.data.type === "twopart"
65+
? response.data.setup + "\n\n" + response.data.delivery
66+
: response.data.joke
67+
: response.data[0].joke
68+
)
69+
.addFields(
70+
...(!isDadJoke
71+
? [
72+
{
73+
name: "Category",
74+
value: response.data.category
75+
}
76+
]
77+
: [])
78+
)
79+
.setFooter({
80+
text: !isDadJoke
81+
? `ID: ${response.data.id}`
82+
: `Type: ${isDadJoke ? "Dad Joke" : "Regular"}`
83+
});
84+
85+
await context.reply({ embeds: [embed] });
86+
} catch (error) {
87+
this.application.logger.error(error);
88+
await context.error("Failed to fetch joke: Invalid API response");
89+
return;
90+
}
91+
}
92+
}
93+
94+
export default JokeCommand;

src/main/typescript/schemas/EnvironmentVariableSchema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export const EnvironmentVariableSchema = z.object({
3333
.string()
3434
.optional()
3535
.nullable()
36-
.transform(value => (value === "null" ? null : value))
36+
.transform(value => (value === "null" ? null : value)),
37+
API_NINJAS_JOKE_API_KEY: z.string().optional()
3738
});
3839

3940
export type EnvironmentVariableRecord = z.infer<typeof EnvironmentVariableSchema>;

0 commit comments

Comments
 (0)