Skip to content

Commit dab5360

Browse files
committed
Bound channels are now stored in a JSON file
1 parent 4389f4f commit dab5360

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/bot.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const Discord = require("discord.js");
2+
const fs = require("fs");
23
const client = new Discord.Client();
34
const MessageReceiver = require("./network/messageReceiver.js");
45
const MessageTransmitter = require("./network/messageTransmitter.js");
@@ -12,6 +13,7 @@ class Bot {
1213
#msgRecv;
1314
#txAddress;
1415
#txPort;
16+
#BOUND_CHANNELS_FILE = "bound_channels.json";
1517

1618
/**
1719
* Creates a new instance of this bot
@@ -23,11 +25,14 @@ class Bot {
2325
constructor(discordToken, rxPort = 500, txAddress = "127.0.0.1", txPort = 501) {
2426
this.#txAddress = txAddress;
2527
this.#txPort = txPort;
26-
this.#msgRecv = new MessageReceiver(rxPort);
28+
this.#msgRecv = new MessageReceiver(rxPort, this.#BOUND_CHANNELS_FILE);
2729

2830
client.on("ready", () => {
2931
console.log("Connected as " + client.user.tag);
3032
client.user.setActivity("type --help for help");
33+
34+
// Reads from this.#BOUND_CHANNELS_FILE and if it doesn't exist, it creates it, if it does exist, it binds all the channels
35+
fs.stat(this.#BOUND_CHANNELS_FILE, (err, _) => (err != null) ? fs.writeFile(this.#BOUND_CHANNELS_FILE, "[]", err => err && console.log(err)) : fs.readFile(this.#BOUND_CHANNELS_FILE, (_, data) => JSON.parse(data.toString()).forEach(async channelID => this.#msgRecv.bindChannel(await client.channels.fetch(channelID)))));
3136
});
3237

3338
client.on("message", this.#messageHandler.bind(this));

src/network/messageReceiver.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const {TextChannel} = require("discord.js");
2+
const fs = require("fs");
23
const Net = require("net");
34

45
/**
@@ -7,13 +8,15 @@ const Net = require("net");
78
class MessageReceiver {
89
#socketServer;
910
#boundChannels;
11+
#BOUND_CHANNELS_FILE;
1012
#buffer = "";
1113

1214
/**
1315
* Opens a new server socket on a specified port
1416
* @param {number} port The port the server socket should bind to
1517
*/
16-
constructor(port) {
18+
constructor(port, boundChannelsFileName) {
19+
this.#BOUND_CHANNELS_FILE = boundChannelsFileName;
1720
this.#socketServer = new Net.Server();
1821
this.#boundChannels = [];
1922

@@ -53,12 +56,19 @@ class MessageReceiver {
5356
});
5457
}
5558

59+
#saveBoundChannels() {
60+
var channelIDs = [];
61+
this.#boundChannels.forEach(channel => channelIDs.push(channel.id));
62+
fs.writeFile(this.#BOUND_CHANNELS_FILE, JSON.stringify(channelIDs), err => err && console.log(err));
63+
}
64+
5665
/**
5766
* Binds a text channel to the bound Minecraft server text chat
5867
* @param {TextChannel} channel The channel to bind
5968
*/
6069
bindChannel(channel) {
6170
this.#boundChannels.push(channel);
71+
this.#saveBoundChannels();
6272
}
6373

6474
/**
@@ -68,6 +78,7 @@ class MessageReceiver {
6878
unbindChannel(channel) {
6979
var elementIndex = this.#boundChannels.indexOf(channel);
7080
this.#boundChannels.splice(elementIndex, 1);
81+
this.#saveBoundChannels();
7182
}
7283

7384
/**

0 commit comments

Comments
 (0)