Skip to content

Commit f8a135d

Browse files
committed
Fixed some faulty code and made username appear bold
1 parent 835574b commit f8a135d

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

src/bot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Bot {
1313
/**
1414
* Creates a new instance of this bot
1515
* @param {String} discordToken The bot token you got from discord
16-
* @param {*} port The port the bot should listen on
16+
* @param {number} port The port the bot should listen on
1717
*/
1818
constructor(discordToken, port) {
1919
this.#msgRecv = new MessageReceiver(port);

src/network/messageReceiver.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ class MessageReceiver {
2727
*/
2828
#onConnection(socket) {
2929
socket.on("data", chunk => this.#boundChannels.forEach(channel => {
30-
var jsonObj = JSON.parse(chunk.toString()); // A message will never be larger than a chunk can handle so this should be fine
31-
var msg = (jsonObj["sender"] === undefined) ? jsonObj["message"] : `${jsonObj["sender"]}: ${jsonObj["message"]}`
32-
channel.send(msg);
30+
try {
31+
var jsonObj = JSON.parse(chunk.toString()); // A message will never be larger than a chunk can handle so this should be fine
32+
var msg = (jsonObj["sender"] === undefined) ? jsonObj["message"] : `**${jsonObj["sender"]}:** ${jsonObj["message"]}`
33+
channel.send(msg);
34+
} catch {
35+
console.log(`Invalid JSON: ${chunk.toString()}`);
36+
channel.send("Message data lost");
37+
}
3338
}));
3439
}
3540

0 commit comments

Comments
 (0)