This commit is contained in:
2025-06-15 01:22:31 +09:00
parent 214fb75f98
commit 43a35e81bd
3 changed files with 55 additions and 87 deletions

View File

@@ -154,6 +154,4 @@ class ControllerPlugin extends BaseControllerPlugin {
} }
} }
module.exports = { module.exports = {ControllerPlugin};
ControllerPlugin
};

110
info.js
View File

@@ -1,11 +1,11 @@
"use strict"; 'use strict';
const lib = require("@clusterio/lib"); const lib = require('@clusterio/lib');
class InstanceActionEvent { 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 = 'ClusterChatSync';
constructor(instanceName, action, content) { constructor(instanceName, action, content) {
this.instanceName = instanceName; this.instanceName = instanceName;
@@ -13,79 +13,63 @@ class InstanceActionEvent {
this.content = content; this.content = content;
} }
static jsonSchema = { static jsonSchema = {type: 'object', required: ['instanceName', 'action', 'content'], properties: {'instanceName': {type: 'string'}, 'action': {type: 'string'}, 'content': {type: 'string'}},};
type: "object", static fromJSON(json) {return new this(json.instanceName, json.action, json.content);}
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 = { const plugin = {
name: "ClusterChatSync", name: 'ClusterChatSync',
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": { 'ClusterChatSync.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": { 'ClusterChatSync.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": { 'ClusterChatSync.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',
initialValue: { initialValue: {
"S1": "123" 'S1': '123'
}, },
}, },
"ClusterChatSync.use_libretranslate": { 'ClusterChatSync.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": { 'ClusterChatSync.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": { 'ClusterChatSync.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": { 'ClusterChatSync.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',
initialValue: "zh-Hants en" initialValue: 'zh-Hants en'
}, },
}, },
messages: [ messages: [InstanceActionEvent],
InstanceActionEvent
],
}; };
module.exports = { module.exports = {plugin, InstanceActionEvent};
plugin,
InstanceActionEvent
};

View File

@@ -1,33 +1,19 @@
"use strict"; "use strict";
const lib = require("@clusterio/lib"); const lib = require("@clusterio/lib");
const { BaseInstancePlugin } = require("@clusterio/host"); const {BaseInstancePlugin} = require("@clusterio/host");
const { InstanceActionEvent } = require("./info.js"); const {InstanceActionEvent} = require("./info.js");
class InstancePlugin extends BaseInstancePlugin { class InstancePlugin extends BaseInstancePlugin {
async init() { async init() {this.messageQueue = [];}
this.messageQueue = [];
}
onControllerConnectionEvent(event) { onControllerConnectionEvent(event) {
if (event === "connect") { if (event === 'connect') {
for (let [action, content] of this.messageQueue) { for (let [action, content] of this.messageQueue) {this.instance.sendTo('controller', new InstanceActionEvent(this.instance.name, action, content));}
this.instance.sendTo("controller", new InstanceActionEvent(this.instance.name, action, content));
}
this.messageQueue = []; this.messageQueue = [];
} }
} }
async onOutput(output) { async onOutput(output) {if (output.type == 'action') {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 (output.type == "action") {
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]);
}
}
}
} }
module.exports = { module.exports = {InstancePlugin};
InstancePlugin
};