Files
Nexumi/apps/bot/src/modules/utility/context-menus.ts
TheOnlyMace 8513848440 Implement command cooldown management and enhance command execution flow
- Introduced `applyCommandCooldown` function to manage per-command cooldowns after execution, improving command rate limiting.
- Updated command execution flow to include cooldown application, ensuring users are informed of cooldown status.
- Enhanced error handling in interaction replies, allowing for more graceful error management and user feedback.
- Improved the handling of interaction responses across various commands, ensuring consistent user experience.
- Added permission checks for new commands, ensuring only authorized users can execute specific actions.
2026-07-25 15:37:56 +02:00

52 lines
1.7 KiB
TypeScript

import { ContextMenuCommandBuilder, ApplicationCommandType } from 'discord.js';
import { t } from '@nexumi/shared';
import type { ContextMenuCommand } from '../../types.js';
import { getGuildLocale } from '../../i18n.js';
async function translateText(text: string, targetLang: string): Promise<string | null> {
const url = new URL('https://api.mymemory.translated.net/get');
url.searchParams.set('q', text.slice(0, 500));
url.searchParams.set('langpair', `auto|${targetLang}`);
const response = await fetch(url.toString());
if (!response.ok) {
return null;
}
const data = (await response.json()) as {
responseData?: { translatedText?: string };
};
return data.responseData?.translatedText ?? null;
}
const translateContextMenu: ContextMenuCommand = {
data: new ContextMenuCommandBuilder()
.setName('Translate')
.setType(ApplicationCommandType.Message),
async execute(interaction, context) {
const locale = await getGuildLocale(context.prisma, interaction.guildId);
const content = interaction.targetMessage.content;
if (!content?.trim()) {
await interaction.reply({ content: t(locale, 'utility.translate.noContent'), ephemeral: true });
return;
}
await interaction.deferReply({ ephemeral: true });
const targetLang = locale === 'de' ? 'de' : 'en';
const translated = await translateText(content, targetLang);
if (!translated) {
await interaction.editReply({ content: t(locale, 'utility.translate.failed') });
return;
}
await interaction.editReply({
content: t(locale, 'utility.translate.result').replace('{text}', translated)
});
}
};
export const utilityContextMenus: ContextMenuCommand[] = [translateContextMenu];