Jump to content
UBot Underground

[Nodejs] How To Create An Irc Bot (Listeners/events + Actions)


Recommended Posts

I think this is going to be useful for most of advanced ubotters, This is a way of helping understanding more technology. But if this is not accepted in this forum, please let me know....


 


 


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.


giphy.gif


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.





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:





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:





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




If you like this tut, visit the blog:


http://i.imgur.com/UUGHQQt.jpg


  • Like 2
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...