Skip to content

Commit 0423794

Browse files
authored
Merge pull request #164 from oof2win2/bugfix/server-start-error
2 parents 508a1be + d79334f commit 0423794

File tree

4 files changed

+44
-47
lines changed

4 files changed

+44
-47
lines changed

src/base/Tails.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export declare interface TailEvents {
2525
playerJoin: (data: playerJoinData) => void;
2626
playerLeave: (data: playerLeaveData) => void;
2727
out: (data: OutputData) => void;
28-
start: (data: OutputData) => void;
2928
logging: (data: OutputData) => void;
3029
datastore: (data: OutputData) => void;
3130
discord: (data: OutputData) => void;
@@ -54,9 +53,6 @@ class tailListener extends EventEmitter {
5453
this.tailLocations.forEach((tailStuff) => {
5554
let tail = new Tail(tailStuff.path);
5655
tail.on("line", (line: string) => {
57-
const serverStartRegExp = new RegExp(
58-
/Info ServerMultiplayerManager.cpp:\d\d\d: Matching server connection resumed/
59-
);
6056
if (line.includes("[CHAT]"))
6157
this.client?.ws
6258
? this.client.logger(`${tailStuff.server.name}: ${line}`)
@@ -79,13 +75,10 @@ class tailListener extends EventEmitter {
7975
line: JSON.parse(line),
8076
server: tailStuff.server,
8177
});
82-
else if (serverStartRegExp.test(line))
83-
return this.emit("start", { line: line, server: tailStuff.server });
84-
else
85-
this.emit(tailStuff.type, {
86-
line: line,
87-
server: tailStuff.server,
88-
});
78+
this.emit(tailStuff.type, {
79+
line: line,
80+
server: tailStuff.server,
81+
});
8982
});
9083
});
9184
}

src/helpers/rcon.ts

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ class RconInterface {
3737
*/
3838
private checkIntervals: Map<string, NodeJS.Timeout> = new Map();
3939

40+
public serverConnected: (server: FactorioServer) => void;
41+
4042
constructor(servers: FactorioServer[]) {
4143
this.servers = servers;
4244
this.rconConnections = [];
4345

4446
this.servers.map((_, i) => this.initServer(i));
47+
this.serverConnected = () => {};
4548
}
4649

4750
private async initServer(serverIndex: number) {
@@ -63,32 +66,33 @@ class RconInterface {
6366
server: server,
6467
hasScenario: hasScenario,
6568
});
69+
this.serverConnected(server);
6670

6771
// reconnection mechanism
6872
rcon.on("end", () => {
6973
this.reconnectRcon(rcon, server); // start the reconnection mechanism
70-
if (!server.dev) {
71-
this.client.sendToErrorChannel(
72-
`Server <#${
73-
server.discordid
74-
}> has dropped connection to RCON at <t:${Math.floor(
75-
Date.now() / 1000
76-
)}>`
77-
);
78-
}
74+
if (!server.dev) {
75+
this.client.sendToErrorChannel(
76+
`Server <#${
77+
server.discordid
78+
}> has dropped connection to RCON at <t:${Math.floor(
79+
Date.now() / 1000
80+
)}>`
81+
);
82+
}
7983
});
8084
} catch (error) {
8185
// mechanism to reconnect to RCON after some time
8286
this.reconnectRcon(rcon, server); // start the reconnection mechanism
8387
if (!server.dev) {
84-
this.client.sendToErrorChannel(
85-
`Server <#${
86-
server.discordid
87-
}> has failed to initially connect to RCON at <t:${Math.floor(
88-
Date.now() / 1000
89-
)}>`
90-
);
91-
}
88+
this.client.sendToErrorChannel(
89+
`Server <#${
90+
server.discordid
91+
}> has failed to initially connect to RCON at <t:${Math.floor(
92+
Date.now() / 1000
93+
)}>`
94+
);
95+
}
9296
}
9397
}
9498

