mirror of
https://github.com/PHIDIAS0303/ClusterChatSync.git
synced 2026-11-15 23:34:52 +09:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c56f2f03de | ||
|
|
984613de7d | ||
|
|
5461422397 | ||
|
|
3b223191da | ||
|
|
4f6c380859 | ||
|
|
43eb44a9f0 | ||
|
|
fdf9c678f7 | ||
|
|
410371870f | ||
|
|
763dd4628f | ||
|
|
969a770663 | ||
|
|
6a51d36d14 | ||
|
|
bd7d027702 | ||
|
|
5aa834c237 |
No files matched your search
@@ -1,5 +1,13 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## v1.0.6
|
||||||
|
|
||||||
|
- Bidirectional
|
||||||
|
|
||||||
|
## v1.0.5
|
||||||
|
|
||||||
|
- Action and player count
|
||||||
|
|
||||||
## v1.0.3
|
## v1.0.3
|
||||||
|
|
||||||
- Changed to TS
|
- Changed to TS
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
Chat Relay
|
Chat Relay
|
||||||
========================
|
========================
|
||||||
|
|
||||||
One way chat relay to Discord.
|
Chat relay of Discord and Factorio, seperate channel
|
||||||
|
|
||||||
Plugin of Clusterio
|
Plugin of Clusterio
|
||||||
https://github.com/clusterio/clusterio
|
https://github.com/clusterio/clusterio
|
||||||
+274
-34
@@ -1,12 +1,18 @@
|
|||||||
import type {ControllerPluginContext} from '@clusterio/controller';
|
import type {ControllerPluginContext} from '@clusterio/controller';
|
||||||
import {REST, Routes} from 'discord.js';
|
import {Client, GatewayIntentBits, REST, Routes} from 'discord.js';
|
||||||
import {ChatEvent} from './index.js';
|
import type {Message} from 'discord.js';
|
||||||
|
import {ChatEvent, ChatFromDiscordEvent} from './index.js';
|
||||||
|
|
||||||
/* Controller-side half of the chat relay: Factorio chat -> Discord.
|
/* Controller-side halves of the chat relay.
|
||||||
* One-way only, so this talks to the REST API directly instead of opening a
|
* Outbound (Factorio -> Discord) talks to the REST API directly: no socket, no intents.
|
||||||
* gateway Client: no intents (and no privileged MessageContent verification),
|
* Inbound (Discord -> Factorio) needs messageCreate, so it owns a real gateway Client.
|
||||||
* no login/destroy lifecycle, no reconnect state machine.
|
* Both are built from the same token and rebuilt by the same enqueue chain, so a token
|
||||||
*/
|
* rotation can't leave one half running on the old credential.
|
||||||
|
*
|
||||||
|
* Both directions read the single `discord_channel_mapping` field (instance name ->
|
||||||
|
* channel ID). The reverse lookup is built by inverting it, so the two directions can
|
||||||
|
* never disagree about which channel belongs to which instance.
|
||||||
|
*/
|
||||||
|
|
||||||
// Discord's hard limit is 2000; keep headroom for future decoration.
|
// Discord's hard limit is 2000; keep headroom for future decoration.
|
||||||
const MAX_MESSAGE_LENGTH = 1950;
|
const MAX_MESSAGE_LENGTH = 1950;
|
||||||
@@ -16,6 +22,10 @@ const MIN_WORD_BREAK_FILL = 0.5;
|
|||||||
|
|
||||||
const ERROR_UNKNOWN_CHANNEL = 10003;
|
const ERROR_UNKNOWN_CHANNEL = 10003;
|
||||||
|
|
||||||
|
// Factorio chat scroll is a shared, scarce resource; be stricter than Discord is.
|
||||||
|
const INBOUND_BUCKET_SIZE = 5;
|
||||||
|
const INBOUND_REFILL_PER_SEC = 0.5;
|
||||||
|
|
||||||
interface DiscordError {
|
interface DiscordError {
|
||||||
code?: number;
|
code?: number;
|
||||||
status?: number;
|
status?: number;
|
||||||
@@ -32,7 +42,7 @@ function splitMessage(text: string, limit: number): string[] {
|
|||||||
return text.length === 0 ? [] : [text];
|
return text.length === 0 ? [] : [text];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Array.from, not slice, so we never cut an emoji's surrogate pair in half.
|
// Array.from, not slice, so we never cut a surrogate pair in half.
|
||||||
const chars = Array.from(text);
|
const chars = Array.from(text);
|
||||||
const chunks: string[] = [];
|
const chunks: string[] = [];
|
||||||
let offset = 0;
|
let offset = 0;
|
||||||
@@ -74,16 +84,30 @@ function splitMessage(text: string, limit: number): string[] {
|
|||||||
return chunks.filter(chunk => chunk.length > 0);
|
return chunks.filter(chunk => chunk.length > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Bucket {
|
||||||
|
tokens: number;
|
||||||
|
updated: number;
|
||||||
|
warned: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export default async function(context: ControllerPluginContext) {
|
export default async function(context: ControllerPluginContext) {
|
||||||
const {controller, logger, plugin} = context;
|
const {controller, logger, plugin} = context;
|
||||||
|
|
||||||
let rest: REST | null = null;
|
let rest: REST | null = null;
|
||||||
|
let client: Client | null = null;
|
||||||
let shuttingDown = false;
|
let shuttingDown = false;
|
||||||
|
|
||||||
// Log-once guards. A channel that is unmapped or unusable gets hit once per chat
|
// Log-once guards. A channel that is unmapped or unusable gets hit once per chat
|
||||||
// line, which is how a typo in the config turns into a 10k-line log file.
|
// line, which is how a typo in the config turns into a 10k-line log file.
|
||||||
const warnedNoMapping = new Set<string>();
|
const warnedNoMapping = new Set<string>();
|
||||||
const warnedBadChannel = new Set<string>();
|
const warnedBadChannel = new Set<string>();
|
||||||
|
const warnedAmbiguous = new Set<string>();
|
||||||
|
const warnedNoInstance = new Set<string>();
|
||||||
|
const warnedSendFail = new Set<string>();
|
||||||
|
|
||||||
|
// Inverted view of discord_channel_mapping, rebuilt rather than mutated in place.
|
||||||
|
let channelToInstances = new Map<string, string[]>();
|
||||||
|
const buckets = new Map<string, Bucket>();
|
||||||
|
|
||||||
// Serialise reconnects so two quick config changes can't interleave and have
|
// Serialise reconnects so two quick config changes can't interleave and have
|
||||||
// the older attempt null out the newer client.
|
// the older attempt null out the newer client.
|
||||||
@@ -94,37 +118,22 @@ export default async function(context: ControllerPluginContext) {
|
|||||||
return run;
|
return run;
|
||||||
};
|
};
|
||||||
|
|
||||||
const connect = async () => {
|
const getToken = () => controller.config.get('chat_relay.discord_bot_token') as string | undefined;
|
||||||
rest = null;
|
|
||||||
|
|
||||||
const token = controller.config.get('chat_relay.discord_bot_token') as string | undefined;
|
// --------------------------------------------------------------- outbound
|
||||||
if (!token) {
|
|
||||||
logger.error('[Chat Relay] Discord bot token not configured.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const client = new REST({version: '10'}).setToken(token);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Fails fast at startup instead of silently on the first chat line.
|
|
||||||
const me = await client.get(Routes.user('@me')) as {id: string, username: string};
|
|
||||||
logger.info(`[Chat Relay] Authenticated with Discord as ${me.username} (${me.id}).`);
|
|
||||||
} catch (err) {
|
|
||||||
logger.error(`[Chat Relay] Discord token rejected:\n${stackOf(err)}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
rest = client;
|
|
||||||
};
|
|
||||||
|
|
||||||
const getChannelId = (instanceName: string): string | null => {
|
const getChannelId = (instanceName: string): string | null => {
|
||||||
const mapping = controller.config.get('chat_relay.discord_channel_mapping') as Record<string, string> | undefined;
|
const mapping = controller.config.get('chat_relay.discord_channel_mapping') as
|
||||||
|
Record<string, string> | undefined;
|
||||||
const channelId = mapping?.[instanceName];
|
const channelId = mapping?.[instanceName];
|
||||||
|
|
||||||
if (!channelId) {
|
if (!channelId) {
|
||||||
if (!warnedNoMapping.has(instanceName)) {
|
if (!warnedNoMapping.has(instanceName)) {
|
||||||
warnedNoMapping.add(instanceName);
|
warnedNoMapping.add(instanceName);
|
||||||
logger.warn(`[Chat Relay] No Discord channel mapped for instance ${instanceName}; dropping its messages.`);
|
logger.warn(
|
||||||
|
`[Chat Relay] No Discord channel mapped for instance ${instanceName};`
|
||||||
|
+ ' dropping its messages.',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@@ -137,7 +146,7 @@ export default async function(context: ControllerPluginContext) {
|
|||||||
if (shuttingDown) return;
|
if (shuttingDown) return;
|
||||||
|
|
||||||
const discord = rest;
|
const discord = rest;
|
||||||
// Not configured, or the token was rejected. connect() already logged.
|
// Not configured, or the token was rejected. connect() already logged it.
|
||||||
if (!discord) return;
|
if (!discord) return;
|
||||||
|
|
||||||
const channelId = getChannelId(instanceName);
|
const channelId = getChannelId(instanceName);
|
||||||
@@ -178,6 +187,228 @@ export default async function(context: ControllerPluginContext) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------- inbound
|
||||||
|
|
||||||
|
const rebuildInboundMap = () => {
|
||||||
|
const mapping = controller.config.get('chat_relay.discord_channel_mapping') as
|
||||||
|
Record<string, string> | undefined;
|
||||||
|
|
||||||
|
const next = new Map<string, string[]>();
|
||||||
|
|
||||||
|
for (const [instanceName, channelId] of Object.entries(mapping ?? {})) {
|
||||||
|
// A blank value would otherwise map the literal channel ''.
|
||||||
|
if (!channelId) continue;
|
||||||
|
|
||||||
|
const names = next.get(channelId) ?? [];
|
||||||
|
names.push(instanceName);
|
||||||
|
next.set(channelId, names);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [channelId, names] of next) {
|
||||||
|
if (names.length > 1 && !warnedAmbiguous.has(channelId)) {
|
||||||
|
warnedAmbiguous.add(channelId);
|
||||||
|
logger.warn(
|
||||||
|
`[Chat Relay] Discord channel ${channelId} is mapped by ${names.length} instances`
|
||||||
|
+ ` (${names.join(', ')}); its messages are printed in all of them.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
channelToInstances = next;
|
||||||
|
};
|
||||||
|
|
||||||
|
const hasInboundMapping = () => channelToInstances.size > 0;
|
||||||
|
|
||||||
|
/** Token bucket per channel: Discord is far chattier than Factorio chat ever is. */
|
||||||
|
const takeToken = (channelId: string): boolean => {
|
||||||
|
const now = Date.now();
|
||||||
|
let bucket = buckets.get(channelId);
|
||||||
|
|
||||||
|
if (!bucket) {
|
||||||
|
bucket = {tokens: INBOUND_BUCKET_SIZE, updated: now, warned: false};
|
||||||
|
buckets.set(channelId, bucket);
|
||||||
|
}
|
||||||
|
|
||||||
|
bucket.tokens = Math.min(
|
||||||
|
INBOUND_BUCKET_SIZE,
|
||||||
|
bucket.tokens + ((now - bucket.updated) / 1000) * INBOUND_REFILL_PER_SEC,
|
||||||
|
);
|
||||||
|
bucket.updated = now;
|
||||||
|
|
||||||
|
// Re-arm the warning once the channel has calmed down.
|
||||||
|
if (bucket.tokens >= INBOUND_BUCKET_SIZE) bucket.warned = false;
|
||||||
|
|
||||||
|
if (bucket.tokens < 1) {
|
||||||
|
if (!bucket.warned) {
|
||||||
|
bucket.warned = true;
|
||||||
|
logger.warn(`[Chat Relay] Rate limiting inbound chat from channel ${channelId}; dropping.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bucket.tokens -= 1;
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Config is keyed by instance name; sendTo wants the controller's numeric ID. */
|
||||||
|
const findInstanceId = (instanceName: string): number | null => {
|
||||||
|
for (const instance of controller.instances.values()) {
|
||||||
|
if (instance.config.get('instance.name') === instanceName) return instance.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
const onDiscordMessage = async (message: Message) => {
|
||||||
|
if (shuttingDown) return;
|
||||||
|
|
||||||
|
// The loop guard. Our own outbound posts are authored by the bot, as are
|
||||||
|
// webhooks and other bots, so Factorio -> Discord -> Factorio dies here.
|
||||||
|
if (message.author.bot) return;
|
||||||
|
|
||||||
|
// DMs, group DMs and other unmapped guilds have no instance behind them.
|
||||||
|
if (!message.inGuild()) return;
|
||||||
|
|
||||||
|
const instanceNames = channelToInstances.get(message.channelId);
|
||||||
|
if (!instanceNames) return;
|
||||||
|
|
||||||
|
if (!message.content) return;
|
||||||
|
if (!takeToken(message.channelId)) return;
|
||||||
|
|
||||||
|
const datetime = message.createdAt.toISOString();
|
||||||
|
const authorName = message.member?.displayName ?? message.author.username;
|
||||||
|
|
||||||
|
|
||||||
|
for (const instanceName of instanceNames) {
|
||||||
|
const instanceId = findInstanceId(instanceName);
|
||||||
|
|
||||||
|
if (instanceId === null) {
|
||||||
|
if (!warnedNoInstance.has(instanceName)) {
|
||||||
|
warnedNoInstance.add(instanceName);
|
||||||
|
logger.warn(
|
||||||
|
`[Chat Relay] Instance ${instanceName} is mapped for chat but does not exist;`
|
||||||
|
+ ' dropping Discord messages for it.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await controller.sendTo({instanceId: instanceId}, new ChatFromDiscordEvent(
|
||||||
|
authorName,
|
||||||
|
message.channelId,
|
||||||
|
datetime,
|
||||||
|
message.content,
|
||||||
|
));
|
||||||
|
} catch (err) {
|
||||||
|
// A stopped instance on a busy channel would otherwise be a log flood.
|
||||||
|
if (!warnedSendFail.has(instanceName)) {
|
||||||
|
warnedSendFail.add(instanceName);
|
||||||
|
logger.error(
|
||||||
|
`[Chat Relay] Failed to relay Discord chat to ${instanceName}:\n${stackOf(err)}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------- client
|
||||||
|
|
||||||
|
const closeGateway = async () => {
|
||||||
|
const old = client;
|
||||||
|
client = null;
|
||||||
|
|
||||||
|
if (old) {
|
||||||
|
try {
|
||||||
|
await old.destroy();
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(`[Chat Relay] Discord gateway shutdown error:\n${stackOf(err)}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Requires an authenticated `rest`; only ever called from connect()/syncGateway(). */
|
||||||
|
const openGateway = async () => {
|
||||||
|
const token = getToken();
|
||||||
|
if (!token || !rest || client) return;
|
||||||
|
|
||||||
|
const newClient = new Client({intents: [
|
||||||
|
GatewayIntentBits.Guilds,
|
||||||
|
GatewayIntentBits.GuildMessages,
|
||||||
|
GatewayIntentBits.MessageContent,
|
||||||
|
]});
|
||||||
|
|
||||||
|
newClient.on('messageCreate', message => {
|
||||||
|
onDiscordMessage(message).catch(err => {
|
||||||
|
logger.error(`[Chat Relay] Inbound handler failed:\n${stackOf(err)}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
newClient.on('error', err => logger.error(`[Chat Relay] Discord gateway error:\n${stackOf(err)}`));
|
||||||
|
newClient.on('shardDisconnect', () => logger.warn('[Chat Relay] Discord gateway disconnected.'));
|
||||||
|
|
||||||
|
try {
|
||||||
|
await newClient.login(token);
|
||||||
|
} catch (err) {
|
||||||
|
// Outbound still works off `rest`; only inbound is down.
|
||||||
|
logger.error(`[Chat Relay] Discord gateway login failed (inbound disabled):\n${stackOf(err)}`);
|
||||||
|
await newClient.destroy().catch(() => {});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shuttingDown) {
|
||||||
|
await newClient.destroy().catch(() => {});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
client = newClient;
|
||||||
|
logger.info('[Chat Relay] Discord gateway connected; inbound chat enabled.');
|
||||||
|
};
|
||||||
|
|
||||||
|
const syncGateway = async () => {
|
||||||
|
if (!hasInboundMapping()) {
|
||||||
|
await closeGateway();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await openGateway();
|
||||||
|
};
|
||||||
|
|
||||||
|
const connect = async () => {
|
||||||
|
// Drop both halves first: the old REST client holds the previous token, and
|
||||||
|
// the old gateway is logged in with it.
|
||||||
|
rest = null;
|
||||||
|
await closeGateway();
|
||||||
|
|
||||||
|
const token = getToken();
|
||||||
|
if (!token) {
|
||||||
|
logger.error('[Chat Relay] Discord bot token not configured.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const newRest = new REST({version: '10'}).setToken(token);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Fail fast at startup instead of silently on the first chat line.
|
||||||
|
const me = await newRest.get(Routes.user('@me')) as {id: string, username: string};
|
||||||
|
logger.info(`[Chat Relay] Authenticated with Discord as ${me.username} (${me.id}).`);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(`[Chat Relay] Discord token rejected:\n${stackOf(err)}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shuttingDown) return;
|
||||||
|
|
||||||
|
rest = newRest;
|
||||||
|
await syncGateway();
|
||||||
|
};
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------- wiring
|
||||||
|
|
||||||
|
rebuildInboundMap();
|
||||||
|
|
||||||
controller.handle(ChatEvent, async (request: ChatEvent) => {
|
controller.handle(ChatEvent, async (request: ChatEvent) => {
|
||||||
await sendMessage(request.instance_name, request.datetime, request.message);
|
await sendMessage(request.instance_name, request.datetime, request.message);
|
||||||
});
|
});
|
||||||
@@ -190,13 +421,22 @@ export default async function(context: ControllerPluginContext) {
|
|||||||
} else if (field === 'chat_relay.discord_channel_mapping') {
|
} else if (field === 'chat_relay.discord_channel_mapping') {
|
||||||
warnedNoMapping.clear();
|
warnedNoMapping.clear();
|
||||||
warnedBadChannel.clear();
|
warnedBadChannel.clear();
|
||||||
|
warnedAmbiguous.clear();
|
||||||
|
warnedNoInstance.clear();
|
||||||
|
warnedSendFail.clear();
|
||||||
|
buckets.clear();
|
||||||
|
rebuildInboundMap();
|
||||||
|
enqueue(syncGateway).catch(err => {
|
||||||
|
logger.error(`[Chat Relay] Discord gateway sync failed:\n${stackOf(err)}`);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
controller.hooks.shutdown.attach(plugin.name, async () => {
|
controller.hooks.shutdown.attach(plugin.name, async () => {
|
||||||
// Nothing to tear down without a gateway; just stop accepting new sends.
|
// Nothing to tear down on the REST side; in-flight calls aren't cancellable
|
||||||
// In-flight REST calls aren't cancellable and will finish or time out.
|
// and will finish or time out. The gateway does need an explicit destroy.
|
||||||
shuttingDown = true;
|
shuttingDown = true;
|
||||||
|
await closeGateway();
|
||||||
});
|
});
|
||||||
|
|
||||||
await connect();
|
await connect();
|
||||||
|
|||||||
@@ -1,70 +1,138 @@
|
|||||||
import type * as lib from '@clusterio/lib';
|
import type * as lib from '@clusterio/lib';
|
||||||
import type {Instance, InstancePluginContext} from '@clusterio/host';
|
import {Type} from '@sinclair/typebox';
|
||||||
import {Type, Static} from '@sinclair/typebox';
|
import type {Static} from '@sinclair/typebox';
|
||||||
|
|
||||||
|
/* Shared declarations for the chat relay plugin.
|
||||||
|
* `sanitiseForFactorio` is used by both processes: the controller cleans text
|
||||||
|
* before it leaves, and the instance cleans it again before it reaches game.print.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export const chatFromDiscord = Type.Object({
|
||||||
|
author_name: Type.String(),
|
||||||
|
channel_id: Type.String(),
|
||||||
|
datetime: Type.String(),
|
||||||
|
message: Type.String(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type ChatFromDiscord = Static<typeof chatFromDiscord>;
|
||||||
|
|
||||||
|
export class ChatFromDiscordEvent {
|
||||||
|
declare ['constructor']: typeof ChatFromDiscordEvent;
|
||||||
|
static type = 'event' as const;
|
||||||
|
static src = 'controller' as const;
|
||||||
|
static dst = 'instance' as const;
|
||||||
|
static plugin = 'chat_relay' as const;
|
||||||
|
static permission = null;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
public author_name: string,
|
||||||
|
public channel_id: string,
|
||||||
|
public datetime: string,
|
||||||
|
public message: string,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
static jsonSchema = chatFromDiscord;
|
||||||
|
|
||||||
|
static fromJSON(json: Static<typeof ChatFromDiscordEvent.jsonSchema>) {
|
||||||
|
return new this(json.author_name, json.channel_id, json.datetime, json.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const MAX_INBOUND_LENGTH = 400;
|
||||||
|
|
||||||
|
// Factorio renders these, so a Discord user could spoof a colour, an item link,
|
||||||
|
// or a fake speaker line. Whitelist the real tags rather than matching \[.*\],
|
||||||
|
// which would also eat innocent text like 'array[0]' or '[lol]'.
|
||||||
|
const RICH_TEXT_RE = new RegExp(
|
||||||
|
String.raw`\[/?(?:color|font|item|entity|technology|recipe|fluid|tile|virtual-signal`
|
||||||
|
+ String.raw`|gps|img|specialisation|achievement|train|planet|space-location|quality)(?:=[^\]]*)?\]`,
|
||||||
|
'gi',
|
||||||
|
);
|
||||||
|
|
||||||
|
/** Make arbitrary text safe to print in-game. Shared by both processes. */
|
||||||
|
export function sanitiseForFactorio(text: string, limit = MAX_INBOUND_LENGTH): string {
|
||||||
|
let clean = text
|
||||||
|
.replace(RICH_TEXT_RE, '')
|
||||||
|
.replace(/[\u0000-\u001f\u007f]/g, '') // control chars, including newlines
|
||||||
|
.replace(/\s+/g, ' ')
|
||||||
|
.trim();
|
||||||
|
|
||||||
|
// Code points, not UTF-16 units: emoji paste from Discord is real here, and a
|
||||||
|
// lone surrogate in a Lua string sent over stdin is not something I'd trust.
|
||||||
|
const chars = Array.from(clean);
|
||||||
|
if (chars.length > limit) {
|
||||||
|
clean = `${chars.slice(0, limit - 3).join('').trimEnd()}...`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return clean;
|
||||||
|
}
|
||||||
|
|
||||||
export class ChatEvent {
|
export class ChatEvent {
|
||||||
declare ['constructor']: typeof ChatEvent;
|
declare ['constructor']: typeof ChatEvent;
|
||||||
static type = 'event' as const;
|
static type = 'event' as const;
|
||||||
static src = 'instance' as const;
|
static src = 'instance' as const;
|
||||||
static dst = 'controller' as const;
|
static dst = 'controller' as const;
|
||||||
static plugin = 'chat_relay' as const;
|
static plugin = 'chat_relay' as const;
|
||||||
static permission = null;
|
static permission = null;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public instance_name: string,
|
public instance_name: string,
|
||||||
public datetime: string,
|
public datetime: string,
|
||||||
public message: string,
|
public message: string,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
static jsonSchema = Type.Object({
|
static jsonSchema = Type.Object({
|
||||||
'instance_name': Type.String(),
|
'instance_name': Type.String(),
|
||||||
'datetime': Type.String(),
|
'datetime': Type.String(),
|
||||||
'message': Type.String(),
|
'message': Type.String(),
|
||||||
});
|
});
|
||||||
|
|
||||||
static fromJSON(json: Static<typeof ChatEvent.jsonSchema>) {
|
static fromJSON(json: Static<typeof ChatEvent.jsonSchema>) {
|
||||||
return new this(json.instance_name, json.datetime, json.message);
|
return new this(json.instance_name, json.datetime, json.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
declare module '@clusterio/lib' {
|
declare module '@clusterio/lib' {
|
||||||
interface ControllerConfigFields {
|
interface ControllerConfigFields {
|
||||||
'chat_relay.discord_bot_token': string;
|
'chat_relay.discord_bot_token': string;
|
||||||
'chat_relay.datetime_on_message': boolean;
|
'chat_relay.datetime_on_message': boolean;
|
||||||
'chat_relay.discord_channel_mapping': Record<string, string>;
|
'chat_relay.discord_channel_mapping': Record<string, string>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const plugin: lib.PluginDeclaration = {
|
export const plugin: lib.PluginDeclaration = {
|
||||||
name: 'chat_relay',
|
name: 'chat_relay',
|
||||||
title: 'Chat Relay',
|
title: 'Chat Relay',
|
||||||
description: 'One way chat relay to Discord.',
|
description: 'Bidirectional chat relay between Factorio and Discord.',
|
||||||
webEntrypoint: './web',
|
instanceEntrypoint: 'dist/node/instance.js',
|
||||||
instanceEntrypoint: 'dist/node/instance.js',
|
controllerEntrypoint: 'dist/node/controller.js',
|
||||||
controllerEntrypoint: 'dist/node/controller.js',
|
controllerConfigFields: {
|
||||||
controllerConfigFields: {
|
'chat_relay.discord_bot_token': {
|
||||||
'chat_relay.discord_bot_token': {
|
title: 'Discord Bot Token',
|
||||||
title: 'Discord Bot Token',
|
description: 'API Token',
|
||||||
description: 'API Token',
|
type: 'string',
|
||||||
type: 'string',
|
credential: ['controller'],
|
||||||
credential: ['controller']
|
},
|
||||||
},
|
'chat_relay.datetime_on_message': {
|
||||||
'chat_relay.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
|
},
|
||||||
},
|
'chat_relay.discord_channel_mapping': {
|
||||||
'chat_relay.discord_channel_mapping': {
|
title: 'Discord Channels',
|
||||||
title: 'Discord Channels',
|
description: 'Instance and Discord channel ID relations, used in both directions',
|
||||||
description: 'Instance and Discord channel ID relations',
|
type: 'object',
|
||||||
type: 'object',
|
initialValue: {
|
||||||
initialValue: {
|
'S1': '123',
|
||||||
'S1': '123'
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
messages: [
|
||||||
messages: [
|
ChatEvent,
|
||||||
ChatEvent,
|
ChatFromDiscordEvent,
|
||||||
|
],
|
||||||
|
features: [
|
||||||
|
'ScriptCommands',
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
+39
-5
@@ -1,9 +1,9 @@
|
|||||||
import type {InstancePluginContext} from '@clusterio/host';
|
import type {InstancePluginContext} from '@clusterio/host';
|
||||||
import {ChatEvent} from './index.js';
|
import {ChatEvent, ChatFromDiscordEvent, sanitiseForFactorio} from './index.js';
|
||||||
|
|
||||||
interface ChatMessage {
|
interface ChatMessage {
|
||||||
datetime: string;
|
datetime: string;
|
||||||
message: string;
|
message: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
function datetime(): string {
|
function datetime(): string {
|
||||||
@@ -25,16 +25,27 @@ function parse_save(line: string): number | null {
|
|||||||
return slot ? Number(slot[1]) : null;
|
return slot ? Number(slot[1]) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 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) {
|
export default async function(context: InstancePluginContext) {
|
||||||
const {instance, host, plugin, logger} = context;
|
const {instance, host, plugin, logger} = context;
|
||||||
const queue: ChatMessage[] = [];
|
const queue: ChatMessage[] = [];
|
||||||
let flushing = false;
|
let flushing = false;
|
||||||
|
|
||||||
|
// Log-once guard: a busy channel plus a stopped instance or dead RCON would
|
||||||
|
// otherwise write one error per message. Re-armed on controller connect.
|
||||||
|
const warnedPrintFail = new Set<string>();
|
||||||
|
|
||||||
const flush = async () => {
|
const flush = async () => {
|
||||||
if (flushing || queue.length === 0 || !host.connector.connected) {
|
if (flushing || queue.length === 0 || !host.connector.connected) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
flushing = true;
|
flushing = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while (queue.length > 0 && host.connector.connected) {
|
while (queue.length > 0 && host.connector.connected) {
|
||||||
try {
|
try {
|
||||||
@@ -52,6 +63,7 @@ export default async function(context: InstancePluginContext) {
|
|||||||
|
|
||||||
instance.hooks.controllerConnectionEvent.attach(plugin.name, async (event) => {
|
instance.hooks.controllerConnectionEvent.attach(plugin.name, async (event) => {
|
||||||
if (event === 'connect') {
|
if (event === 'connect') {
|
||||||
|
warnedPrintFail.clear();
|
||||||
await flush();
|
await flush();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -69,18 +81,18 @@ export default async function(context: InstancePluginContext) {
|
|||||||
const message_content = message_index === -1 ? '' : message.slice(message_index + 1).trim();
|
const message_content = message_index === -1 ? '' : message.slice(message_index + 1).trim();
|
||||||
|
|
||||||
queue.push({datetime: dt, message: `**\`${message_username}\`**: ${message_content}`});
|
queue.push({datetime: dt, message: `**\`${message_username}\`**: ${message_content}`});
|
||||||
} else if (action === 'JOIN' || action === 'LEAVE' || action === 'KICK' || action === 'BAN' || action === 'UNBANNED') {
|
} else if (action === 'JOIN' || action === 'LEAVE' || action === 'KICK') {
|
||||||
const message_index = message.indexOf(' ');
|
const message_index = message.indexOf(' ');
|
||||||
const message_username = message_index === -1 ? message : message.slice(0, message_index);
|
const message_username = message_index === -1 ? message : message.slice(0, message_index);
|
||||||
const message_content = message_index === -1 ? '' : message.slice(message_index + 1).trim();
|
const message_content = message_index === -1 ? '' : message.slice(message_index + 1).trim();
|
||||||
const player_count = instance.playersOnline.size + (action === 'JOIN' ? 0 : -1);
|
const player_count = instance.playersOnline.size + (action === 'LEAVE' ? -1 : 0);
|
||||||
|
|
||||||
queue.push({datetime: dt, message: `**\`${message_username}\`** ${message_content} (${player_count})`});
|
queue.push({datetime: dt, message: `**\`${message_username}\`** ${message_content} (${player_count})`});
|
||||||
}
|
}
|
||||||
|
|
||||||
await flush();
|
await flush();
|
||||||
} else if (output.type === 'log') {
|
} else if (output.type === 'log') {
|
||||||
let message = output.message.trim();
|
const message = output.message.trim();
|
||||||
const save_slot = parse_save(message);
|
const save_slot = parse_save(message);
|
||||||
|
|
||||||
if (!save_slot) {
|
if (!save_slot) {
|
||||||
@@ -92,4 +104,26 @@ export default async function(context: InstancePluginContext) {
|
|||||||
await flush();
|
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 = `[DC] ${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)})`);
|
||||||
|
logger.info(`[Chat Relay] ${line}`);
|
||||||
|
} catch (err) {
|
||||||
|
if (!warnedPrintFail.has(instance.name)) {
|
||||||
|
warnedPrintFail.add(instance.name);
|
||||||
|
logger.error(`[Chat Relay] Failed to print Discord message in game:\n${(err as Error).stack}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
+7
-7
@@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "@phidias0303/chat_relay",
|
"name": "@phidias0303/chat_relay",
|
||||||
"version": "1.0.5",
|
"version": "1.0.7",
|
||||||
"description": "One way chat relay to Discord",
|
"description": "Chat relay of Discord and Factorio, seperate channel",
|
||||||
"author": "PHIDIAS <phidias@aperx.org>",
|
"author": "PHIDIAS <phidias@aperx.org>",
|
||||||
"license": "MIT",
|
"license": "Apache 2.0",
|
||||||
"homepage": "https://github.com/PHIDIAS0303/chat_relay#readme",
|
"homepage": "https://github.com/PHIDIAS0303/chat_relay#readme",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -22,18 +22,18 @@
|
|||||||
],
|
],
|
||||||
"main": "dist/node/index.js",
|
"main": "dist/node/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepare": "tsc --build && webpack-cli --mode production",
|
"prepare": "tsc --build && vite build",
|
||||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
|
"@clusterio/controller": "workspace:^",
|
||||||
"@clusterio/lib": "workspace:^"
|
"@clusterio/lib": "workspace:^"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@clusterio/controller": "workspace:^",
|
||||||
"@clusterio/lib": "workspace:^",
|
"@clusterio/lib": "workspace:^",
|
||||||
"@clusterio/web_ui": "workspace:^",
|
"@clusterio/web_ui": "workspace:^",
|
||||||
"webpack": "catalog:",
|
"vite": "catalog:"
|
||||||
"webpack-cli": "catalog:",
|
|
||||||
"webpack-merge": "catalog:"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"discord.js": "^14.27.0",
|
"discord.js": "^14.27.0",
|
||||||
|
|||||||
@@ -4,5 +4,5 @@
|
|||||||
{"path": "../../packages/lib/tsconfig.browser.json"},
|
{"path": "../../packages/lib/tsconfig.browser.json"},
|
||||||
{"path": "../../packages/web_ui/tsconfig.browser.json"}
|
{"path": "../../packages/web_ui/tsconfig.browser.json"}
|
||||||
],
|
],
|
||||||
"include": ["web/**/*.tsx", "web/**/*.ts", "index.ts", "package.json"]
|
"include": ["index.ts", "package.json"]
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import { defineConfig } from "vite";
|
||||||
|
import { clusterioPlugin } from "@clusterio/web_ui/vite.mjs";
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [clusterioPlugin()],
|
||||||
|
});
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
import {type WebPluginContext} from '@clusterio/web_ui';
|
|
||||||
|
|
||||||
export default async function loadWebPlugin(context: WebPluginContext) {
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
const path = require('path');
|
|
||||||
const webpack = require('webpack');
|
|
||||||
const {merge} = require('webpack-merge');
|
|
||||||
const common = require('@clusterio/web_ui/webpack.common');
|
|
||||||
|
|
||||||
module.exports = (env = {}, argv = {}) => merge(common(env, argv), {
|
|
||||||
context: __dirname,
|
|
||||||
entry: './web/index.tsx',
|
|
||||||
output: {
|
|
||||||
path: path.resolve(__dirname, 'dist', 'web'),
|
|
||||||
},
|
|
||||||
plugins: [
|
|
||||||
new webpack.container.ModuleFederationPlugin({
|
|
||||||
name: 'chat_relay',
|
|
||||||
library: {type: 'var', name: 'plugin_chat_relay'},
|
|
||||||
exposes: {
|
|
||||||
'./': './index.ts',
|
|
||||||
'./package.json': './package.json',
|
|
||||||
'./web': './web/index.tsx',
|
|
||||||
},
|
|
||||||
shared: {
|
|
||||||
'@clusterio/lib': {import: false},
|
|
||||||
'@clusterio/web_ui': {import: false},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
});
|
|
||||||
Reference in new issue
Block a user