mirror of
https://github.com/PHIDIAS0303/ClusterChatSync.git
synced 2026-11-24 08:13:12 +09:00
1.0.4 RC
This commit is contained in:
1 parent
0eff19b29c
commit
a27aa94108
3 files changed
+81
-88
No files matched your search
+9
-47
@@ -3,12 +3,6 @@ import {ChatEvent} from './index.js';
|
|||||||
import * as Discord from 'discord.js';
|
import * as Discord from 'discord.js';
|
||||||
|
|
||||||
const MAX_DISCORD_MESSAGE_LENGTH = 1950;
|
const MAX_DISCORD_MESSAGE_LENGTH = 1950;
|
||||||
const ACTION_VERBS: Record<string, string> = {
|
|
||||||
JOIN: 'joined the game',
|
|
||||||
LEAVE: 'left the game',
|
|
||||||
KICK: 'was kicked from the game',
|
|
||||||
BAN: 'was banned from the game',
|
|
||||||
};
|
|
||||||
|
|
||||||
export default async function(context: ControllerPluginContext) {
|
export default async function(context: ControllerPluginContext) {
|
||||||
const {controller, logger, plugin} = context;
|
const {controller, logger, plugin} = context;
|
||||||
@@ -50,11 +44,11 @@ export default async function(context: ControllerPluginContext) {
|
|||||||
logger.info('[Chat Relay] Logged in Discord successfully.');
|
logger.info('[Chat Relay] Logged in Discord successfully.');
|
||||||
};
|
};
|
||||||
|
|
||||||
const sendMessage = async (instanceName: string, message: string) => {
|
const sendMessage = async (instance_name: string, datetime: string, message: string) => {
|
||||||
if (!client) return;
|
if (!client) return;
|
||||||
|
|
||||||
const mapping = controller.config.get('chat_relay.discord_channel_mapping') as Record<string, string>;
|
const mapping = controller.config.get('chat_relay.discord_channel_mapping') as Record<string, string>;
|
||||||
const channelId = mapping?.[instanceName];
|
const channelId = mapping?.[instance_name];
|
||||||
if (!channelId) return;
|
if (!channelId) return;
|
||||||
|
|
||||||
let channel: Discord.Channel | null;
|
let channel: Discord.Channel | null;
|
||||||
@@ -73,23 +67,20 @@ export default async function(context: ControllerPluginContext) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let nrcMsg = message;
|
let msg = message;
|
||||||
|
|
||||||
if (controller.config.get('chat_relay.datetime_on_message')) {
|
if (controller.config.get('chat_relay.datetime_on_message')) {
|
||||||
const now = new Date();
|
msg = `${datetime} ${msg}`;
|
||||||
const p = (n: number) => String(n).padStart(2, '0');
|
|
||||||
const stamp = `${now.getFullYear()}${p(now.getMonth() + 1)}${p(now.getDate())} ${p(now.getHours())}${p(now.getMinutes())}${p(now.getSeconds())}`;
|
|
||||||
nrcMsg = `${stamp} ${nrcMsg}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const send = (content: string) => channel.send({content, allowedMentions: {parse: []}});
|
const send = (content: string) => channel.send({content, allowedMentions: {parse: []}});
|
||||||
|
|
||||||
if (nrcMsg.length <= MAX_DISCORD_MESSAGE_LENGTH) {
|
if (msg.length <= MAX_DISCORD_MESSAGE_LENGTH) {
|
||||||
await send(nrcMsg);
|
await send(msg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let remaining = nrcMsg;
|
let remaining = msg;
|
||||||
|
|
||||||
while (remaining.length > 0) {
|
while (remaining.length > 0) {
|
||||||
let chunk = remaining.slice(0, MAX_DISCORD_MESSAGE_LENGTH);
|
let chunk = remaining.slice(0, MAX_DISCORD_MESSAGE_LENGTH);
|
||||||
@@ -107,37 +98,8 @@ export default async function(context: ControllerPluginContext) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
controller.handle(ChatEvent, async (request: ChatEvent) => {
|
controller.handle(ChatEvent, async (request: ChatEvent) => {
|
||||||
const {instanceId, instanceName, action, content} = request;
|
const {instance_name, datetime, message} = request;
|
||||||
|
await sendMessage(instance_name, datetime, message);
|
||||||
if (action === 'CHAT' || action === 'SHOUT') {
|
|
||||||
const nrc = content.replace(/\[special-item=.*?\]/g, '<blueprint>').replace(/<@/g, '<@\u200c>');
|
|
||||||
const index = nrc.indexOf(':');
|
|
||||||
const username = index === -1 ? nrc.trim() : nrc.slice(0, index);
|
|
||||||
const msg = index === -1 ? '' : nrc.slice(index + 1).trim();
|
|
||||||
await sendMessage(instanceName, `**\`${username}\`**: ${msg}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const verb = ACTION_VERBS[action];
|
|
||||||
const text = content.trim();
|
|
||||||
|
|
||||||
if (!verb) {
|
|
||||||
await sendMessage(instanceName, `unknown action ${action} **\`${text}\`**`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const name = text.toLowerCase().endsWith(verb)
|
|
||||||
? text.slice(0, -verb.length).trim()
|
|
||||||
: text;
|
|
||||||
|
|
||||||
const instance = await controller.instances.get(instanceId);
|
|
||||||
|
|
||||||
if (!instance) {
|
|
||||||
await sendMessage(instanceName, `**\`${name}\`** ${verb}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await sendMessage(instanceName, `**\`${name}\`** ${verb} (${instance.playersOnline.size})`);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
controller.config.on('fieldChanged', (field) => {
|
controller.config.on('fieldChanged', (field) => {
|
||||||
|
|||||||
@@ -11,21 +11,19 @@ export class ChatEvent {
|
|||||||
static permission = null;
|
static permission = null;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public instanceId: number,
|
public instance_name: string,
|
||||||
public instanceName: string,
|
public datetime: string,
|
||||||
public action: string,
|
public message: string,
|
||||||
public content: string,
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
static jsonSchema = Type.Object({
|
static json_schema = Type.Object({
|
||||||
'instanceId': Type.Integer(),
|
'instance_name': Type.String(),
|
||||||
'instanceName': Type.String(),
|
'datetime': Type.String(),
|
||||||
'action': Type.String(),
|
'message': Type.String(),
|
||||||
'content': Type.String(),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
static fromJSON(json: Static<typeof ChatEvent.jsonSchema>) {
|
static fromJSON(json: Static<typeof ChatEvent.json_schema>) {
|
||||||
return new this(json.instanceId, json.instanceName, json.action, json.content);
|
return new this(json.instance_name, json.datetime, json.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+63
-30
@@ -1,43 +1,76 @@
|
|||||||
import type {InstancePluginContext} from '@clusterio/host';
|
import type {InstancePluginContext} from '@clusterio/host';
|
||||||
import {ChatEvent} from './index.js';
|
import {ChatEvent} from './index.js';
|
||||||
|
|
||||||
|
interface ChatMessage {
|
||||||
|
action: string;
|
||||||
|
content: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ACTION_VERBS: Record<string, string> = {
|
||||||
|
JOIN: 'joined the game',
|
||||||
|
LEAVE: 'left the game',
|
||||||
|
KICK: 'was kicked from the game',
|
||||||
|
BAN: 'was banned from the game',
|
||||||
|
};
|
||||||
|
|
||||||
export default async function(context: InstancePluginContext) {
|
export default async function(context: InstancePluginContext) {
|
||||||
const {instance, host, plugin} = context;
|
const {instance, host, plugin, logger} = context;
|
||||||
let queue: [string, string][] = [];
|
|
||||||
|
|
||||||
instance.hooks.controllerConnectionEvent.attach(plugin.name, async (event) => {
|
const queue: ChatMessage[] = [];
|
||||||
if (event !== 'connect' || queue.length === 0) {
|
let flushing = false;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const pending = queue;
|
const flush = async () => {
|
||||||
queue = [];
|
if (flushing || queue.length === 0 || !host.connector.connected) {
|
||||||
const failed: [string, string][] = [];
|
return;
|
||||||
|
|
||||||
for (const [action, content] of pending) {
|
|
||||||
try {
|
|
||||||
await instance.sendTo('controller', new ChatEvent(instance.id, instance.name, action, content));
|
|
||||||
} catch (err) {
|
|
||||||
failed.push([action, content]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
flushing = true;
|
||||||
|
try {
|
||||||
|
while (queue.length > 0 && host.connector.connected) {
|
||||||
|
try {
|
||||||
|
await instance.sendTo('controller', new ChatEvent(instance.name, queue[0].datetime, queue[0].message));
|
||||||
|
queue.shift();
|
||||||
|
} catch (err) {
|
||||||
|
logger.warn(`[Chat Relay] Failed to send chat to controller: ${err}`);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
flushing = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
queue = failed;
|
instance.hooks.controllerConnectionEvent.attach(plugin.name, async (event) => {
|
||||||
});
|
if (event === 'connect') {
|
||||||
|
await flush();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
instance.hooks.output.attach(plugin.name, async (output) => {
|
instance.hooks.output.attach(plugin.name, async (output) => {
|
||||||
if (output.type !== 'action') {
|
if (output.type !== 'action') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (host.connector.connected) {
|
const now = new Date();
|
||||||
try {
|
const p = (n: number) => String(n).padStart(2, '0');
|
||||||
await instance.sendTo('controller', new ChatEvent(instance.id, instance.name, output.action, output.message));
|
const dt = `${now.getFullYear()}${p(now.getMonth() + 1)}${p(now.getDate())} ${p(now.getHours())}${p(now.getMinutes())}${p(now.getSeconds())}`;
|
||||||
} catch (err) {
|
|
||||||
queue.push([output.action, output.message]);
|
if (output.action === 'CHAT' || output.action === 'SHOUT') {
|
||||||
}
|
const message = output.message.trim().replace(/\[special-item=.*?\]/g, '<blueprint>').replace(/<@/g, '<@\u200c>');
|
||||||
} else {
|
const message_index = message.indexOf(':');
|
||||||
queue.push([output.action, output.message]);
|
const message_username = message_index === -1 ? message.trim() : message.slice(0, message_index);
|
||||||
}
|
const message_content = index === -1 ? '' : message.slice(index + 1).trim();
|
||||||
});
|
|
||||||
|
queue.push({datetime: dt, message: `**\`${message_username}\`**: ${message_content}`});
|
||||||
|
} else if (ACTION_VERBS[output.action]) {
|
||||||
|
const name = text.toLowerCase().endsWith(ACTION_VERBS[output.action])
|
||||||
|
? text.slice(0, -ACTION_VERBS[output.action].length).trim()
|
||||||
|
: text;
|
||||||
|
|
||||||
|
queue.push({datetime: dt, message: `**\`${name}\`** ${ACTION_VERBS[output.action]} (${instance.playersOnline.size})`});
|
||||||
|
} else {
|
||||||
|
queue.push({datetime: dt, message: `unknown action ${output.action}`});
|
||||||
|
}
|
||||||
|
|
||||||
|
await flush();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user