diff --git a/controller.js b/controller.js index 06fc299..2246410 100644 --- a/controller.js +++ b/controller.js @@ -21,9 +21,29 @@ class LibreTranslateAPI { return response.json(); } - async init() {try {this.allowedLanguages = (await this.handleResponse(await fetch(`${this.url}languages?api_key=${this.apiKey}`, {method: 'GET'})))[0].targets || [];} catch (err) {this.logger.error(`[Chat Sync] failed to initialize languages:\n${err.stack}`);}} - async translateRequest(q, source, target) {try {return (await this.handleResponse(await fetch(`${this.url}translate`, {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({q: q, api_key: this.apiKey, source: source, target: target})}))).translatedText;} catch (err) {this.logger.error(`[Chat Sync] Translation failed:\n${err.stack}`);}} - async detectLanguage(q) {try {return (await this.handleResponse(await fetch(`${this.url}detect`, {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({q: q, api_key: this.apiKey})})))[0];} catch (err) {this.logger.error(`[Chat Sync] Detection failed:\n${err.stack}`);}} + async init() { + try { + this.allowedLanguages = (await this.handleResponse(await fetch(`${this.url}languages?api_key=${this.apiKey}`, {method: 'GET'})))?.[0]?.targets || []; + } catch (err) { + this.logger.error(`[Chat Sync] failed to initialize languages:\n${err.stack}`); + } + } + + async translateRequest(q, source, target) { + try { + return (await this.handleResponse(await fetch(`${this.url}translate`, {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({q: q, api_key: this.apiKey, source: source, target: target})})))?.translatedText; + } catch (err) { + this.logger.error(`[Chat Sync] Translation failed:\n${err.stack}`); + } + } + + async detectLanguage(q) { + try { + return (await this.handleResponse(await fetch(`${this.url}detect`, {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({q: q, api_key: this.apiKey})})))?.[0]; + } catch (err) { + this.logger.error(`[Chat Sync] Detection failed:\n${err.stack}`); + } + } async translate(query, targetLanguages = ['zh-Hant', 'en']) { console.log(query); @@ -48,7 +68,9 @@ class LibreTranslateAPI { } return result; - } catch (err) {this.logger.error(`[Chat Sync] translation failed:\n${err.stack}`);} + } catch (err) { + this.logger.error(`[Chat Sync] translation failed:\n${err.stack}`); + } } } @@ -68,7 +90,7 @@ class ControllerPlugin extends BaseControllerPlugin { } async connect() { - await this.clientDestroy() + await this.clientDestroy(); if (!this.controller.config.get('ClusterChatSync.discord_bot_token')) { this.logger.error('[Chat Sync] Discord bot token not configured.'); @@ -78,9 +100,7 @@ class ControllerPlugin extends BaseControllerPlugin { this.client = new Discord.Client({intents: [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMessages, Discord.GatewayIntentBits.MessageContent]}); this.logger.info('[Chat Sync] Logging into Discord.'); - try { - await this.client.login(this.controller.config.get('ClusterChatSync.discord_bot_token')); - } catch (err) { + try {await this.client.login(this.controller.config.get('ClusterChatSync.discord_bot_token'))} catch (err) { this.logger.error(`[Chat Sync] Discord login error:\n${err.stack}`); await this.clientDestroy() return; diff --git a/info.js b/info.js index ed0b013..a3652cc 100644 --- a/info.js +++ b/info.js @@ -13,8 +13,15 @@ class InstanceActionEvent { this.content = content; } - static jsonSchema = {type: 'object', required: ['instanceName', 'action', 'content'], properties: {'instanceName': {type: 'string'}, 'action': {type: 'string'}, 'content': {type: 'string'}}}; - static fromJSON(json) {return new this(json.instanceName, json.action, json.content);} + static jsonSchema = { + type: 'object', + required: ['instanceName', 'action', 'content'], + properties: {'instanceName': {type: 'string'}, 'action': {type: 'string'}, 'content': {type: 'string'}} + } + + static fromJSON(json) { + return new this(json.instanceName, json.action, json.content); + } } const plugin = { diff --git a/instance.js b/instance.js index ef5050e..5e2e83d 100644 --- a/instance.js +++ b/instance.js @@ -4,18 +4,30 @@ const {BaseInstancePlugin} = require("@clusterio/host"); const {InstanceActionEvent} = require("./info.js"); class InstancePlugin extends BaseInstancePlugin { - async init() {this.messageQueue = [];} + async init() { + this.messageQueue = []; + } onControllerConnectionEvent(event) { if (event === 'connect') { - for (const [action, content] of this.messageQueue) {try {this.instance.sendTo('controller', new InstanceActionEvent(this.instance.name, action, content))} catch (err) {this.messageQueue.push([output.action, output.message])}} + for (const [action, content] of this.messageQueue) { + try { + this.instance.sendTo('controller', new InstanceActionEvent(this.instance.name, action, content)); + } catch (err) { + this.messageQueue.push([output.action, output.message]); + } + } this.messageQueue = []; } } async onOutput(output) { if (output.type !== 'action') return; - if (this.host.connector.connected) {this.instance.sendTo('controller', new InstanceActionEvent(this.instance.name, output.action, output.message))} else {this.messageQueue.push([output.action, output.message])} + if (this.host.connector.connected) { + this.instance.sendTo('controller', new InstanceActionEvent(this.instance.name, output.action, output.message)); + } else { + this.messageQueue.push([output.action, output.message]); + } } } diff --git a/webpack.config.js b/webpack.config.js index 7689684..13fb006 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,27 +1,27 @@ -"use strict"; -const path = require("path"); -const webpack = require("webpack"); -const { merge } = require("webpack-merge"); +'use strict'; +const path = require('path'); +const webpack = require('webpack'); +const { merge } = require('webpack-merge'); -const common = require("@clusterio/web_ui/webpack.common"); +const common = require('@clusterio/web_ui/webpack.common'); module.exports = (env = {}) => merge(common(env), { context: __dirname, - entry: "./web/index.jsx", + entry: './web/index.jsx', output: { - path: path.resolve(__dirname, "dist", "web"), + path: path.resolve(__dirname, 'dist', 'web'), }, plugins: [ new webpack.container.ModuleFederationPlugin({ - name: "ClusterChatSync", - library: { type: "var", name: "plugin_ClusterChatSync" }, + name: 'ClusterChatSync', + library: {type: 'var', name: 'plugin_ClusterChatSync' }, exposes: { - "./": "./info.js", - "./package.json": "./package.json", + './': './info.js', + './package.json': './package.json', }, shared: { - "@clusterio/lib": { import: false }, - "@clusterio/web_ui": { import: false }, + '@clusterio/lib': {import: false}, + '@clusterio/web_ui': {import: false}, }, }), ],