mirror of
https://github.com/PHIDIAS0303/ClusterChatSync.git
synced 2026-11-11 15:14:52 +09:00
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import type {InstancePluginContext} from '@clusterio/host';
|
|
import {ChatEvent} from './index.js';
|
|
|
|
export default async function(context: InstancePluginContext) {
|
|
const {instance, host, plugin} = context;
|
|
let queue: [string, string][] = [];
|
|
|
|
instance.hooks.controllerConnectionEvent.attach(plugin.name, async (event) => {
|
|
if (event !== 'connect' || queue.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const pending = queue;
|
|
queue = [];
|
|
const failed: [string, string][] = [];
|
|
|
|
for (const [action, content] of pending) {
|
|
try {
|
|
await instance.sendTo('controller', new ChatEvent(instance.name, action, content));
|
|
} catch (err) {
|
|
failed.push([action, content]);
|
|
}
|
|
}
|
|
|
|
queue = failed;
|
|
});
|
|
|
|
instance.hooks.output.attach(plugin.name, async (output) => {
|
|
if (output.type !== 'action') {
|
|
return;
|
|
}
|
|
|
|
if (host.connector.connected) {
|
|
try {
|
|
await instance.sendTo('controller', new ChatEvent(instance.name, output.action, output.message));
|
|
} catch (err) {
|
|
queue.push([output.action, output.message]);
|
|
}
|
|
} else {
|
|
queue.push([output.action, output.message]);
|
|
}
|
|
});
|
|
|
|
instance.hooks.controllerConnectionEvent.attach(plugin.name, async (event) => {
|
|
|
|
});
|
|
}
|