Skip to content

Commit 1721ff1

Browse files
authored
Merge pull request #158 from oof2win2/bugfix/rcon
2 parents cb45bca + 3a4122d commit 1721ff1

File tree

7 files changed

+49
-41
lines changed

7 files changed

+49
-41
lines changed

src/base/Comfy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Comfy extends Client {
7878
});
7979
});
8080
setInterval(() => {
81-
this.serverQueues.forEach((server) => {
81+
this.serverQueues.forEach(async (server) => {
8282
if (server.sendingMessage === true) return;
8383
server.sendingMessage = true;
8484
let message = "";
@@ -98,7 +98,7 @@ class Comfy extends Client {
9898
}
9999
}
100100
if (message.length) {
101-
const channel = this.channels.cache.get(server.server.discordid);
101+
const channel = await this.channels.fetch(server.server.discordid);
102102
if (channel.isText())
103103
channel.send(message).then(() => (server.sendingMessage = false));
104104
} else {

src/commands/Factorio/getroles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const Getroles: Command<Message> = {
2323
const id =
2424
message.mentions.users.first()?.id || args[0] || message.author.id;
2525
const user = await client.findOrCreateUser({ id: id });
26-
if (user.factorioRoles === [])
26+
if (user.factorioRoles.length === 0)
2727
return message.channel.send(`User has no roles!`);
2828
else
2929
message.channel.send(

src/commands/Factorio/onlineplayers.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,28 @@ const OnlinePlayers: Command<Message> = {
2424
const serversWithScenario = rcon.rconConnections
2525
.filter((connection) => connection.hasScenario)
2626
.filter((connection) => connection.server.hidden === false)
27-
.map((connection) => connection.server.discordname);
27+
.map((connection) => connection.server.name);
2828
const serversWithoutScenario = rcon.rconConnections
2929
.filter((connection) => !connection.hasScenario)
3030
.filter((connection) => connection.server.hidden === false)
31-
.map((connection) => connection.server.discordname);
31+
.map((connection) => connection.server.name);
3232

33-
const scenarioOutputProm = serversWithScenario.map((discordname) =>
33+
const scenarioOutputProm = serversWithScenario.map((name) =>
3434
rcon.rconCommand(
3535
"/interface t = {}; for _, player in ipairs(game.connected_players) do t[player.name] = Roles.get_player_highest_role(player).name end; rcon.print(game.table_to_json(t))",
36-
discordname
36+
name
3737
)
3838
);
3939
const withoutScenarioOutputProm = serversWithoutScenario.map(
40-
(discordname) => rcon.rconCommand("/p o", discordname)
40+
(name) => rcon.rconCommand("/p o", name)
4141
);
4242
const scenarioOutput = await Promise.all(scenarioOutputProm);
4343
const withoutScenarioOutput = await Promise.all(withoutScenarioOutputProm);
4444

4545
scenarioOutput.forEach((response) => {
4646
if (!response.resp)
4747
return embed.addField(
48-
response.server.discordname,
48+
response.resp != false ? response.server.discordname : response.identifier,
4949
"Server is unreachable"
5050
);
5151
const playerRoles = JSON.parse(
@@ -63,7 +63,7 @@ const OnlinePlayers: Command<Message> = {
6363
withoutScenarioOutput.forEach((response) => {
6464
if (!response.resp || typeof response.resp !== "string")
6565
return embed.addField(
66-
response.server.discordname,
66+
response.resp != false ? response.server.discordname : response.identifier,
6767
"Server is unreachable"
6868
);
6969
const players = response.resp

src/commands/Moderation/rconcmdall.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ const RconcmdAll: Command<Message> = {
4141
if (out.resp && out.resp.length > 1024)
4242
throw Error("Response too long!");
4343
else
44-
outEmbed.addField(`${out.server.discordname}`, out.resp.toString());
44+
outEmbed.addField(`${out.resp != false ? out.server.discordname : out.identifier}`, out.resp.toString());
4545
} catch (error) {
46-
outEmbed.addField(`${out.server.discordname}`, error);
46+
outEmbed.addField(`${out.resp != false ? out.server.discordname : out.identifier}`, error);
4747
console.error(error);
4848
}
4949
});

src/events/guild/guildMemberUpdate.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ export default async (
3030

3131
Object.keys(client.config.factorioRoles).map((name) => {
3232
const role = client.config.factorioRoles[name];
33-
console.log(role);
3433
if (shouldHaveRoleIDs.includes(role.id)) factorioRoles.push(role.name);
3534
});
3635
user.factorioRoles.forEach((role) => {
3736
if (!factorioRoles.includes(role)) {
38-
console.log(role);
3937
rolesToUnassign.push(role);
4038
}
4139
});
@@ -51,8 +49,9 @@ export default async (
5149
}
5250
factorioRoles.forEach((role) => {
5351
rcon.rconCommandAll(
54-
`/interface Roles.assign_player('${user.factorioName}', '${role}', "JammyBot")`
52+
`/interface Roles.assign_player('${user.factorioName}', '${role}', "JammyBot", true)`
5553
);
5654
});
55+
await user.save()
5756
}
5857
};

src/helpers/rcon.ts

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface RCONCommandSuccessOutput {
1818
}
1919
interface RCONCommandFailOutput {
2020
resp: false;
21-
server: FactorioServer;
21+
identifier: string;
2222
}
2323
type RCONCommandOutput = RCONCommandSuccessOutput | RCONCommandFailOutput;
2424

@@ -30,7 +30,7 @@ interface Connection {
3030

3131
class RconInterface {
3232
private servers: FactorioServer[];
33-
private client: Comfy | null;
33+
public client: Comfy | null;
3434
public rconConnections: Connection[];
3535
/**
3636
* Intervals of checking if a server is online
@@ -67,24 +67,28 @@ class RconInterface {
6767
// reconnection mechanism
6868
rcon.on("end", () => {
6969
this.reconnectRcon(rcon, server); // start the reconnection mechanism
70-
this.client.sendToErrorChannel(
71-
`Server <#${
72-
server.discordid
73-
}> has dropped connection to RCON at <t:${Math.floor(
74-
Date.now() / 1000
75-
)}>`
76-
);
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+
}
7779
});
7880
} catch (error) {
7981
// mechanism to reconnect to RCON after some time
8082
this.reconnectRcon(rcon, server); // start the reconnection mechanism
81-
this.client.sendToErrorChannel(
82-
`Server <#${
83-
server.discordid
84-
}> has failed to initially connect to RCON at <t:${Math.floor(
85-
Date.now() / 1000
86-
)}>`
87-
);
83+
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+
}
8892
}
8993
}
9094

@@ -124,13 +128,15 @@ class RconInterface {
124128
if (connectionAttempts === 11520) connectionAttempts = 5760;
125129
// dayjs is used to get the relative time since the start of the reconnection attempts
126130
if (attempts.includes(connectionAttempts)) {
127-
this.client.sendToErrorChannel(
128-
`Server <#${
129-
server.discordid
130-
}> has been unable to connect to RCON since ${dayjs(
131-
startedAt
132-
).fromNow()}`
133-
);
131+
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+
}
134140
}
135141
}
136142
};
@@ -153,17 +159,19 @@ class RconInterface {
153159
s.server.name === serverIdentifier ||
154160
s.server.discordid === serverIdentifier
155161
);
162+
// if (serverIdentifier == "724696348871622818")
163+
// console.log(server)
156164
if (!server)
157165
return {
158166
resp: false,
159-
server: server.server,
167+
identifier: serverIdentifier,
160168
};
161169
// eslint-disable-next-line @typescript-eslint/no-empty-function
162170
const response = await server.rcon.send(command).catch(() => {});
163171
if (!response)
164172
return {
165173
resp: false,
166-
server: server.server,
174+
identifier: serverIdentifier,
167175
};
168176
return {
169177
resp: response,

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { readdir } from "fs/promises";
44
import mongoose from "mongoose";
55
import { Intents } from "discord.js";
66
import Comfy from "./base/Comfy";
7+
import rcon from "./helpers/rcon";
78

89
const client = new Comfy({
910
intents: [
@@ -14,6 +15,7 @@ const client = new Comfy({
1415
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
1516
Intents.FLAGS.GUILD_PRESENCES,
1617
Intents.FLAGS.GUILD_WEBHOOKS,
18+
Intents.FLAGS.GUILD_MEMBERS,
1719
],
1820
});
1921

@@ -66,7 +68,6 @@ const init = async () => {
6668

6769
client.login(client.config.token);
6870

69-
let rcon = require("./helpers/rcon");
7071
rcon.client = client;
7172
const serverHandler = require("./helpers/serverHandler");
7273
const servers = new serverHandler(client);

0 commit comments

Comments
 (0)