From 6f24c14bd04ff50aa529dfe0a4d8185b1d29039d Mon Sep 17 00:00:00 2001 From: Vineeth <94882582+FantomWolf182@users.noreply.github.com> Date: Sun, 25 Aug 2024 14:05:44 -0500 Subject: [PATCH 01/15] Update settings.js --- settings.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/settings.js b/settings.js index 9c6bb34..815561f 100644 --- a/settings.js +++ b/settings.js @@ -1,12 +1,14 @@ export default { "minecraft_version": "1.20.4", // supports up to 1.20.4 - "host": "127.0.0.1", // or "localhost", "your.ip.address.here" + "host": "localhost", // or "localhost", "your.ip.address.here" "port": 55916, "auth": "offline", // or "microsoft" "profiles": [ - "./andy.json", + //"./andy.json", + "./profiles/dolphin.json" // Groq support, mixtral model + // add more profiles here, check ./profiles/ for more // more than 1 profile will require you to /msg each bot indivually ], @@ -19,4 +21,5 @@ export default "max_commands": -1, // max number of commands to use in a response. -1 for no limit "verbose_commands": true, // show full command syntax "narrate_behavior": true, // chat simple automatic actions ('Picking up item!') -} \ No newline at end of file + "preferred_language": "english", // the bot will respond/message in this language. Secondly all language names are based on google translate's names. +} From 600de3ae9de8220b25d5d9cdcf42be9421a84299 Mon Sep 17 00:00:00 2001 From: Vineeth <94882582+FantomWolf182@users.noreply.github.com> Date: Sun, 25 Aug 2024 14:06:57 -0500 Subject: [PATCH 02/15] Added translation functionality --- src/agent/agent.js | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/agent/agent.js b/src/agent/agent.js index 801e243..367498f 100644 --- a/src/agent/agent.js +++ b/src/agent/agent.js @@ -8,7 +8,8 @@ import { NPCContoller } from './npc/controller.js'; import { MemoryBank } from './memory_bank.js'; import { SelfPrompter } from './self_prompter.js'; import settings from '../../settings.js'; - +import translate from 'google-translate-api-x'; +const preferred_lang = settings.preferred_language; export class Agent { async start(profile_fp, load_mem=false, init_message=null) { @@ -48,16 +49,18 @@ export class Agent { "Gamerule " ]; const eventname = settings.profiles.length > 1 ? 'whisper' : 'chat'; - this.bot.on(eventname, (username, message) => { + this.bot.on(eventname, async (username, message) => { if (username === this.name) return; if (ignore_messages.some((m) => message.startsWith(m))) return; - console.log('received message from', username, ':', message); + var translation = await this.handleTranslation(message, "en"); + + console.log('received message from', username, ':', translation); this.shut_up = false; - this.handleMessage(username, message); + this.handleMessage(username, translation); }); // set the bot to automatically eat food when hungry @@ -77,7 +80,9 @@ export class Agent { this.handleMessage('system', init_message, 2); } else { - this.bot.chat('Hello world! I am ' + this.name); + const translation = await this.handleTranslation("Hello world! I am", `${preferred_lang}`); + print(translation) + this.bot.chat(translation+" "+this.name); this.bot.emit('finished_executing'); } @@ -85,10 +90,26 @@ export class Agent { }); } - cleanChat(message) { + async handleTranslation(message, lang) { + try { + 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) { // newlines are interpreted as separate chats, which triggers spam filters. replace them with spaces message = message.replaceAll('\n', ' '); - return this.bot.chat(message); + const preferred_lang = settings.preferred_language; + var translation = await this.handleTranslation(message, `${preferred_lang}`); + return this.bot.chat(translation); } shutUp() { @@ -280,4 +301,3 @@ export class Agent { process.exit(1); } } - From 50b75faa756f51c24fa9a7a56993d9d3fe722aa9 Mon Sep 17 00:00:00 2001 From: Vineeth <94882582+FantomWolf182@users.noreply.github.com> Date: Sun, 25 Aug 2024 14:14:08 -0500 Subject: [PATCH 03/15] Update settings.js --- settings.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/settings.js b/settings.js index 815561f..03b9b30 100644 --- a/settings.js +++ b/settings.js @@ -6,8 +6,7 @@ export default "auth": "offline", // or "microsoft" "profiles": [ - //"./andy.json", - "./profiles/dolphin.json" // Groq support, mixtral model + "./andy.json" // add more profiles here, check ./profiles/ for more // more than 1 profile will require you to /msg each bot indivually From c8e801b446ee7994571578299d78e9c92f0945d1 Mon Sep 17 00:00:00 2001 From: Vineeth <94882582+FantomWolf182@users.noreply.github.com> Date: Sun, 25 Aug 2024 17:30:28 -0500 Subject: [PATCH 04/15] New command !setPreferredLanguage --- src/agent/commands/actions.js | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/agent/commands/actions.js b/src/agent/commands/actions.js index 651f362..2bcaf51 100644 --- a/src/agent/commands/actions.js +++ b/src/agent/commands/actions.js @@ -1,5 +1,8 @@ import * as skills from '../library/skills.js'; import settings from '../../../settings.js'; +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath, pathToFileURL } from 'url'; function wrapExecution(func, timeout=-1, resume_name=null) { return async function (agent, ...args) { @@ -286,4 +289,38 @@ export const actionsList = [ return 'Set npc goal: ' + agent.npc.data.curr_goal.name; } }, + { + name: '!setPreferredLanguage', + description: 'Change the preferred language in settings.js to a specified language.', + params: { + 'language': '(string) The language code to set as the preferred language. Example Perameters: "english", "spanish", "french".' + }, + perform: async function (agent, language) { + try { + + const __filename = fileURLToPath(import.meta.url); + const __dirname = path.dirname(__filename); + const settingsPath = path.join(__dirname, '../../../settings.js'); + const settingsUrl = pathToFileURL(settingsPath).href; // Convert to file:// URL + const settingsModule = await import(settingsUrl); + const settings = settingsModule.default; + + + if (typeof language !== 'string' || !language.trim()) { + throw new Error('Invalid language code provided.'); + } + + + settings.preferred_language = language; + + fs.writeFileSync(settingsPath, `export default ${JSON.stringify(settings, null, 4)};`, 'utf8'); + + return `Preferred language changed to ${language}.`; + } catch (error) { + return `Error: ${error.message}`; + } + } +} + + ]; From 0616e20b6cd535e061321c209917bc39e7c65bd2 Mon Sep 17 00:00:00 2001 From: Vineeth <94882582+FantomWolf182@users.noreply.github.com> Date: Mon, 26 Aug 2024 17:34:50 -0500 Subject: [PATCH 05/15] Agent now translates before saying. --- src/agent/modes.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/agent/modes.js b/src/agent/modes.js index 952fcf0..12f1fe6 100644 --- a/src/agent/modes.js +++ b/src/agent/modes.js @@ -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 From 860e457d0c090fde3377be63bcdb72995b14552c Mon Sep 17 00:00:00 2001 From: Vineeth <94882582+FantomWolf182@users.noreply.github.com> Date: Mon, 26 Aug 2024 17:35:49 -0500 Subject: [PATCH 06/15] checks preferred_langauge is english before translating --- src/agent/agent.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/agent/agent.js b/src/agent/agent.js index 367498f..e9b9ad9 100644 --- a/src/agent/agent.js +++ b/src/agent/agent.js @@ -84,6 +84,7 @@ export class Agent { print(translation) this.bot.chat(translation+" "+this.name); this.bot.emit('finished_executing'); + } this.startEvents(); @@ -91,11 +92,20 @@ export class Agent { } async handleTranslation(message, lang) { - try { - 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 + 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 From da2e865fb9349796f57f4bdd366adff8b04183af Mon Sep 17 00:00:00 2001 From: Vineeth <94882582+FantomWolf182@users.noreply.github.com> Date: Mon, 26 Aug 2024 17:46:18 -0500 Subject: [PATCH 07/15] Update package.json. Added google-translate module --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 504a91e..82c1ffe 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "dependencies": { "@anthropic-ai/sdk": "^0.17.1", "@google/generative-ai": "^0.2.1", + "google-translate-api-x": "^10.7.1", "minecraft-data": "^3.46.2", "mineflayer": "^4.20.0", "mineflayer-armor-manager": "^2.0.1", From e90d0e91ad48407dd949ff628d1dfa1e54201abd Mon Sep 17 00:00:00 2001 From: Vineeth <94882582+FantomWolf182@users.noreply.github.com> Date: Thu, 29 Aug 2024 19:16:28 -0500 Subject: [PATCH 08/15] changed to original with one change --- settings.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/settings.js b/settings.js index 03b9b30..0372dea 100644 --- a/settings.js +++ b/settings.js @@ -1,12 +1,12 @@ export default { "minecraft_version": "1.20.4", // supports up to 1.20.4 - "host": "localhost", // or "localhost", "your.ip.address.here" + "host": "127.0.0.1", // or "localhost", "your.ip.address.here" "port": 55916, "auth": "offline", // or "microsoft" "profiles": [ - "./andy.json" + "./andy.json", // add more profiles here, check ./profiles/ for more // more than 1 profile will require you to /msg each bot indivually @@ -20,5 +20,5 @@ export default "max_commands": -1, // max number of commands to use in a response. -1 for no limit "verbose_commands": true, // show full command syntax "narrate_behavior": true, // chat simple automatic actions ('Picking up item!') - "preferred_language": "english", // the bot will respond/message in this language. Secondly all language names are based on google translate's names. + "language": "spanish", // the bot will respond/message in this language. All language names are based on google translate's names. } From 6865eae455c90ef87c602b3cb982a243f5647347 Mon Sep 17 00:00:00 2001 From: Vineeth <94882582+FantomWolf182@users.noreply.github.com> Date: Thu, 29 Aug 2024 19:18:21 -0500 Subject: [PATCH 09/15] Reworked system to work with translator.js --- src/agent/modes.js | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/src/agent/modes.js b/src/agent/modes.js index 12f1fe6..6b7e9c8 100644 --- a/src/agent/modes.js +++ b/src/agent/modes.js @@ -2,29 +2,7 @@ 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'; - - -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 - } - } +import { handleTranslation, handleEnglishTranslation } from './translator.js'; @@ -32,7 +10,7 @@ async function handleTranslation(message, lang) { async function say(agent, message) { const preferred_lang = settings.preferred_language; if (agent.shut_up || !settings.narrate_behavior) return; - var translation = await handleTranslation(message, `${preferred_lang}`); + var translation = await handleTranslation(message); agent.bot.chat(translation); } From 0bac79e8aa7fe1fac3733d65b394475b1f2f7442 Mon Sep 17 00:00:00 2001 From: Vineeth <94882582+FantomWolf182@users.noreply.github.com> Date: Thu, 29 Aug 2024 19:18:51 -0500 Subject: [PATCH 10/15] Reworked system to work with translator.js --- src/agent/agent.js | 32 ++++---------------------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/src/agent/agent.js b/src/agent/agent.js index e9b9ad9..87cda62 100644 --- a/src/agent/agent.js +++ b/src/agent/agent.js @@ -8,8 +8,7 @@ import { NPCContoller } from './npc/controller.js'; import { MemoryBank } from './memory_bank.js'; import { SelfPrompter } from './self_prompter.js'; import settings from '../../settings.js'; -import translate from 'google-translate-api-x'; -const preferred_lang = settings.preferred_language; +import { handleTranslation, handleEnglishTranslation } from './translator.js'; export class Agent { 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; - var translation = await this.handleTranslation(message, "en"); + let translation = await handleEnglishTranslation(message); console.log('received message from', username, ':', translation); @@ -80,8 +79,7 @@ export class Agent { this.handleMessage('system', init_message, 2); } else { - const translation = await this.handleTranslation("Hello world! I am", `${preferred_lang}`); - print(translation) + const translation = await handleTranslation("Hello world! I am"); this.bot.chat(translation+" "+this.name); 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) { // newlines are interpreted as separate chats, which triggers spam filters. replace them with spaces message = message.replaceAll('\n', ' '); const preferred_lang = settings.preferred_language; - var translation = await this.handleTranslation(message, `${preferred_lang}`); + let translation = await handleTranslation(message); return this.bot.chat(translation); } From 4165fe2d356f7557fb33aed032f6cc92aab3d304 Mon Sep 17 00:00:00 2001 From: Vineeth <94882582+FantomWolf182@users.noreply.github.com> Date: Thu, 29 Aug 2024 19:20:16 -0500 Subject: [PATCH 11/15] Add files via upload --- src/agent/translator.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/agent/translator.js diff --git a/src/agent/translator.js b/src/agent/translator.js new file mode 100644 index 0000000..0a5ad45 --- /dev/null +++ b/src/agent/translator.js @@ -0,0 +1,32 @@ +import translate from 'google-translate-api-x'; +import settings from '../../settings.js'; + + +const preferred_lang = settings.language; + + +export async function handleTranslation(message) { + try { + if (preferred_lang.toLowerCase() === 'en' || preferred_lang.toLowerCase() === 'english') { + return message; + } else { + const lang = String(preferred_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 + } +} + +export async function handleEnglishTranslation(message) { + try { + const translation = await translate(message, { to: 'english' }); + return translation.text || message; // Ensures translation.text is a string + } catch (error) { + console.error('Error translating message:', error); + return message; // Fallback to the original message if translation fails + } +} From 6b170e492fdbf26f34cbe22d81e096bb558ebb85 Mon Sep 17 00:00:00 2001 From: Vineeth <94882582+FantomWolf182@users.noreply.github.com> Date: Thu, 29 Aug 2024 19:31:22 -0500 Subject: [PATCH 12/15] removed !setPreferredLanguage --- src/agent/commands/actions.js | 36 ----------------------------------- 1 file changed, 36 deletions(-) diff --git a/src/agent/commands/actions.js b/src/agent/commands/actions.js index 2bcaf51..e48b8a6 100644 --- a/src/agent/commands/actions.js +++ b/src/agent/commands/actions.js @@ -1,8 +1,5 @@ import * as skills from '../library/skills.js'; import settings from '../../../settings.js'; -import fs from 'fs'; -import path from 'path'; -import { fileURLToPath, pathToFileURL } from 'url'; function wrapExecution(func, timeout=-1, resume_name=null) { return async function (agent, ...args) { @@ -288,39 +285,6 @@ export const actionsList = [ agent.bot.emit('idle'); // to trigger the goal return 'Set npc goal: ' + agent.npc.data.curr_goal.name; } - }, - { - name: '!setPreferredLanguage', - description: 'Change the preferred language in settings.js to a specified language.', - params: { - 'language': '(string) The language code to set as the preferred language. Example Perameters: "english", "spanish", "french".' - }, - perform: async function (agent, language) { - try { - - const __filename = fileURLToPath(import.meta.url); - const __dirname = path.dirname(__filename); - const settingsPath = path.join(__dirname, '../../../settings.js'); - const settingsUrl = pathToFileURL(settingsPath).href; // Convert to file:// URL - const settingsModule = await import(settingsUrl); - const settings = settingsModule.default; - - - if (typeof language !== 'string' || !language.trim()) { - throw new Error('Invalid language code provided.'); - } - - - settings.preferred_language = language; - - fs.writeFileSync(settingsPath, `export default ${JSON.stringify(settings, null, 4)};`, 'utf8'); - - return `Preferred language changed to ${language}.`; - } catch (error) { - return `Error: ${error.message}`; - } } -} - ]; From 166c01c52bdab8fc6a2088c115182166b5769342 Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Wed, 2 Oct 2024 00:57:28 -0500 Subject: [PATCH 13/15] cleaned up stuff --- settings.js | 4 +- src/agent/agent.js | 25 +++++++----- src/agent/commands/actions.js | 3 +- src/agent/modes.js | 5 +-- src/{agent => utils}/translator.js | 62 +++++++++++++++--------------- 5 files changed, 49 insertions(+), 50 deletions(-) rename src/{agent => utils}/translator.js (59%) diff --git a/settings.js b/settings.js index 0372dea..7182f0a 100644 --- a/settings.js +++ b/settings.js @@ -7,18 +7,18 @@ export default "profiles": [ "./andy.json", - // add more profiles here, check ./profiles/ for more // more than 1 profile will require you to /msg each bot indivually ], "load_memory": false, // load memory from previous session "init_message": "Say hello world and your name", // sends to all on spawn + "language": "en", // translate to/from this language. Supports these language names: https://cloud.google.com/translate/docs/languages + "allow_insecure_coding": false, // allows newAction command and model can write/run code on your computer. enable at own risk "code_timeout_mins": 10, // minutes code is allowed to run. -1 for no timeout "max_commands": -1, // max number of commands to use in a response. -1 for no limit "verbose_commands": true, // show full command syntax "narrate_behavior": true, // chat simple automatic actions ('Picking up item!') - "language": "spanish", // the bot will respond/message in this language. All language names are based on google translate's names. } diff --git a/src/agent/agent.js b/src/agent/agent.js index 87cda62..ee8a19d 100644 --- a/src/agent/agent.js +++ b/src/agent/agent.js @@ -8,7 +8,7 @@ import { NPCContoller } from './npc/controller.js'; import { MemoryBank } from './memory_bank.js'; import { SelfPrompter } from './self_prompter.js'; import settings from '../../settings.js'; -import { handleTranslation, handleEnglishTranslation } from './translator.js'; +import { handleTranslation, handleEnglishTranslation } from '../utils/translator.js'; export class Agent { async start(profile_fp, load_mem=false, init_message=null) { @@ -79,8 +79,8 @@ export class Agent { this.handleMessage('system', init_message, 2); } else { - const translation = await handleTranslation("Hello world! I am"); - this.bot.chat(translation+" "+this.name); + const translation = await handleTranslation("Hello world! I am "+this.name); + this.bot.chat(translation); this.bot.emit('finished_executing'); } @@ -90,12 +90,17 @@ export class Agent { } - async cleanChat(message) { + async cleanChat(message, translate_up_to=-1) { + let to_translate = message; + let remainging = ''; + if (translate_up_to != -1) { + to_translate = to_translate.substring(0, translate_up_to); + remainging = message.substring(translate_up_to); + } + message = (await handleTranslation(to_translate)).trim() + " " + remainging; // newlines are interpreted as separate chats, which triggers spam filters. replace them with spaces - message = message.replaceAll('\n', ' '); - const preferred_lang = settings.preferred_language; - let translation = await handleTranslation(message); - return this.bot.chat(translation); + message = message.replaceAll('\n', ' '); + return this.bot.chat(message); } shutUp() { @@ -165,10 +170,10 @@ export class Agent { this.self_prompter.handleUserPromptedCmd(self_prompt, isAction(command_name)); if (settings.verbose_commands) { - this.cleanChat(res); + this.cleanChat(res, res.indexOf(command_name)); } else { // only output command name - let pre_message = res.substring(0, res.indexOf(command_name)).trim(); + let pre_message = res.substring(0, res.indexOf(command_name)).trim(); let chat_message = `*used ${command_name.substring(1)}*`; if (pre_message.length > 0) chat_message = `${pre_message} ${chat_message}`; diff --git a/src/agent/commands/actions.js b/src/agent/commands/actions.js index e48b8a6..651f362 100644 --- a/src/agent/commands/actions.js +++ b/src/agent/commands/actions.js @@ -285,6 +285,5 @@ export const actionsList = [ agent.bot.emit('idle'); // to trigger the goal return 'Set npc goal: ' + agent.npc.data.curr_goal.name; } - } - + }, ]; diff --git a/src/agent/modes.js b/src/agent/modes.js index 6b7e9c8..5a7b4d8 100644 --- a/src/agent/modes.js +++ b/src/agent/modes.js @@ -2,13 +2,10 @@ 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 { handleTranslation, handleEnglishTranslation } from './translator.js'; - - +import { handleTranslation } from '../utils/translator.js'; async function say(agent, message) { - const preferred_lang = settings.preferred_language; if (agent.shut_up || !settings.narrate_behavior) return; var translation = await handleTranslation(message); agent.bot.chat(translation); diff --git a/src/agent/translator.js b/src/utils/translator.js similarity index 59% rename from src/agent/translator.js rename to src/utils/translator.js index 0a5ad45..879cfe4 100644 --- a/src/agent/translator.js +++ b/src/utils/translator.js @@ -1,32 +1,30 @@ -import translate from 'google-translate-api-x'; -import settings from '../../settings.js'; - - -const preferred_lang = settings.language; - - -export async function handleTranslation(message) { - try { - if (preferred_lang.toLowerCase() === 'en' || preferred_lang.toLowerCase() === 'english') { - return message; - } else { - const lang = String(preferred_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 - } -} - -export async function handleEnglishTranslation(message) { - try { - const translation = await translate(message, { to: 'english' }); - return translation.text || message; // Ensures translation.text is a string - } catch (error) { - console.error('Error translating message:', error); - return message; // Fallback to the original message if translation fails - } -} +import translate from 'google-translate-api-x'; +import settings from '../../settings.js'; + +const preferred_lang = settings.language; + +export async function handleTranslation(message) { + try { + if (preferred_lang.toLowerCase() === 'en' || preferred_lang.toLowerCase() === 'english') { + return message; + } else { + const lang = String(preferred_lang); + + const translation = await translate(message, { to: lang }); + return translation.text || message; + } + } catch (error) { + console.error('Error translating message:', error); + return message; + } +} + +export async function handleEnglishTranslation(message) { + try { + const translation = await translate(message, { to: 'english' }); + return translation.text || message; + } catch (error) { + console.error('Error translating message:', error); + return message; + } +} From 2490dc302c684fe0e9d3abd695ff460100410304 Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Wed, 2 Oct 2024 01:01:22 -0500 Subject: [PATCH 14/15] cleaned formatting --- src/agent/agent.js | 19 +++++++++---------- src/agent/modes.js | 2 +- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/agent/agent.js b/src/agent/agent.js index ee8a19d..644ced0 100644 --- a/src/agent/agent.js +++ b/src/agent/agent.js @@ -79,10 +79,9 @@ export class Agent { this.handleMessage('system', init_message, 2); } else { - const translation = await handleTranslation("Hello world! I am "+this.name); + const translation = await handleTranslation("Hello world! I am "+this.name); this.bot.chat(translation); this.bot.emit('finished_executing'); - } this.startEvents(); @@ -91,13 +90,13 @@ export class Agent { async cleanChat(message, translate_up_to=-1) { - let to_translate = message; - let remainging = ''; - if (translate_up_to != -1) { - to_translate = to_translate.substring(0, translate_up_to); - remainging = message.substring(translate_up_to); - } - message = (await handleTranslation(to_translate)).trim() + " " + remainging; + let to_translate = message; + let remainging = ''; + if (translate_up_to != -1) { + to_translate = to_translate.substring(0, translate_up_to); + remainging = message.substring(translate_up_to); + } + message = (await handleTranslation(to_translate)).trim() + " " + remainging; // newlines are interpreted as separate chats, which triggers spam filters. replace them with spaces message = message.replaceAll('\n', ' '); return this.bot.chat(message); @@ -173,7 +172,7 @@ export class Agent { this.cleanChat(res, res.indexOf(command_name)); } else { // only output command name - let pre_message = res.substring(0, res.indexOf(command_name)).trim(); + let pre_message = res.substring(0, res.indexOf(command_name)).trim(); let chat_message = `*used ${command_name.substring(1)}*`; if (pre_message.length > 0) chat_message = `${pre_message} ${chat_message}`; diff --git a/src/agent/modes.js b/src/agent/modes.js index 5a7b4d8..4dbcc34 100644 --- a/src/agent/modes.js +++ b/src/agent/modes.js @@ -7,7 +7,7 @@ import { handleTranslation } from '../utils/translator.js'; async function say(agent, message) { if (agent.shut_up || !settings.narrate_behavior) return; - var translation = await handleTranslation(message); + let translation = await handleTranslation(message); agent.bot.chat(translation); } From 895fe5ba711c6c38fa0d2a41b9bbf43fff535843 Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Wed, 2 Oct 2024 01:08:26 -0500 Subject: [PATCH 15/15] newline --- settings.js | 1 + 1 file changed, 1 insertion(+) diff --git a/settings.js b/settings.js index 7182f0a..6e29750 100644 --- a/settings.js +++ b/settings.js @@ -22,3 +22,4 @@ export default "verbose_commands": true, // show full command syntax "narrate_behavior": true, // chat simple automatic actions ('Picking up item!') } +