Agent now translates before saying.

This commit is contained in:
Vineeth 2024-08-26 17:34:50 -05:00 committed by GitHub
parent 2962f27272
commit 0616e20b6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,10 +2,38 @@ import * as skills from './library/skills.js';
import * as world from './library/world.js';
import * as mc from '../utils/mcdata.js';
import settings from '../../settings.js'
import translate from 'google-translate-api-x';
function say(agent, message) {
async function handleTranslation(message, lang) {
const preferred_lang = settings.preferred_language;
try {
if (preferred_lang.toLowerCase() == "en" || preferred_lang.toLowerCase() == "english"){
return message;
}
else{
lang = String(lang); // Ensure lang is a string
const translation = await translate(message, { to: lang });
return translation.text || message; // Ensure translation.text is a string
}
} catch (error) {
console.error('Error translating message:', error);
return message; // Fallback to the original message if translation fails
}
}
async function say(agent, message) {
const preferred_lang = settings.preferred_language;
if (agent.shut_up || !settings.narrate_behavior) return;
agent.bot.chat(message);
var translation = await handleTranslation(message, `${preferred_lang}`);
agent.bot.chat(translation);
}
// a mode is a function that is called every tick to respond immediately to the world