{"id":254,"date":"2016-10-18T06:35:47","date_gmt":"2016-10-18T06:35:47","guid":{"rendered":"http:\/\/wizardofbots.com\/network\/?p=254"},"modified":"2016-10-18T06:35:47","modified_gmt":"2016-10-18T06:35:47","slug":"create-an-irc-bot-with-nodejs","status":"publish","type":"post","link":"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/","title":{"rendered":"Create an IRC Bot with NodeJS"},"content":{"rendered":"<p>IRC was the best 10 years ago, now other softwares have eclipsed, but many savvy people keep using it for proper communication with special individuals. So, if you never heard or configured eggdrops, this is something similar.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full\" src=\"https:\/\/media.giphy.com\/media\/JgL05kZwR1NMk\/giphy.gif\" alt=\"\" width=\"537\" height=\"313\" \/><\/p>\n<p>So first you will need to install NodeJS, and I will consider that you already have it, then you should have NPM also installed, then just install the irc library:<\/p>\n<p><strong>npm install irc<\/strong><\/p>\n<p>Once you have that, you need to specify that you are using this module and then set the configure on connecting to know which channels the bot will join automatically, and then set up the port, which is not a regular port for IRC but you need to set something more stable for a bot.<\/p>\n<pre class=\"lang:js decode:true\">var irc = require('irc');\r\nvar bot = new irc.Client('chat.freenode.net','w0bot', {\r\n    channels: ['#botplace', '#w0b'],\r\n    port: 8001,\r\n    debug: true,\r\n    userName: 'wizard', \/\/ on the host is like wizard@host\/ip\r\n    realName: 'Im a bot from Wizard of Bots ;)',  \/\/ real name on whois\r\n    showErrors: true, \r\n    autoRejoin: true, \/\/ auto rejoin channel when kicked\r\n    autoConnect: true, \/\/ persistence to connect\r\n});<\/pre>\n<p>So as you see, first we set up the server, then the nick, and then we open encapsulation to add the channels, the port and a mode where we can see what is going on on your console.<\/p>\n<p><strong>NOTE:<\/strong> You might get some errors when connecting, but try again and again. When you are able to connect, no matter if you sleep your localhost, it will continue connecting. Also this is for leaving it working on a VPS or shell so you have a always online bot.<\/p>\n<p>But now that you have your bot online, what&#8217;s next? You need to know wtf is going on in the channels that you join or the PM you receive, and for that we have listeners.<\/p>\n<p>Before getting into this I will explain the functions we have available so we can use them when we get events on the listeners:<\/p>\n<pre class=\"lang:js decode:true\">bot.join('#yourchannel yourpass'); \/\/ this will join a channel with a pass (or no pass)\r\nbot.part('#yourchannel'); \/\/part a channel\r\nbot.say('#yourchannel', \"I'm a bot for w0b!\"); \/\/ send a message to a channel\r\nbot.whois(nickname, function (whois) {\r\n    console.log(whois); \/\/ you need this callback to log the results once it finished doing the whois\r\nbot.notice(target, \"your message for the notice\"); \/\/target is either a nickname or a channel\r\n});\r\nbot.send('MODE', '#yourchannel', '+o', 'yournick'); \/\/ Gives OP to yournick in #yourchannel\r\n<\/pre>\n<p>So now that you know the commands to use on the Events, we now are listing the Listeners for this stuff:<\/p>\n<pre class=\"lang:js decode:true \">bot.addListener('pm', function (from, message) {\r\n    console.log(from + ' =&gt; ME: ' + message); \/\/ when you get a PM you log into console\r\n    bot.say(from, 'Hello I am a bot from Wizard of Bots '); \/\/ Also you can automatically respond to that message with the command say\r\n});\r\nbot.addListener('message#yourchannel', function (from, message) {\r\n    console.log(from + ' => #yourchannel: ' + message); \/\/ if someone sends a message to a specific channel\r\n});\r\nbot.addListener('join', function(channel, who) {\r\n    console.log('%s has joined %s', who, channel);\r\n    bot.say(who, 'Hello and welcome to ' + channel); \/\/ When someone joins a channel automatically welcome him\r\n});\r\nbot.addListener('kick', function(channel, who, by, reason) {\r\n    console.log('%s was kicked from %s by %s: %s', who, channel, by, reason); \/\/ when someone is kicked log into the console what happend.\r\n});\r\nbot.addListener('part', function(channel, who, reason) {\r\n    console.log('%s has left %s: %s', who, channel, reason); \/\/ when someone part\r\n    \/\/ you can also send a PM to this guy to convince to stay.\r\n});\r\nbot.addListener('message', function(from, to, message) {\r\n    if(  message.indexOf('Know any good jokes?') > -1\r\n      || message.indexOf('good joke') > -1\r\n    ) {\r\n        bot.say(to, 'Knock knock!');\r\n    }\r\n});  \/\/ and in like other eggdrops, if you say those words, it will answer Knock Knock\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>IRC was the best 10 years ago, now other softwares have eclipsed, but many savvy people keep using it for proper communication with special individuals. So, if you never heard or configured eggdrops, this is something similar. So first you will need to install NodeJS, and I will consider that you already have it, then [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[33,15,111],"tags":[42,112,39],"class_list":["post-254","post","type-post","status-publish","format-standard","hentry","category-javascript","category-linux","category-nodejs","tag-irc","tag-irc-bot","tag-nodejs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Create an IRC Bot with NodeJS - Wizard Of Bots<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create an IRC Bot with NodeJS - Wizard Of Bots\" \/>\n<meta property=\"og:description\" content=\"IRC was the best 10 years ago, now other softwares have eclipsed, but many savvy people keep using it for proper communication with special individuals. So, if you never heard or configured eggdrops, this is something similar. So first you will need to install NodeJS, and I will consider that you already have it, then [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/\" \/>\n<meta property=\"og:site_name\" content=\"Wizard Of Bots\" \/>\n<meta property=\"article:published_time\" content=\"2016-10-18T06:35:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/media.giphy.com\/media\/JgL05kZwR1NMk\/giphy.gif\" \/>\n<meta name=\"author\" content=\"wizardofbots\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"wizardofbots\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/\",\"url\":\"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/\",\"name\":\"Create an IRC Bot with NodeJS - Wizard Of Bots\",\"isPartOf\":{\"@id\":\"https:\/\/wizardofbots.com\/network\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/media.giphy.com\/media\/JgL05kZwR1NMk\/giphy.gif\",\"datePublished\":\"2016-10-18T06:35:47+00:00\",\"dateModified\":\"2016-10-18T06:35:47+00:00\",\"author\":{\"@id\":\"https:\/\/wizardofbots.com\/network\/#\/schema\/person\/31f9e486da1c11791d94a861854a2a9f\"},\"breadcrumb\":{\"@id\":\"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/#primaryimage\",\"url\":\"https:\/\/media.giphy.com\/media\/JgL05kZwR1NMk\/giphy.gif\",\"contentUrl\":\"https:\/\/media.giphy.com\/media\/JgL05kZwR1NMk\/giphy.gif\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/wizardofbots.com\/network\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create an IRC Bot with NodeJS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/wizardofbots.com\/network\/#website\",\"url\":\"https:\/\/wizardofbots.com\/network\/\",\"name\":\"Wizard Of Bots\",\"description\":\"Botting and AI community\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/wizardofbots.com\/network\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/wizardofbots.com\/network\/#\/schema\/person\/31f9e486da1c11791d94a861854a2a9f\",\"name\":\"wizardofbots\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/wizardofbots.com\/network\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/2.gravatar.com\/avatar\/584eebc303f64610559ab9f305f6928d?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/2.gravatar.com\/avatar\/584eebc303f64610559ab9f305f6928d?s=96&d=mm&r=g\",\"caption\":\"wizardofbots\"},\"url\":\"http:\/\/wizardofbots.com\/network\/author\/wizardofbots\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create an IRC Bot with NodeJS - Wizard Of Bots","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/","og_locale":"en_US","og_type":"article","og_title":"Create an IRC Bot with NodeJS - Wizard Of Bots","og_description":"IRC was the best 10 years ago, now other softwares have eclipsed, but many savvy people keep using it for proper communication with special individuals. So, if you never heard or configured eggdrops, this is something similar. So first you will need to install NodeJS, and I will consider that you already have it, then [&hellip;]","og_url":"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/","og_site_name":"Wizard Of Bots","article_published_time":"2016-10-18T06:35:47+00:00","og_image":[{"url":"https:\/\/media.giphy.com\/media\/JgL05kZwR1NMk\/giphy.gif"}],"author":"wizardofbots","twitter_card":"summary_large_image","twitter_misc":{"Written by":"wizardofbots","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/","url":"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/","name":"Create an IRC Bot with NodeJS - Wizard Of Bots","isPartOf":{"@id":"https:\/\/wizardofbots.com\/network\/#website"},"primaryImageOfPage":{"@id":"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/#primaryimage"},"image":{"@id":"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/#primaryimage"},"thumbnailUrl":"https:\/\/media.giphy.com\/media\/JgL05kZwR1NMk\/giphy.gif","datePublished":"2016-10-18T06:35:47+00:00","dateModified":"2016-10-18T06:35:47+00:00","author":{"@id":"https:\/\/wizardofbots.com\/network\/#\/schema\/person\/31f9e486da1c11791d94a861854a2a9f"},"breadcrumb":{"@id":"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/#primaryimage","url":"https:\/\/media.giphy.com\/media\/JgL05kZwR1NMk\/giphy.gif","contentUrl":"https:\/\/media.giphy.com\/media\/JgL05kZwR1NMk\/giphy.gif"},{"@type":"BreadcrumbList","@id":"http:\/\/wizardofbots.com\/network\/create-an-irc-bot-with-nodejs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wizardofbots.com\/network\/"},{"@type":"ListItem","position":2,"name":"Create an IRC Bot with NodeJS"}]},{"@type":"WebSite","@id":"https:\/\/wizardofbots.com\/network\/#website","url":"https:\/\/wizardofbots.com\/network\/","name":"Wizard Of Bots","description":"Botting and AI community","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wizardofbots.com\/network\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/wizardofbots.com\/network\/#\/schema\/person\/31f9e486da1c11791d94a861854a2a9f","name":"wizardofbots","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wizardofbots.com\/network\/#\/schema\/person\/image\/","url":"http:\/\/2.gravatar.com\/avatar\/584eebc303f64610559ab9f305f6928d?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/584eebc303f64610559ab9f305f6928d?s=96&d=mm&r=g","caption":"wizardofbots"},"url":"http:\/\/wizardofbots.com\/network\/author\/wizardofbots\/"}]}},"_links":{"self":[{"href":"http:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/posts\/254"}],"collection":[{"href":"http:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/comments?post=254"}],"version-history":[{"count":3,"href":"http:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":257,"href":"http:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/posts\/254\/revisions\/257"}],"wp:attachment":[{"href":"http:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/media?parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/categories?post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/tags?post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}