@@ -121,6 +125,7 @@ class RconInterface {
121125
Date.now() / 1000
122126
)}>, after ${dayjs(startedAt).fromNow(true)}. Synchronizing banlist`
123127
);
128+
this.serverConnected(server);
124129
return;
125130
} catch {
126131
connectionAttempts++;
@@ -129,14 +134,14 @@ class RconInterface {
129134
// dayjs is used to get the relative time since the start of the reconnection attempts
130135
if (attempts.includes(connectionAttempts)) {
131136
if (!server.dev) {
132-
this.client.sendToErrorChannel(
133-
`Server <#${
134-
server.discordid
135-
}> has dropped connection to RCON at <t:${Math.floor(
136-
Date.now() / 1000
137-
)}>`
138-
);
139-
}
137+
this.client.sendToErrorChannel(
138+
`Server <#${
139+
server.discordid
140+
}> has dropped connection to RCON at <t:${Math.floor(
141+
Date.now() / 1000
142+
)}>`
143+
);
144+
}
140145
}
141146
}
142147
};
@@ -159,19 +164,19 @@ class RconInterface {
159164
s.server.name === serverIdentifier ||
160165
s.server.discordid === serverIdentifier
161166
);
162-
// if (serverIdentifier == "724696348871622818")
163-
// console.log(server)
167+
// if (serverIdentifier == "724696348871622818")
168+
// console.log(server)
164169
if (!server)
165170
return {
166171
resp: false,
167-
identifier: serverIdentifier,
172+
identifier: serverIdentifier,
168173
};
169174
// eslint-disable-next-line @typescript-eslint/no-empty-function
170175
const response = await server.rcon.send(command).catch(() => {});
171176
if (!response)
172177
return {
173178
resp: false,
174-
identifier: serverIdentifier,
179+
identifier: serverIdentifier,
175180
};
176181
return {
177182
resp: response,

src/helpers/serverHandler.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class serverHandler {
3535
Tails.on("datastore", (log) => this.datastoreHandler(log));
3636
Tails.on("discord", (log) => this.discordHandler(log));
3737
// Tails.on("ALL", (log) => this.allHandler(log))
38-
Tails.on("start", (log) => this.startHandler(log));
3938
}
4039
private formatDate(line: string) {
4140
return line.trim().slice(line.indexOf("0.000") + 6, 25);
@@ -545,8 +544,7 @@ class serverHandler {
545544
embeds: [new MessageEmbed(embed)],
546545
});
547546
}
548-
async startHandler(data: OutputData) {
549-
let server = data.server;
547+
async startHandler(server: FactorioServer) {
550548
setTimeout(async () => {
551549
let roles = await Users.find({})
552550
.select({ factorioName: 1, factorioRoles: 1 })
@@ -565,7 +563,7 @@ class serverHandler {
565563
)
566564
.then((output) => output);
567565
if (res.resp && res.resp.trim() == "Command Complete") {
568-
const channel = this.client.channels.cache.get(data.server.discordid);
566+
const channel = this.client.channels.cache.get(server.discordid);
569567
channel.isText() && channel.send("Roles have synced");
570568
}
571569
}, 5000); // allow server to connect to rcon
@@ -580,4 +578,4 @@ class serverHandler {
580578
}
581579
}
582580
}
583-
module.exports = serverHandler;
581+
export default serverHandler;

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const client = new Comfy({
1515
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
1616
Intents.FLAGS.GUILD_PRESENCES,
1717
Intents.FLAGS.GUILD_WEBHOOKS,
18-
Intents.FLAGS.GUILD_MEMBERS,
18+
Intents.FLAGS.GUILD_MEMBERS,
1919
],
2020
});
2121

@@ -69,8 +69,9 @@ const init = async () => {
6969
client.login(client.config.token);
7070

7171
rcon.client = client;
72-
const serverHandler = require("./helpers/serverHandler");
72+
const serverHandler = (await import("./helpers/serverHandler")).default;
7373
const servers = new serverHandler(client);
74+
rcon.serverConnected = (server) => servers.startHandler(server);
7475

7576
mongoose
7677
.connect(client.config.mongoDB)

0 commit comments

Comments
 (0)