File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11const Discord = require ( "discord.js" ) ;
22const client = new Discord . Client ( ) ;
33const MessageReceiver = require ( "./network/messageReceiver.js" ) ;
4+ const MessageTransmitter = require ( "./network/messageTransmitter.js" ) ;
45
56class Bot {
67 #commands = [ "--bindchannel" , "--unbindchannel" ] . sort ( ) ;
@@ -32,6 +33,9 @@ class Bot {
3233 * @param {Discord.Message } msg
3334 */
3435 #messageHandler( msg ) {
36+ if ( msg . author . bot )
37+ return ;
38+
3539 switch ( msg . content ) {
3640 case "--bindchannel" :
3741 this . #msgRecv. bindChannel ( msg . channel ) ;
@@ -44,6 +48,19 @@ class Bot {
4448 this . #commands. forEach ( commandStr => response += `\`${ commandStr } \`: ${ this . #commandHelp[ commandStr ] } \n` ) ;
4549 msg . channel . send ( response . slice ( 0 , response . length - 1 ) ) ;
4650 break ;
51+ default :
52+ if ( ! this . #msgRecv. isBound ( msg . channel ) )
53+ break ;
54+
55+ MessageTransmitter . transmit (
56+ "127.0.0.1" ,
57+ 501 ,
58+ {
59+ sender : msg . member . displayName ,
60+ message : msg . content
61+ }
62+ ) ;
63+ break ;
4764 }
4865 }
4966}
Original file line number Diff line number Diff line change @@ -69,6 +69,14 @@ class MessageReceiver {
6969 var elementIndex = this . #boundChannels. indexOf ( channel ) ;
7070 this . #boundChannels. splice ( elementIndex , 1 ) ;
7171 }
72+
73+ /**
74+ * Checks if a certain channel is bound to the bound Minecraft server text chat
75+ * @param {TextChannel } channel The channel to check
76+ */
77+ isBound ( channel ) {
78+ return this . #boundChannels. indexOf ( channel ) != - 1 ;
79+ }
7280}
7381
7482module . exports = MessageReceiver ;
Original file line number Diff line number Diff line change 1+ const Net = require ( "net" ) ;
2+
3+ class MessageTransmitter {
4+ /**
5+ * Transmits a message over TCP
6+ * @param {String } address The host to send the message to
7+ * @param {number } port The host's port
8+ * @param {Object } message A JSON object representing the message to send
9+ */
10+ static transmit ( address , port , message ) {
11+ var socket = new Net . Socket ( ) ;
12+ socket . connect ( port , address , ( ) => socket . write ( JSON . stringify ( message ) ) ) ;
13+ socket . on ( "close" , _ => socket . destroy ( ) ) ;
14+ }
15+ }
16+
17+ module . exports = MessageTransmitter ;
You can’t perform that action at this time.
0 commit comments