User Tools

Site Tools


programming:irc

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
programming:irc [2021/06/13 07:05]
nanodano
programming:irc [2022/03/13 16:49] (current)
nanodano
Line 3: Line 3:
 IRC stands for Internet Relay Chat. IRC stands for Internet Relay Chat.
  
-===== DevDungeon IRC Server ===== 
- 
-Connect to the DevDungeon IRC server at ''irc.devdungeon.com'' on port ''6667''. 
- 
-<code> 
-irc.devdungeon.com 
-</code> 
  
 ===== 2-minute Intro to IRC ===== ===== 2-minute Intro to IRC =====
Line 63: Line 56:
 var net = require('net');  // https://nodejs.org/dist/latest/docs/api/net.html var net = require('net');  // https://nodejs.org/dist/latest/docs/api/net.html
  
 +var BOT_NAME = 'mybot2';
 +var BOT_CHANNEL = '#bots';
  
 var client = new net.Socket(); var client = new net.Socket();
  
 client.connect(6667, 'irc.devdungeon.com', function() { client.connect(6667, 'irc.devdungeon.com', function() {
-  client.write('USER dano 0 * :John Leon\n'); +  client.write('USER ' + BOT_NAME +  ' 0 * :' + BOT_NAME + '\n'); 
-  client.write('NICK dano\n'); // Must wait a second to register+  client.write('NICK ' + BOT_NAME + '\n'); // Must wait a second to register
  
   setTimeout(function(){    setTimeout(function(){ 
-     client.write('JOIN #general\n'); +      client.write('JOIN ' + BOT_CHANNEL + '\n'); 
-      client.write('PRIVMSG #general Hello from my Node.js bot!\n');+      client.write('PRIVMSG ' + BOT_CHANNEL + ' Hello my Node.js bot is online!\n');
    }, 3000);    }, 3000);
        
Line 79: Line 74:
 client.on('data', function(data) { client.on('data', function(data) {
   console.log('MESSAGE_FROM_SERVER: ' + data);   console.log('MESSAGE_FROM_SERVER: ' + data);
 + var text = data.toString(); 
 + var splitText = text.split(' ');
   // If data starts with PING, respond with PONG :whatever   // If data starts with PING, respond with PONG :whatever
-  if (data.toString().startsWith('PING')) { +  if (text.startsWith('PING')) { 
-    client.write('PONG ' + data.toString().substring(5));  // Remove `PING ` and pong back what it sent me+    client.write('PONG ' + text.substring(5));  // Remove `PING ` and pong back what it sent me
     console.log('Recieved PING. PONGED.');     console.log('Recieved PING. PONGED.');
-  } else if (data.toString().split(' ')[1] == 'PRIVMSG') { +  } else if (splitText[1] == 'PRIVMSG') { // PM or chat room 
-    // If data comes from a channel +    // If data comes from a channel (#general): 
-    // :nanodano1!johnd@107.181.165.217 PRIVMSG #general :Hello bot +    //   :nanodano!johnd@107.181.165.217 PRIVMSG #general :Hello bot 
-    // If data comes from privmsg +    // If data comes from privmsg (dano): 
-    // :nanodano1!johnd@107.181.165.217 PRIVMSG dano :hey +    //   :nanodano!johnd@107.181.165.217 PRIVMSG danobot :hey 
-    console.log('Destination:'data.toString().split(' ')[2])+    console.log('Destination:'splitText[2])
     // Was it a direct message or to the channel?     // Was it a direct message or to the channel?
-    if (data.toString().split(' ')[2] == '#general') { +    if (splitText[2] == BOT_CHANNEL) { 
-       console.log('message was from general chat')+       console.log('message was from ' + BOT_CHANNEL)
     }      } 
-    if (data.toString().split(' ')[2] == 'dano') { +    if (splitText[2] == BOT_NAME) { 
-       console.log('message was directly to me chat')+       console.log('message was directly to me (' + BOT_NAME + ')')
     }     }
     // Was the message a command?     // Was the message a command?
-    if (data.toString().split(' ')[3].substring(1).startsWith('!')) {+    if (splitText[3].substring(1).startsWith('!')) {
       console.log('Command detected with ! prefix')       console.log('Command detected with ! prefix')
       // Cut off first character, and check command. Run appropriate function. !bitcoin. !ping !8-ball       // Cut off first character, and check command. Run appropriate function. !bitcoin. !ping !8-ball
 +      var command = splitText[3].substring(2).trim().toLowerCase();
 +      console.log('Command: `' + command + '`');
 +      if (command == 'ping') { 
 +        console.log('Ping command detected! Ponging.');
 +        client.write('PRIVMSG ' + BOT_CHANNEL + ' Pong\n');
 +      }
     }     }
-    console.log('Message:' + data.toString().split(' ')[3].substring(1)); // Cut off : in front+    console.log('PrivMsg: ' + splitText[3].substring(1)); // Cut off : in front    
   } else {   } else {
-    console.log('discarded. unimportant or unplanned');+    console.log('Discarded unexpected/unimportant message.');
   }   }
-}); 
  
 +});
 </code> </code>
  
programming/irc.1623567938.txt.gz · Last modified: 2021/06/13 07:05 by nanodano