/[sudobot]/branches/6.x/src/commands/fun/JokeCommand.ts
ViewVC logotype

Contents of /branches/6.x/src/commands/fun/JokeCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 5184 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 /**
2 * This file is part of SudoBot.
3 *
4 * Copyright (C) 2021-2023 OSN Developers.
5 *
6 * SudoBot is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * SudoBot is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 import axios, { AxiosRequestConfig } from "axios";
21 import { EmbedBuilder, SlashCommandBuilder } from "discord.js";
22 import Command, { BasicCommandContext, CommandMessage, CommandReturn, ValidationRule } from "../../core/Command";
23 import { GuildConfig } from "../../types/GuildConfigSchema";
24 import { log, logError } from "../../utils/logger";
25
26 type JokeType = GuildConfig["commands"]["default_joke_type"];
27
28 export default class JokeCommand extends Command {
29 public readonly name = "joke";
30 public readonly validationRules: ValidationRule[] = [];
31 public readonly permissions = [];
32 public readonly urls: Record<JokeType, () => [Exclude<JokeType, "random">, string, AxiosRequestConfig]> = {
33 joke: () => [
34 "joke",
35 "https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist",
36 {
37 headers: {
38 Accept: "application/json"
39 }
40 }
41 ],
42 dadjoke: () => [
43 "dadjoke",
44 "https://api.api-ninjas.com/v1/dadjokes?limit=1",
45 {
46 headers: {
47 Accept: "application/json",
48 "X-Api-Key": process.env.API_NINJAS_JOKE_API_KEY
49 }
50 }
51 ],
52 random: () => {
53 const values = Object.values(this.urls);
54 return values[Math.floor(Math.random() * values.length)]!();
55 }
56 };
57
58 public readonly description = "Tells you a random joke.";
59
60 public readonly slashCommandBuilder = new SlashCommandBuilder().addStringOption(option =>
61 option
62 .setName("type")
63 .setDescription("The type of the joke")
64 .setChoices<Array<{ name: string; value: JokeType }>>(
65 {
66 name: "Random (Default)",
67 value: "random"
68 },
69 {
70 name: "Regular Joke",
71 value: "joke"
72 },
73 ...(process.env.API_NINJAS_JOKE_API_KEY
74 ? ([
75 {
76 name: "Dad Joke",
77 value: "dadjoke"
78 }
79 ] as const)
80 : [])
81 )
82 );
83
84 async execute(message: CommandMessage, context: BasicCommandContext): Promise<CommandReturn> {
85 const type = <JokeType>(
86 ((context.isLegacy ? null : context.options.getString("type")) ??
87 this.client.configManager.config[message.guildId!]?.commands?.default_joke_type)
88 );
89
90 await this.deferIfInteraction(message);
91
92 const [finalType, url, options] = this.urls[type]();
93
94 try {
95 const response = await axios.get(url, options);
96
97 log(response.data);
98
99 await this.deferredReply(message, {
100 embeds: [
101 new EmbedBuilder()
102 .setColor("#007bff")
103 .setTitle("Joke")
104 .setDescription(
105 finalType === "joke"
106 ? response.data.type === "twopart"
107 ? response.data.setup + "\n\n" + response.data.delivery
108 : response.data.joke
109 : response.data[0].joke
110 )
111 .addFields(
112 ...(finalType === "joke"
113 ? [
114 {
115 name: "Category",
116 value: response.data.category
117 }
118 ]
119 : [])
120 )
121 .setFooter({
122 text:
123 finalType === "joke"
124 ? `ID: ${response.data.id}`
125 : `Type: ${type[0].toUpperCase() + type.substring(1)}`
126 })
127 ]
128 });
129 } catch (e) {
130 logError(e);
131
132 await this.deferredReply(message, {
133 content: "Something went wrong with the API response. Please try again later."
134 });
135 }
136 }
137 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26