mirror of
https://github.com/PHIDIAS0303/ClusterChatSync.git
synced 2026-11-17 07:40:41 +09:00
1.0.6 RC2
This commit is contained in:
1 parent
5aa834c237
commit
bd7d027702
4 files changed
+320
-42
No files matched your search
+26
-1
@@ -1,5 +1,5 @@
|
||||
import type {InstancePluginContext} from '@clusterio/host';
|
||||
import {ChatEvent} from './index.js';
|
||||
import {ChatEvent, ChatFromDiscordEvent, sanitiseForFactorio} from './index.js';
|
||||
|
||||
interface ChatMessage {
|
||||
datetime: string;
|
||||
@@ -25,6 +25,13 @@ function parse_save(line: string): number | null {
|
||||
return slot ? Number(slot[1]) : null;
|
||||
}
|
||||
|
||||
/* Instance-side half of inbound chat: turn a relayed Discord message into a chat line. */
|
||||
|
||||
/** Escape a JS string for embedding in a double-quoted Lua literal. */
|
||||
function luaString(text: string): string {
|
||||
return `"${text.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
|
||||
}
|
||||
|
||||
export default async function(context: InstancePluginContext) {
|
||||
const {instance, host, plugin, logger} = context;
|
||||
const queue: ChatMessage[] = [];
|
||||
@@ -92,4 +99,22 @@ export default async function(context: InstancePluginContext) {
|
||||
await flush();
|
||||
}
|
||||
});
|
||||
|
||||
instance.handle(ChatFromDiscordEvent, async (event: ChatFromDiscordEvent) => {
|
||||
// Re-sanitise here too: any producer can publish this event, and this is
|
||||
// the process that actually calls game.print.
|
||||
const author = sanitiseForFactorio(event.author_name, 40);
|
||||
const message = sanitiseForFactorio(event.message);
|
||||
if (!message) return;
|
||||
|
||||
const line = `[Discord] ${author}: ${message}`;
|
||||
|
||||
try {
|
||||
// game.print reaches every player and does NOT fire on_console_chat,
|
||||
// so this cannot echo back out through the outbound half.
|
||||
await instance.sendRcon(`/sc game.print(${luaString(line)})`);
|
||||
} catch (err) {
|
||||
logger.error(`[Chat Relay] Failed to print Discord message in game:\n${(err as Error).stack}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user