joke.ts
1import { ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js'
2import { Command } from '@/types/bot'
3import axios from 'axios'
4
5export default {
6 data: new SlashCommandBuilder()
7 .setName('joke')
8 .setDescription('Get a random joke')
9 .addStringOption((option) =>
10 option
11 .setName('category')
12 .setDescription('Joke category')
13 .addChoices(
14 { name: 'Programming', value: 'programming' },
15 { name: 'General', value: 'general' },
16 { name: 'Pun', value: 'pun' },
17 )
18 .setRequired(false),
19 ),
20
21 async execute(interaction: ChatInputCommandInteraction) {
22 const category = interaction.options.getString('category') ?? 'any'
23
24 try {
25 const response = await axios.get(
26 `https://v2.jokeapi.dev/joke/${category}?safe-mode`,
27 )
28 const joke = response.data
29
30 if (joke.type === 'single') {
31 await interaction.reply(`😄 ${joke.joke}`)
32 } else {
33 await interaction.reply(`😄 ${joke.setup}\n\n||${joke.delivery}||`)
34 }
35 } catch (error) {
36 await interaction.reply('Failed to fetch a joke. Please try again later.')
37 }
38 },
39} as Command
40