This shows you the differences between two versions of the page.
| 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 | ||
| - | </ | ||
| ===== 2-minute Intro to IRC ===== | ===== 2-minute Intro to IRC ===== | ||
| Line 63: | Line 56: | ||
| var net = require(' | var net = require(' | ||
| + | var BOT_NAME = ' | ||
| + | var BOT_CHANNEL = '# | ||
| var client = new net.Socket(); | var client = new net.Socket(); | ||
| client.connect(6667, | client.connect(6667, | ||
| - | client.write(' | + | client.write(' | 
| - | client.write(' | + | client.write(' | 
| setTimeout(function(){ | setTimeout(function(){ | ||
| - | client.write(' | + |  | 
| - | client.write(' | + | client.write(' | 
| }, 3000); | }, 3000); | ||
| Line 79: | Line 74: | ||
| client.on(' | client.on(' | ||
| console.log(' | console.log(' | ||
| + | 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(' | + | if (text.startsWith(' | 
| - | client.write(' | + | client.write(' | 
| console.log(' | console.log(' | ||
| - | } else if (data.toString().split(' | + | } else if (splitText[1] == ' | 
| - | // If data comes from a channel | + | // If data comes from a channel | 
| - | // :nanodano1!johnd@107.181.165.217 PRIVMSG #general :Hello bot | + | // | 
| - | // If data comes from privmsg | + | // If data comes from privmsg | 
| - | // :nanodano1!johnd@107.181.165.217 PRIVMSG | + | // | 
| - | console.log(' | + | console.log(' | 
| // Was it a direct message or to the channel? | // Was it a direct message or to the channel? | ||
| - | if (data.toString().split(' | + | if (splitText[2] == BOT_CHANNEL) { | 
| - |  | + |  | 
| } | } | ||
| - | if (data.toString().split(' | + | if (splitText[2] == BOT_NAME) { | 
| - |  | + |  | 
| } | } | ||
| // Was the message a command? | // Was the message a command? | ||
| - | if (data.toString().split(' | + | if (splitText[3].substring(1).startsWith(' | 
| console.log(' | console.log(' | ||
| // 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(' | ||
| + | if (command == ' | ||
| + | console.log(' | ||
| + | client.write(' | ||
| + | } | ||
| } | } | ||
| - | console.log(' | + | console.log(' | 
| } else { | } else { | ||
| - | console.log(' | + | console.log(' | 
| } | } | ||
| - | }); | ||
| + | }); | ||
| </ | </ | ||