File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ) ;
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments