This commit is contained in:
2025-06-15 14:02:25 +09:00
parent af39a1bfa5
commit 4a3fd3feb8
3 changed files with 20 additions and 20 deletions

View File

@@ -77,7 +77,7 @@ class LibreTranslateAPI {
class ControllerPlugin extends BaseControllerPlugin { class ControllerPlugin extends BaseControllerPlugin {
async init() { async init() {
this.controller.config.on('fieldChanged', (field, curr, prev) => { this.controller.config.on('fieldChanged', (field, curr, prev) => {
if (field === 'ClusterChatSync.discord_bot_token') { if (field === 'chat_sync.discord_bot_token') {
this.connect().catch(err => { this.connect().catch(err => {
this.logger.error(`[Chat Sync] Discord bot token:\n${err.stack}`); this.logger.error(`[Chat Sync] Discord bot token:\n${err.stack}`);
}); });
@@ -98,7 +98,7 @@ class ControllerPlugin extends BaseControllerPlugin {
async connect() { async connect() {
await this.clientDestroy(); await this.clientDestroy();
if (!this.controller.config.get('ClusterChatSync.discord_bot_token')) { if (!this.controller.config.get('chat_sync.discord_bot_token')) {
this.logger.error('[Chat Sync] Discord bot token not configured.'); this.logger.error('[Chat Sync] Discord bot token not configured.');
return; return;
} }
@@ -107,7 +107,7 @@ class ControllerPlugin extends BaseControllerPlugin {
this.logger.info('[Chat Sync] Logging into Discord.'); this.logger.info('[Chat Sync] Logging into Discord.');
try { try {
await this.client.login(this.controller.config.get('ClusterChatSync.discord_bot_token')); await this.client.login(this.controller.config.get('chat_sync.discord_bot_token'));
} catch (err) { } catch (err) {
this.logger.error(`[Chat Sync] Discord login error:\n${err.stack}`); this.logger.error(`[Chat Sync] Discord login error:\n${err.stack}`);
await this.clientDestroy(); await this.clientDestroy();
@@ -116,10 +116,10 @@ class ControllerPlugin extends BaseControllerPlugin {
this.logger.info('[Chat Sync] Logged in Discord successfully.'); this.logger.info('[Chat Sync] Logged in Discord successfully.');
if (this.controller.config.get('ClusterChatSync.use_libretranslate')) { if (this.controller.config.get('chat_sync.use_libretranslate')) {
this.translator = new LibreTranslateAPI(this.controller.config.get('ClusterChatSync.libretranslate_url'), this.controller.config.get('ClusterChatSync.libretranslate_key'), this.logger); this.translator = new LibreTranslateAPI(this.controller.config.get('chat_sync.libretranslate_url'), this.controller.config.get('chat_sync.libretranslate_key'), this.logger);
await this.translator.init(); await this.translator.init();
this.translator_language = this.controller.config.get('ClusterChatSync.libretranslate_language').trim().split(/\s+/) || ['zh-Hant', 'en']; this.translator_language = this.controller.config.get('chat_sync.libretranslate_language').trim().split(/\s+/) || ['zh-Hant', 'en'];
} }
} }
@@ -128,7 +128,7 @@ class ControllerPlugin extends BaseControllerPlugin {
} }
async sendMessage(request, nrc_msg) { async sendMessage(request, nrc_msg) {
const channel_id = this.controller.config.get('ClusterChatSync.discord_channel_mapping')[request.instanceName]; const channel_id = this.controller.config.get('chat_sync.discord_channel_mapping')[request.instanceName];
if (!channel_id) return; if (!channel_id) return;
let channel; let channel;
@@ -145,7 +145,7 @@ class ControllerPlugin extends BaseControllerPlugin {
} }
} }
if (this.controller.config.get('ClusterChatSync.datetime_on_message')) { if (this.controller.config.get('chat_sync.datetime_on_message')) {
let now = new Date(); let now = new Date();
nrc_msg = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}${String(now.getSeconds()).padStart(2, '0')} ${nrc_msg}` nrc_msg = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}${String(now.getSeconds()).padStart(2, '0')} ${nrc_msg}`
} }
@@ -177,7 +177,7 @@ class ControllerPlugin extends BaseControllerPlugin {
const nrc_message = nrc.substring(nrc_index + 1).trim(); const nrc_message = nrc.substring(nrc_index + 1).trim();
await this.sendMessage(request, `**\`${nrc_username}\`**: ${nrc_message}`); await this.sendMessage(request, `**\`${nrc_username}\`**: ${nrc_message}`);
if (this.controller.config.get('ClusterChatSync.use_libretranslate')) { if (this.controller.config.get('chat_sync.use_libretranslate')) {
const result = await this.translator.translate(nrc_message, this.translator_language); const result = await this.translator.translate(nrc_message, this.translator_language);
if (result && result.action) { if (result && result.action) {

18
info.js
View File

@@ -5,7 +5,7 @@ class InstanceActionEvent {
static type = 'event'; static type = 'event';
static src = 'instance'; static src = 'instance';
static dst = 'controller'; static dst = 'controller';
static plugin = 'ClusterChatSync'; static plugin = 'chat_sync';
constructor(instanceName, action, content) { constructor(instanceName, action, content) {
this.instanceName = instanceName; this.instanceName = instanceName;
@@ -25,24 +25,24 @@ class InstanceActionEvent {
} }
const plugin = { const plugin = {
name: 'ClusterChatSync', name: 'chat_sync',
title: 'Cluster Chat Sync', title: 'Cluster Chat Sync',
description: 'One way chat sync.', description: 'One way chat sync.',
instanceEntrypoint: 'instance', instanceEntrypoint: 'instance',
controllerEntrypoint: 'controller', controllerEntrypoint: 'controller',
controllerConfigFields: { controllerConfigFields: {
'ClusterChatSync.discord_bot_token': { 'chat_sync.discord_bot_token': {
title: 'Discord Bot Token', title: 'Discord Bot Token',
description: 'API Token', description: 'API Token',
type: 'string' type: 'string'
}, },
'ClusterChatSync.datetime_on_message': { 'chat_sync.datetime_on_message': {
title: 'Message Datetime', title: 'Message Datetime',
description: 'Append datetime in front', description: 'Append datetime in front',
type: 'boolean', type: 'boolean',
initialValue: true initialValue: true
}, },
'ClusterChatSync.discord_channel_mapping': { 'chat_sync.discord_channel_mapping': {
title: 'Channels', title: 'Channels',
description: 'Putting the discord channel id and instance relations here', description: 'Putting the discord channel id and instance relations here',
type: 'object', type: 'object',
@@ -50,25 +50,25 @@ const plugin = {
'S1': '123' 'S1': '123'
}, },
}, },
'ClusterChatSync.use_libretranslate': { 'chat_sync.use_libretranslate': {
title: 'Translate Message', title: 'Translate Message',
description: 'Using self host or paid service of libretranslate', description: 'Using self host or paid service of libretranslate',
type: 'boolean', type: 'boolean',
initialValue: false initialValue: false
}, },
'ClusterChatSync.libretranslate_url': { 'chat_sync.libretranslate_url': {
title: 'Translate Server URL', title: 'Translate Server URL',
description: 'Including http protocol, and the port if needed', description: 'Including http protocol, and the port if needed',
type: 'string', type: 'string',
initialValue: 'http://localhost:5000' initialValue: 'http://localhost:5000'
}, },
'ClusterChatSync.libretranslate_key': { 'chat_sync.libretranslate_key': {
title: 'Translate Server API Key', title: 'Translate Server API Key',
description: 'The API key for the translate server', description: 'The API key for the translate server',
type: 'string', type: 'string',
initialValue: '123456' initialValue: '123456'
}, },
'ClusterChatSync.libretranslate_language': { 'chat_sync.libretranslate_language': {
title: 'Translate Server Target Language', title: 'Translate Server Target Language',
description: 'Put a space between each language, using ISO 639-1 codes', description: 'Put a space between each language, using ISO 639-1 codes',
type: 'string', type: 'string',

View File

@@ -13,8 +13,8 @@ module.exports = (env = {}) => merge(common(env), {
}, },
plugins: [ plugins: [
new webpack.container.ModuleFederationPlugin({ new webpack.container.ModuleFederationPlugin({
name: 'ClusterChatSync', name: 'chat_sync',
library: {type: 'var', name: 'plugin_ClusterChatSync' }, library: {type: 'var', name: 'plugin_chat_sync'},
exposes: { exposes: {
'./': './info.js', './': './info.js',
'./package.json': './package.json', './package.json': './package.json',