mirror of
https://github.com/PHIDIAS0303/ClusterChatSync.git
synced 2026-11-11 15:14:52 +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';
|
||||
|
||||
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) {
|
||||
const {controller, logger, plugin} = context;
|
||||
@@ -50,11 +44,11 @@ export default async function(context: ControllerPluginContext) {
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
let channel: Discord.Channel | null;
|
||||
@@ -73,23 +67,20 @@ export default async function(context: ControllerPluginContext) {
|
||||
return;
|
||||
}
|
||||
|
||||
let nrcMsg = message;
|
||||
let msg = message;
|
||||
|
||||
if (controller.config.get('chat_relay.datetime_on_message')) {
|
||||
const now = new Date();
|
||||
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}`;
|
||||
msg = `${datetime} ${msg}`;
|
||||
}
|
||||
|
||||
const send = (content: string) => channel.send({content, allowedMentions: {parse: []}});
|
||||
|
||||
if (nrcMsg.length <= MAX_DISCORD_MESSAGE_LENGTH) {
|
||||
await send(nrcMsg);
|
||||
if (msg.length <= MAX_DISCORD_MESSAGE_LENGTH) {
|
||||
await send(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
let remaining = nrcMsg;
|
||||
let remaining = msg;
|
||||
|
||||
while (remaining.length > 0) {
|
||||
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) => {
|
||||
const {instanceId, instanceName, action, content} = request;
|
||||
|
||||
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})`);
|
||||
const {instance_name, datetime, message} = request;
|
||||
await sendMessage(instance_name, datetime, message);
|
||||
});
|
||||
|
||||
controller.config.on('fieldChanged', (field) => {
|
||||
|
||||
@@ -11,21 +11,19 @@ export class ChatEvent {
|
||||
static permission = null;
|
||||
|
||||
constructor(
|
||||
public instanceId: number,
|
||||
public instanceName: string,
|
||||
public action: string,
|
||||
public content: string,
|
||||
public instance_name: string,
|
||||
public datetime: string,
|
||||
public message: string,
|
||||
) {}
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
'instanceId': Type.Integer(),
|
||||
'instanceName': Type.String(),
|
||||
'action': Type.String(),
|
||||
'content': Type.String(),
|
||||
static json_schema = Type.Object({
|
||||
'instance_name': Type.String(),
|
||||
'datetime': Type.String(),
|
||||
'message': Type.String(),
|
||||
});
|
||||
|
||||
static fromJSON(json: Static<typeof ChatEvent.jsonSchema>) {
|
||||
return new this(json.instanceId, json.instanceName, json.action, json.content);
|
||||
static fromJSON(json: Static<typeof ChatEvent.json_schema>) {
|
||||
return new this(json.instance_name, json.datetime, json.message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+63
-30
@@ -1,43 +1,76 @@
|
||||
import type {InstancePluginContext} from '@clusterio/host';
|
||||
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) {
|
||||
const {instance, host, plugin} = context;
|
||||
let queue: [string, string][] = [];
|
||||
const {instance, host, plugin, logger} = context;
|
||||
|
||||
instance.hooks.controllerConnectionEvent.attach(plugin.name, async (event) => {
|
||||
if (event !== 'connect' || queue.length === 0) {
|
||||
return;
|
||||
}
|
||||
const queue: ChatMessage[] = [];
|
||||
let flushing = false;
|
||||
|
||||
const pending = queue;
|
||||
queue = [];
|
||||
const failed: [string, string][] = [];
|
||||
|
||||
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]);
|
||||
}
|
||||
const flush = async () => {
|
||||
if (flushing || queue.length === 0 || !host.connector.connected) {
|
||||
return;
|
||||
}
|
||||
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) => {
|
||||
if (output.type !== 'action') {
|
||||
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.id, instance.name, output.action, output.message));
|
||||
} catch (err) {
|
||||
queue.push([output.action, output.message]);
|
||||
}
|
||||
} else {
|
||||
queue.push([output.action, output.message]);
|
||||
}
|
||||
});
|
||||
const now = new Date();
|
||||
const p = (n: number) => String(n).padStart(2, '0');
|
||||
const dt = `${now.getFullYear()}${p(now.getMonth() + 1)}${p(now.getDate())} ${p(now.getHours())}${p(now.getMinutes())}${p(now.getSeconds())}`;
|
||||
|
||||
if (output.action === 'CHAT' || output.action === 'SHOUT') {
|
||||
const message = output.message.trim().replace(/\[special-item=.*?\]/g, '<blueprint>').replace(/<@/g, '<@\u200c>');
|
||||
const message_index = message.indexOf(':');
|
||||
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