Files
blitzkit/packages/bot/src/events/interactionCreate/handlers/chatInputCommand.ts
T

203 lines
6.5 KiB
TypeScript
Raw Normal View History

2025-03-30 14:37:40 -05:00
import { isFunny } from '@blitzkit/core';
2023-06-25 02:32:56 +00:00
import {
ActionRowBuilder,
2023-11-14 13:42:15 -06:00
AttachmentBuilder,
2023-06-25 02:32:56 +00:00
ButtonBuilder,
2025-01-16 14:10:28 -06:00
ButtonStyle,
2023-06-25 02:32:56 +00:00
CacheType,
ChatInputCommandInteraction,
2023-11-14 13:42:15 -06:00
InteractionReplyOptions,
2023-06-25 02:32:56 +00:00
} from 'discord.js';
2024-03-16 00:06:44 -05:00
import { InteractionRawReturnable, commands } from '..';
2025-01-16 14:10:28 -06:00
import { UserError } from '../../../core/blitzkit/userError';
2024-09-08 10:11:24 -05:00
import { buttonLink } from '../../../core/discord/buttonLink';
import { embedWarning } from '../../../core/discord/embedWarning';
import { normalizeInteractionReturnable } from '../../../core/discord/normalizeInteractionReturnable';
2023-10-17 20:58:36 -05:00
import { psa } from '../../../core/discord/psa';
2024-03-09 12:01:41 -06:00
import { translator } from '../../../core/localization/translator';
2024-06-23 20:59:03 -05:00
import { Writeable } from '../../../types/writable';
2023-06-25 02:32:56 +00:00
2025-03-30 14:37:40 -05:00
export enum FunnyType {
Rude,
Weird,
}
const funnyResponses: Record<FunnyType, string[]> = {
[FunnyType.Rude]: [
"Your decisions were calculated, but, man, you're bad at math.",
'Hey man, have you considered like not lacking?',
'Let me guess... it was your team?',
'Confidence is key. Unfortunately, you lost the lock.',
"When I said live green, I wasn't talking about your winrate.",
'Viewing your stats, publicly? You must be brave.',
'Bold of you to assume the numbers have gone up since yesterday.',
"I'd give you advice, but I don't think you'd understand.",
"I didn't believe in negative numbers until I saw your stats.",
'I was gaining hope in humanity. Something changed today.',
"I've seen potatoes aim better than you.",
"I've seen sloths move faster than your tank.",
],
[FunnyType.Weird]: [
'yeah daddy, use me more',
'uwu, hewe are your swaws... uwu',
'hehehehe it tickles',
'yeeeeeeeeeeeeahhhhhh that hits the spot',
'ooooh yesss my circuits are starting to hurt yeeeeeeee',
],
};
2024-09-08 10:11:24 -05:00
export async function handleChatInputCommand(
2023-06-25 02:32:56 +00:00
interaction: ChatInputCommandInteraction<CacheType>,
) {
2025-03-30 14:37:40 -05:00
let funny: FunnyType | undefined = undefined;
await interaction.deferReply();
if (isFunny) {
const funniesLength = Object.keys(funnyResponses).length;
if (import.meta.env.DEBUG_FUNNY === 'true') {
// in debug mode, just pick a random one
funny = Math.floor(Math.random() * funniesLength);
} else {
const id = BigInt(interaction.user.id);
funny = Number(id % BigInt(funniesLength));
}
const responses = funnyResponses[funny];
const responseIndex = Math.floor(Math.random() * responses.length);
const response = `### ${responses[responseIndex]}`;
const waitTime = response.length * 45;
await interaction.editReply(response);
await new Promise((resolve) => setTimeout(resolve, waitTime));
}
2024-01-19 21:32:46 -06:00
const awaitedCommands = await commands;
const registry = awaitedCommands[interaction.commandName];
2023-06-25 02:32:56 +00:00
try {
2023-06-27 01:43:30 +00:00
const returnable = await registry.handler(interaction);
2023-06-25 02:32:56 +00:00
2023-06-27 01:43:30 +00:00
if (registry.handlesInteraction) return;
2023-06-25 02:32:56 +00:00
2024-03-16 00:06:44 -05:00
const chunks: InteractionRawReturnable[][] = [[]];
2023-06-25 02:32:56 +00:00
2024-03-16 00:06:44 -05:00
(Array.isArray(returnable) ? returnable : [returnable]).forEach(
(object) => {
if (typeof object === 'string') {
chunks.push([object]);
} else chunks.at(-1)?.push(object!);
},
);
2023-11-14 13:42:15 -06:00
2024-03-16 00:06:44 -05:00
let index = 0;
for (const chunk of chunks.filter((chunk) => chunk.length > 0)) {
2025-03-30 14:37:40 -05:00
const reply = await normalizeInteractionReturnable(chunk, funny);
2024-03-16 00:06:44 -05:00
if (
psa.data &&
!psa.data.secret &&
(psa.data.commands === undefined ||
psa.data.commands.includes(interaction.commandName))
) {
2024-04-10 16:29:12 -05:00
if (psa.data.type === undefined || psa.data.type === 'embed') {
2024-03-16 00:06:44 -05:00
if (!reply.embeds) reply.embeds = [];
2024-04-10 16:29:12 -05:00
const embed = embedWarning(psa.data.title, psa.data.description);
if (psa.data.image) embed.setImage(psa.data.image);
2024-06-23 20:59:03 -05:00
(reply.embeds as Writeable<typeof reply.embeds>).push(embed);
2024-04-10 16:29:12 -05:00
} else if (psa.data.type === 'image') {
2024-03-16 00:06:44 -05:00
if (!reply.files) reply.files = [];
2024-06-23 20:59:03 -05:00
(reply.files as Writeable<typeof reply.files>).push(
new AttachmentBuilder(psa.data.image),
);
2024-03-16 00:06:44 -05:00
reply.content = reply.content ?? psa.data.title;
2023-11-14 13:42:15 -06:00
}
2024-03-16 00:06:44 -05:00
if (psa.data.links) {
if (!reply.components?.[0]) {
reply.components = [new ActionRowBuilder<ButtonBuilder>()];
}
(
reply.components[0] as ActionRowBuilder<ButtonBuilder>
).addComponents(
psa.data.links.map(({ title, url }) => buttonLink(url, title)),
);
}
2023-11-14 13:42:15 -06:00
}
2023-06-25 02:32:56 +00:00
2024-03-16 00:06:44 -05:00
if (index === 0) {
await interaction.editReply(reply);
2023-11-14 13:42:15 -06:00
} else {
2024-03-16 00:06:44 -05:00
await interaction.followUp(reply);
2023-11-14 13:42:15 -06:00
}
2024-03-16 00:06:44 -05:00
if (
psa.data &&
psa.data.secret &&
(psa.data.commands === undefined ||
psa.data.commands.includes(interaction.commandName))
) {
const followUp: InteractionReplyOptions = {
ephemeral: true,
};
2024-04-10 16:29:12 -05:00
if (psa.data.type === undefined || psa.data.type === 'embed') {
2024-03-16 00:06:44 -05:00
if (!followUp.embeds) followUp.embeds = [];
2024-04-10 16:29:12 -05:00
const embed = embedWarning(psa.data.title, psa.data.description);
if (psa.data.image) embed.setImage(psa.data.image);
2024-06-23 20:59:03 -05:00
(followUp.embeds as Writeable<typeof followUp.embeds>).push(embed);
2024-04-10 16:29:12 -05:00
} else if (psa.data.type === 'image') {
2024-03-16 00:06:44 -05:00
if (!followUp.files) followUp.files = [];
2024-06-23 20:59:03 -05:00
(followUp.files as Writeable<typeof followUp.files>).push(
new AttachmentBuilder(psa.data.image),
);
2024-03-16 00:06:44 -05:00
followUp.content = followUp.content ?? psa.data.title;
2023-11-14 13:42:15 -06:00
}
2024-03-16 00:06:44 -05:00
if (psa.data.links) {
if (!followUp.components?.[0]) {
followUp.components = [new ActionRowBuilder<ButtonBuilder>()];
}
(
followUp.components[0] as ActionRowBuilder<ButtonBuilder>
).addComponents(
psa.data.links.map(({ title, url }) => buttonLink(url, title)),
);
}
interaction.followUp(followUp);
2023-11-14 13:42:15 -06:00
}
2024-03-16 00:06:44 -05:00
index++;
2023-11-14 13:42:15 -06:00
}
2023-06-25 02:32:56 +00:00
} catch (error) {
2025-01-16 14:10:28 -06:00
const { strings } = translator(interaction.locale);
const components = [
new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setLabel(strings.bot.common.errors.get_help)
.setURL('https://discord.gg/nDt7AjGJQH')
.setStyle(ButtonStyle.Link),
),
];
if (error instanceof UserError) {
interaction.editReply({
content: error.message,
components,
});
} else {
console.error(interaction.commandName, error);
interaction.editReply({
content: strings.bot.common.errors.uncaught_error,
components,
});
}
2023-06-25 02:32:56 +00:00
}
}