This commit is contained in:
phidias authored and GitHub committed 2026-10-09 22:51:45 +09:00
1 parent 31bf3b3375
commit a167374688
4 files changed
+29 -28

No files matched your search

+3 -2
View File
@@ -1,6 +1,7 @@
Cluster Chat Sync Chat Relay
======================== ========================
One way chat forward to Discord. One way chat relay to Discord.
Plugin of Clusterio
https://github.com/clusterio/clusterio https://github.com/clusterio/clusterio
+11 -11
View File
@@ -24,10 +24,10 @@ export default async function(context: ControllerPluginContext) {
const connect = async () => { const connect = async () => {
await clientDestroy(); await clientDestroy();
const token = controller.config.get('ClusterChatSync.discord_bot_token') as string | undefined; const token = controller.config.get('chat_relay.discord_bot_token') as string | undefined;
if (!token) { if (!token) {
logger.error('[Chat Sync] Discord bot token not configured.'); logger.error('[Chat Relay] Discord bot token not configured.');
return; return;
} }
@@ -37,23 +37,23 @@ export default async function(context: ControllerPluginContext) {
Discord.GatewayIntentBits.MessageContent, Discord.GatewayIntentBits.MessageContent,
]}); ]});
logger.info('[Chat Sync] Logging into Discord.'); logger.info('[Chat Relay] Logging into Discord.');
try { try {
await client.login(token); await client.login(token);
} catch (err) { } catch (err) {
logger.error(`[Chat Sync] Discord login error:\n${(err as Error).stack}`); logger.error(`[Chat Relay] Discord login error:\n${(err as Error).stack}`);
await clientDestroy(); await clientDestroy();
return; return;
} }
logger.info('[Chat Sync] Logged in Discord successfully.'); logger.info('[Chat Relay] Logged in Discord successfully.');
}; };
const sendMessage = async (instanceName: string, message: string) => { const sendMessage = async (instanceName: string, message: string) => {
if (!client) return; if (!client) return;
const mapping = controller.config.get('ClusterChatSync.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?.[instanceName];
if (!channelId) return; if (!channelId) return;
@@ -63,19 +63,19 @@ export default async function(context: ControllerPluginContext) {
channel = await client.channels.fetch(channelId); channel = await client.channels.fetch(channelId);
} catch (err) { } catch (err) {
if ((err as {code?: number}).code !== 10003) { if ((err as {code?: number}).code !== 10003) {
logger.error(`[Chat Sync] Discord channel fetch error:\n${(err as Error).stack}`); logger.error(`[Chat Relay] Discord channel fetch error:\n${(err as Error).stack}`);
} }
return; return;
} }
if (channel === null || !channel.isSendable()) { if (channel === null || !channel.isSendable()) {
logger.error(`[Chat Sync] Discord Channel ID ${channelId} is not a usable channel.`); logger.error(`[Chat Relay] Discord Channel ID ${channelId} is not a usable channel.`);
return; return;
} }
let nrcMsg = message; let nrcMsg = message;
if (controller.config.get('ClusterChatSync.datetime_on_message')) { if (controller.config.get('chat_relay.datetime_on_message')) {
const now = new Date(); const now = new Date();
const p = (n: number) => String(n).padStart(2, '0'); 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())}`; const stamp = `${now.getFullYear()}${p(now.getMonth() + 1)}${p(now.getDate())} ${p(now.getHours())}${p(now.getMinutes())}${p(now.getSeconds())}`;
@@ -126,9 +126,9 @@ export default async function(context: ControllerPluginContext) {
}); });
controller.config.on('fieldChanged', (field) => { controller.config.on('fieldChanged', (field) => {
if (field === 'ClusterChatSync.discord_bot_token') { if (field === 'chat_relay.discord_bot_token') {
connect().catch(err => { connect().catch(err => {
logger.error(`[Chat Sync] Discord bot token:\n${(err as Error).stack}`); logger.error(`[Chat Relay] Discord bot token:\n${(err as Error).stack}`);
}); });
} }
}); });
+10 -10
View File
@@ -6,7 +6,7 @@ export class 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 = 'ClusterChatSync' as const; static plugin = 'chat_relay' as const;
static permission = null; static permission = null;
constructor( constructor(
@@ -29,32 +29,32 @@ export class ChatEvent {
declare module '@clusterio/lib' { declare module '@clusterio/lib' {
interface ControllerConfigFields { interface ControllerConfigFields {
'ClusterChatSync.discord_bot_token': string; 'chat_relay.discord_bot_token': string;
'ClusterChatSync.datetime_on_message': boolean; 'chat_relay.datetime_on_message': boolean;
'ClusterChatSync.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: 'ClusterChatSync', name: 'chat_relay',
title: 'Cluster Chat Sync', title: 'Chat Relay',
description: 'One way chat forward to Discord.', description: 'One way chat relay to Discord.',
webEntrypoint: './web', webEntrypoint: './web',
instanceEntrypoint: 'dist/node/instance.js', instanceEntrypoint: 'dist/node/instance.js',
controllerEntrypoint: 'dist/node/controller.js', controllerEntrypoint: 'dist/node/controller.js',
controllerConfigFields: { controllerConfigFields: {
'ClusterChatSync.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'
}, },
'ClusterChatSync.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
}, },
'ClusterChatSync.discord_channel_mapping': { 'chat_relay.discord_channel_mapping': {
title: 'Discord Channels', title: 'Discord Channels',
description: 'Instance and Discord channel ID relations', description: 'Instance and Discord channel ID relations',
type: 'object', type: 'object',
+5 -5
View File
@@ -1,16 +1,16 @@
{ {
"name": "@phidias0303/clusterio_chat_sync", "name": "@phidias0303/chat_relay",
"version": "1.0.3", "version": "1.0.3",
"description": "One way chat forward to Discord", "description": "One way chat relay to Discord",
"author": "PHIDIAS <phidias@aperx.org>", "author": "PHIDIAS <phidias@aperx.org>",
"license": "MIT", "license": "MIT",
"homepage": "https://github.com/PHIDIAS0303/ClusterChatSync#readme", "homepage": "https://github.com/PHIDIAS0303/chat_relay#readme",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/PHIDIAS0303/ClusterChatSync" "url": "git+https://github.com/PHIDIAS0303/chat_relay"
}, },
"bugs": { "bugs": {
"url": "https://github.com/PHIDIAS0303/ClusterChatSync/issues" "url": "https://github.com/PHIDIAS0303/chat_relay/issues"
}, },
"type": "module", "type": "module",
"keywords": [ "keywords": [