@rithien/discord_bridge 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/LICENSE.txt +21 -0
- package/README.md +20 -0
- package/controller.js +270 -0
- package/dist/web/manifest.json +6 -0
- package/dist/web/static/discord_bridge.611b8899ce001451b081.js +526 -0
- package/dist/web/static/info_js.3e97e6576d2e451b2fd6.js +22 -0
- package/dist/web/static/main.75bf3c903fc16e7f86b5.js +120 -0
- package/dist/web/static/package_json.14b21c0791a49b7b3105.js +22 -0
- package/info.js +206 -0
- package/instance.js +56 -0
- package/package.json +33 -0
- package/web/index.jsx +0 -0
- package/webpack.config.js +28 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## v0.3.0 - rithien fork
|
|
4
|
+
|
|
5
|
+
- Forked from Hornwitser/discord_bridge v0.2.5.
|
|
6
|
+
- Added `instance.discord_bridge.channel_id` for per-instance Discord channel routing.
|
|
7
|
+
- The controller-level `discord_bridge.channel_id` is now the default for instances without their own channel, and the destination for host connect/disconnect notifications.
|
|
8
|
+
- Migrated the controller config reactor from the deprecated `config.on("fieldChanged", ...)` to `onControllerConfigFieldChanged` and `onInstanceConfigFieldChanged` hooks (Clusterio alpha 14).
|
|
9
|
+
|
|
10
|
+
## v0.2.5
|
|
11
|
+
|
|
12
|
+
- Updated for Clusterio 2.0.0-alpha.14
|
|
13
|
+
- Added configurable templates for how the messages appears in the game and on Discord.
|
|
14
|
+
- Added templates for instance created and deleted statuses
|
|
15
|
+
|
|
16
|
+
## v0.2.4
|
|
17
|
+
|
|
18
|
+
- Updated for Clusterio 2.0.0-alpha.13
|
|
19
|
+
|
|
20
|
+
## v0.2.3
|
|
21
|
+
|
|
22
|
+
- Updated for Clusterio 2.0.0-alpha.9
|
|
23
|
+
- Fixed instance stop notification not being shown.
|
|
24
|
+
- Added option to notify of host connection and disconnection.
|
|
25
|
+
|
|
26
|
+
## v0.2.2
|
|
27
|
+
|
|
28
|
+
- Updated for Clusterio 2.0.0-alpha.5.
|
|
29
|
+
- Added separate options for bridging joins, chat, shouts, kicks & bans, promotions and commands to the Discord channel.
|
|
30
|
+
- Added option to notify of instance starts and stops to the Discord channel.
|
|
31
|
+
|
|
32
|
+
## v0.2.1
|
|
33
|
+
|
|
34
|
+
- Fixed hang on controller startup.
|
|
35
|
+
|
|
36
|
+
## v0.2.0
|
|
37
|
+
|
|
38
|
+
- Updated for Clusterio 2.0.0-alpha.4.
|
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Hornwitser
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Clusterio Discord Bridge
|
|
2
|
+
========================
|
|
3
|
+
|
|
4
|
+
Bridges chat between a Discord channel and instances, with per-instance channel routing.
|
|
5
|
+
|
|
6
|
+
Forked from [Hornwitser/discord_bridge](https://github.com/Hornwitser/discord_bridge); adds support for configuring a different Discord channel per Clusterio instance, with the controller-level `channel_id` acting as the default for unconfigured instances and the destination for host connect/disconnect notifications.
|
|
7
|
+
|
|
8
|
+
Installation
|
|
9
|
+
------------
|
|
10
|
+
|
|
11
|
+
Run the following commands in the folder Clusterio is installed to:
|
|
12
|
+
|
|
13
|
+
npm install @rithien/discord_bridge
|
|
14
|
+
npx clusteriocontroller plugin add @rithien/discord_bridge
|
|
15
|
+
|
|
16
|
+
Substitute clusteriocontroller with clusteriohost or clusterioctl if this a dedicate host or ctl installation respectively.
|
|
17
|
+
|
|
18
|
+
Once installed you need to set the `discord_bridge.bot_token` and `discord_bridge.channel_id` in the controller config to make the bridge operational.
|
|
19
|
+
|
|
20
|
+
Note that the Message Content Intent is required to be enabled in Discords developer dashboard for the bot.
|
package/controller.js
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const Discord = require("discord.js");
|
|
3
|
+
const { BaseControllerPlugin } = require("@clusterio/controller");
|
|
4
|
+
|
|
5
|
+
const { InstanceActionEvent, DiscordChatEvent } = require("./info.js");
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ControllerPlugin extends BaseControllerPlugin {
|
|
9
|
+
async init() {
|
|
10
|
+
this.client = null;
|
|
11
|
+
this.channels = new Map();
|
|
12
|
+
this.channelByInstance = new Map();
|
|
13
|
+
this.instancesByChannel = new Map();
|
|
14
|
+
this.fallbackChannel = null;
|
|
15
|
+
|
|
16
|
+
this.controller.handle(InstanceActionEvent, this.handleInstanceAction.bind(this));
|
|
17
|
+
await this.connect();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async connect() {
|
|
21
|
+
if (this.client) {
|
|
22
|
+
this.client.destroy();
|
|
23
|
+
this.client = null;
|
|
24
|
+
}
|
|
25
|
+
this.channels = new Map();
|
|
26
|
+
this.channelByInstance = new Map();
|
|
27
|
+
this.instancesByChannel = new Map();
|
|
28
|
+
this.fallbackChannel = null;
|
|
29
|
+
|
|
30
|
+
let token = this.controller.config.get("discord_bridge.bot_token");
|
|
31
|
+
if (!token) {
|
|
32
|
+
this.logger.warn("Bot token not configured, bridge is offline");
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
this.client = new Discord.Client({
|
|
37
|
+
intents: [
|
|
38
|
+
Discord.GatewayIntentBits.Guilds,
|
|
39
|
+
Discord.GatewayIntentBits.GuildMessages,
|
|
40
|
+
Discord.GatewayIntentBits.MessageContent,
|
|
41
|
+
],
|
|
42
|
+
});
|
|
43
|
+
this.client.on("messageCreate", (message) => {
|
|
44
|
+
this.discordMessage(message).catch(err => { this.logger.error(`Unexpected error:\n${err.stack}`); });
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
this.logger.info("Logging in to Discord");
|
|
48
|
+
try {
|
|
49
|
+
await this.client.login(token);
|
|
50
|
+
} catch (err) {
|
|
51
|
+
this.logger.error(`Error logging in to Discord, bridge is offline:\n${err.stack}`);
|
|
52
|
+
this.client.destroy();
|
|
53
|
+
this.client = null;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
this.logger.info("Successfully logged in");
|
|
57
|
+
await this.fetchChannels();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async fetchChannels() {
|
|
61
|
+
this.channels = new Map();
|
|
62
|
+
this.channelByInstance = new Map();
|
|
63
|
+
this.instancesByChannel = new Map();
|
|
64
|
+
this.fallbackChannel = null;
|
|
65
|
+
|
|
66
|
+
if (!this.client) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const fallbackId = this.controller.config.get("discord_bridge.channel_id") || null;
|
|
71
|
+
const wanted = new Set();
|
|
72
|
+
if (fallbackId) {
|
|
73
|
+
wanted.add(fallbackId);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
for (const instance of this.controller.instances.values()) {
|
|
77
|
+
const overrideId = instance.config.get("discord_bridge.channel_id");
|
|
78
|
+
const effectiveId = overrideId || fallbackId;
|
|
79
|
+
if (!effectiveId) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
this.channelByInstance.set(instance.id, effectiveId);
|
|
84
|
+
wanted.add(effectiveId);
|
|
85
|
+
|
|
86
|
+
if (!this.instancesByChannel.has(effectiveId)) {
|
|
87
|
+
this.instancesByChannel.set(effectiveId, new Set());
|
|
88
|
+
}
|
|
89
|
+
this.instancesByChannel.get(effectiveId).add(instance.id);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
for (const id of wanted) {
|
|
93
|
+
try {
|
|
94
|
+
const channel = await this.client.channels.fetch(id);
|
|
95
|
+
if (channel) {
|
|
96
|
+
this.channels.set(id, channel);
|
|
97
|
+
} else {
|
|
98
|
+
this.logger.error(`Channel ID ${id} was not found`);
|
|
99
|
+
}
|
|
100
|
+
} catch (err) {
|
|
101
|
+
if (err.code !== 10003) { // Unknown channel
|
|
102
|
+
throw err;
|
|
103
|
+
}
|
|
104
|
+
this.logger.error(`Channel ID ${id} was not found`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (fallbackId) {
|
|
109
|
+
this.fallbackChannel = this.channels.get(fallbackId) ?? null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getChannelForInstance(instance) {
|
|
114
|
+
const channelId = this.channelByInstance.get(instance.id);
|
|
115
|
+
if (channelId) {
|
|
116
|
+
return this.channels.get(channelId) ?? null;
|
|
117
|
+
}
|
|
118
|
+
// Instance added after the last fetchChannels(): fall back to the global channel.
|
|
119
|
+
return this.fallbackChannel;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async onShutdown() {
|
|
123
|
+
if (this.client) {
|
|
124
|
+
this.client.destroy();
|
|
125
|
+
this.client = null;
|
|
126
|
+
this.channels = new Map();
|
|
127
|
+
this.channelByInstance = new Map();
|
|
128
|
+
this.instancesByChannel = new Map();
|
|
129
|
+
this.fallbackChannel = null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async onControllerConfigFieldChanged(field, curr, prev) {
|
|
134
|
+
if (field === "discord_bridge.bot_token") {
|
|
135
|
+
await this.connect();
|
|
136
|
+
} else if (field === "discord_bridge.channel_id") {
|
|
137
|
+
await this.fetchChannels();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async onInstanceConfigFieldChanged(instance, field, curr, prev) {
|
|
142
|
+
if (field === "discord_bridge.channel_id") {
|
|
143
|
+
await this.fetchChannels();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param message {Discord.Message}
|
|
149
|
+
*/
|
|
150
|
+
async discordMessage(message) {
|
|
151
|
+
if (message.author.bot) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const targetInstanceIds = this.instancesByChannel.get(message.channel.id);
|
|
156
|
+
if (!targetInstanceIds || targetInstanceIds.size === 0) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const template = this.controller.config.get("discord_bridge.discord_template");
|
|
161
|
+
const content = template.replace(/(__display_name__|__username__|__content__)/g, (sub) => ({
|
|
162
|
+
"__display_name__": message.member.displayName,
|
|
163
|
+
"__username__": message.member.user.tag,
|
|
164
|
+
"__content__": message.cleanContent,
|
|
165
|
+
}[sub]));
|
|
166
|
+
|
|
167
|
+
const event = new DiscordChatEvent(content);
|
|
168
|
+
for (const instanceId of targetInstanceIds) {
|
|
169
|
+
this.controller.sendTo({ instanceId }, event);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
formatMessage(template, hostName, instanceName, content) {
|
|
174
|
+
return template.replace(/(__host_name__|__instance_name__|__content__)/g, (sub) => ({
|
|
175
|
+
"__host_name__": hostName,
|
|
176
|
+
"__instance_name__": instanceName,
|
|
177
|
+
"__content__": content,
|
|
178
|
+
}[sub]));
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
getHostName(hostId) {
|
|
182
|
+
return this.controller.hosts.get(hostId)?.name ?? "";
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async onInstanceStatusChanged(instance, prev) {
|
|
186
|
+
if (!this.controller.config.get("discord_bridge.notify_instance_status")) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const channel = this.getChannelForInstance(instance);
|
|
191
|
+
if (!channel) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
let instanceName = instance.config.get("instance.name");
|
|
196
|
+
const hostName = this.getHostName(instance.config.get("instance.assigned_host"));
|
|
197
|
+
|
|
198
|
+
const template =
|
|
199
|
+
this.controller.config.get("discord_bridge.instance_status_templates")[`${prev}:${instance.status}`]
|
|
200
|
+
?? this.controller.config.get("discord_bridge.instance_status_templates")[instance.status]
|
|
201
|
+
;
|
|
202
|
+
|
|
203
|
+
if (!template) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
await channel.send(this.formatMessage(template, hostName, instanceName, ""));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
onHostConnectionEvent(hostConnection, event) {
|
|
211
|
+
if (!this.fallbackChannel || !this.controller.config.get("discord_bridge.notify_host_connections")) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
let hostName = this.controller.hosts.get(hostConnection.id).name;
|
|
216
|
+
const template = this.controller.config.get("discord_bridge.host_templates")[event];
|
|
217
|
+
if (!template) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const message = this.formatMessage(template, hostName, "", "");
|
|
221
|
+
this.fallbackChannel.send(message).catch(
|
|
222
|
+
err => { this.logger.error(`Unexpected error:\n${err.stack}`); }
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async handleInstanceAction(request, src) {
|
|
227
|
+
// Known action types at the time of writing:
|
|
228
|
+
// WARNING COMMAND SHOUT CHAT COLOR JOIN LEAVE KICK BAN UNBANNED PROMOTE DEMOTE
|
|
229
|
+
let { instanceName, action, content } = request;
|
|
230
|
+
const enabled = (
|
|
231
|
+
["JOIN", "LEAVE"].includes(action) && this.controller.config.get("discord_bridge.bridge_player_joins")
|
|
232
|
+
|| action === "CHAT" && this.controller.config.get("discord_bridge.bridge_player_chat")
|
|
233
|
+
|| action === "SHOUT" && this.controller.config.get("discord_bridge.bridge_player_shouts")
|
|
234
|
+
|| (
|
|
235
|
+
["KICK", "BAN", "UNBANNED"].includes(action)
|
|
236
|
+
&& this.controller.config.get("discord_bridge.bridge_player_kicks_and_bans")
|
|
237
|
+
)
|
|
238
|
+
|| action === "COMMAND" && this.controller.config.get("discord_bridge.bridge_player_commands")
|
|
239
|
+
|| (
|
|
240
|
+
["PROMOTE", "DEMOTE"].includes(action)
|
|
241
|
+
&& this.controller.config.get("discord_bridge.bridge_player_promotions")
|
|
242
|
+
)
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
if (!enabled) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const instance = this.controller.instances.get(src.id);
|
|
250
|
+
if (!instance) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
const channel = this.getChannelForInstance(instance);
|
|
254
|
+
if (!channel) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const template = this.controller.config.get("discord_bridge.player_templates")[action];
|
|
259
|
+
if (!template) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
const hostName = this.getHostName(instance.config.get("instance.assigned_host"));
|
|
263
|
+
const message = this.formatMessage(template, hostName, instanceName, content);
|
|
264
|
+
await channel.send(message, { allowedMentions: { parse: [] }});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
module.exports = {
|
|
269
|
+
ControllerPlugin,
|
|
270
|
+
};
|