mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-06-07 01:35:54 +02:00
Reworked system to work with translator.js
This commit is contained in:
parent
6865eae455
commit
0bac79e8aa
1 changed files with 4 additions and 28 deletions
|
@ -8,8 +8,7 @@ import { NPCContoller } from './npc/controller.js';
|
||||||
import { MemoryBank } from './memory_bank.js';
|
import { MemoryBank } from './memory_bank.js';
|
||||||
import { SelfPrompter } from './self_prompter.js';
|
import { SelfPrompter } from './self_prompter.js';
|
||||||
import settings from '../../settings.js';
|
import settings from '../../settings.js';
|
||||||
import translate from 'google-translate-api-x';
|
import { handleTranslation, handleEnglishTranslation } from './translator.js';
|
||||||
const preferred_lang = settings.preferred_language;
|
|
||||||
|
|
||||||
export class Agent {
|
export class Agent {
|
||||||
async start(profile_fp, load_mem=false, init_message=null) {
|
async start(profile_fp, load_mem=false, init_message=null) {
|
||||||
|
@ -54,7 +53,7 @@ export class Agent {
|
||||||
|
|
||||||
if (ignore_messages.some((m) => message.startsWith(m))) return;
|
if (ignore_messages.some((m) => message.startsWith(m))) return;
|
||||||
|
|
||||||
var translation = await this.handleTranslation(message, "en");
|
let translation = await handleEnglishTranslation(message);
|
||||||
|
|
||||||
console.log('received message from', username, ':', translation);
|
console.log('received message from', username, ':', translation);
|
||||||
|
|
||||||
|
@ -80,8 +79,7 @@ export class Agent {
|
||||||
this.handleMessage('system', init_message, 2);
|
this.handleMessage('system', init_message, 2);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const translation = await this.handleTranslation("Hello world! I am", `${preferred_lang}`);
|
const translation = await handleTranslation("Hello world! I am");
|
||||||
print(translation)
|
|
||||||
this.bot.chat(translation+" "+this.name);
|
this.bot.chat(translation+" "+this.name);
|
||||||
this.bot.emit('finished_executing');
|
this.bot.emit('finished_executing');
|
||||||
|
|
||||||
|
@ -91,34 +89,12 @@ export class Agent {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async 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 cleanChat(message) {
|
async cleanChat(message) {
|
||||||
// newlines are interpreted as separate chats, which triggers spam filters. replace them with spaces
|
// newlines are interpreted as separate chats, which triggers spam filters. replace them with spaces
|
||||||
message = message.replaceAll('\n', ' ');
|
message = message.replaceAll('\n', ' ');
|
||||||
const preferred_lang = settings.preferred_language;
|
const preferred_lang = settings.preferred_language;
|
||||||
var translation = await this.handleTranslation(message, `${preferred_lang}`);
|
let translation = await handleTranslation(message);
|
||||||
return this.bot.chat(translation);
|
return this.bot.chat(translation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue