1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Create an IRC Bot with NodeJS

Discussion in 'Programming' started by rodvan, Dec 2, 2016.

  1. rodvan

    rodvan Jr. VIP Jr. VIP

    Joined:
    Jul 27, 2010
    Messages:
    1,293
    Likes Received:
    492
    Occupation:
    developer, marketing, automation, machine learning
    Location:
    Wizard of Bots
    Home Page:
    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.

    [​IMG]

    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:

    npm install irc

    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.
    Code:
    var irc = require('irc');
    var bot = new irc.Client('chat.freenode.net','w0bot', {
        channels: ['#botplace', '#w0b'],
        port: 8001,
        debug: true,
        userName: 'wizard', // on the host is like wizard@host/ip
        realName: 'Im a bot from Wizard of Bots ;)',  // real name on whois
        showErrors: true,
        autoRejoin: true, // auto rejoin channel when kicked
        autoConnect: true, // persistence to connect
    });
    
    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.

    NOTE: 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.

    But now that you have your bot online, what’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.

    Before getting into this I will explain the functions we have available so we can use them when we get events on the listeners:


    Code:
    bot.join('#yourchannel yourpass'); // this will join a channel with a pass (or no pass)
    bot.part('#yourchannel'); //part a channel
    bot.say('#yourchannel', "I'm a bot for w0b!"); // send a message to a channel
    bot.whois(nickname, function (whois) {
        console.log(whois); // you need this callback to log the results once it finished doing the whois
    bot.notice(target, "your message for the notice"); //target is either a nickname or a channel
    });
    bot.send('MODE', '#yourchannel', '+o', 'yournick'); // Gives OP to yournick in #yourchannel
    
    So now that you know the commands to use on the Events, we now are listing the Listeners for this stuff:

    Code:
    bot.addListener('pm', function (from, message) {
        console.log(from + ' => ME: ' + message); // when you get a PM you log into console
        bot.say(from, 'Hello I am a bot from Wizard of Bots '); // Also you can automatically respond to that message with the command say
    });
    bot.addListener('message#yourchannel', function (from, message) {
        console.log(from + ' => #yourchannel: ' + message); // if someone sends a message to a specific channel
    });
    bot.addListener('join', function(channel, who) {
        console.log('%s has joined %s', who, channel);
        bot.say(who, 'Hello and welcome to ' + channel); // When someone joins a channel automatically welcome him
    });
    bot.addListener('kick', function(channel, who, by, reason) {
        console.log('%s was kicked from %s by %s: %s', who, channel, by, reason); // when someone is kicked log into the console what happend.
    });
    bot.addListener('part', function(channel, who, reason) {
        console.log('%s has left %s: %s', who, channel, reason); // when someone part
        // you can also send a PM to this guy to convince to stay.
    });
    bot.addListener('message', function(from, to, message) {
        if(  message.indexOf('Know any good jokes?') > -1
          || message.indexOf('good joke') > -1
        ) {
            bot.say(to, 'Knock knock!');
        }
    });  // and in like other eggdrops, if you say those words, it will answer Knock Knock
    
     
  2. towelfox

    towelfox Junior Member

    Joined:
    Jan 11, 2012
    Messages:
    173
    Likes Received:
    55
    But what's the point? You even mentioned eggdrop in the OP. I've not been active with IRC in 15-20 years but I'm pretty sure there's more feature rich stuff already about that would still compile.

    Thanks for the memory jolt though. If it wasn't for the mIRC scripting I did back then I probably wouldn't have got into other stuff and if not for desperately wanting to like bitchx I'd have never installed Linux (red hat 5 if you were wondering) so always fond memories of IRC

    Cheers