From 62eb790b0b2ba5815074182e892d0422b8b3c6ea Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 30 May 2024 22:03:51 +0300 Subject: [PATCH] Add /translate command --- public/scripts/extensions/translate/index.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/public/scripts/extensions/translate/index.js b/public/scripts/extensions/translate/index.js index dbc8ab587..20519db5b 100644 --- a/public/scripts/extensions/translate/index.js +++ b/public/scripts/extensions/translate/index.js @@ -12,6 +12,9 @@ import { } from '../../../script.js'; import { extension_settings, getContext } from '../../extensions.js'; import { findSecret, secret_state, writeSecret } from '../../secrets.js'; +import { SlashCommand } from '../../slash-commands/SlashCommand.js'; +import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../slash-commands/SlashCommandArgument.js'; +import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; import { splitRecursive } from '../../utils.js'; export const autoModeOptions = { @@ -649,4 +652,21 @@ jQuery(() => { eventSource.on(event_types.MESSAGE_UPDATED, handleMessageEdit); document.body.classList.add('translate'); + + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'translate', + helpString: 'Translate text to a target language. If target language is not provided, the value from the extension settings will be used.', + namedArgumentList: [ + new SlashCommandNamedArgument('target', 'The target language code to translate to', ARGUMENT_TYPE.STRING, false, false, '', Object.values(languageCodes)), + ], + unnamedArgumentList: [ + new SlashCommandArgument('The text to translate', ARGUMENT_TYPE.STRING, true, false, ''), + ], + callback: async (args, value) => { + const target = args?.target && Object.values(languageCodes).includes(String(args.target)) + ? String(args.target) + : extension_settings.translate.target_language; + return await translate(String(value), target); + }, + })); });