Code Monkey home page Code Monkey logo

jerk's Introduction

Jerk

A fun little IRC bot library for node.js. Ridiculously simple to set-up and get going!

OHMYGOD

Seriously, it's stupidly simple.

Your First Bot

Firstly, we'll need to grab Jerk. If you use npm it's as easy as:

npm install jerk

If you prefer straight-up git:

git clone git://github.com/gf3/Jerk.git

Hoo haa, now that we're locked and loaded, let's write a goddamn bot! We need to include Jerk:

var jerk = require( 'jerk' )

You'll need some options. Jerk takes the exact same options object as the IRC-js library. Let's just go ahead and supply some basic info:

var options =
  { server: 'irc.freenode.net'
  , nick: 'YourBot9001'
  , channels: [ '#your-channel' ]
  }

Hah, now you're going to cry once you see how easy this is:

jerk( function( j ) {

  j.watch_for( 'soup', function( message ) {
    message.say( message.user + ': soup is good food!' )
  })

  j.watch_for( /^(.+) are silly$/, function( message ) {
    message.say( message.user + ': ' + message.match_data[1] + ' are NOT SILLY. Don\'t joke!' )
  })

}).connect( options )

Really. That's it.

ADVANCED USER OF THE INTERNETS

The jerk object (j) has only one method: watch_for. Which takes two arguments, the first can be either a string or a regex to match messages against. The second argument is your hollaback function for when a match is found. The hollaback receives only one argument, the message object. It looks like this:

{ user:       String
, source:     Channel
, match_data: Array
, say:        Function( message )
, msg:        Function( message )
}

One thing I will tell you though, is the say method is smart enough to reply to the context that the message was received, so you don't need to pass it any extra info, just a reply :) However, the msg method can be used if you'd like to force sending a message directly to a user (aka a PM).

Cast source to a string to return the channel name. You can also work out who is in a channel by iterating over source.clients.

The connect method returns an object with some handy methods that you can use outside of your watch_fors:

{ say:    Function( destination, message )
, action: Function( destination, action )
, forget: Function( pattern )
, part:   Function( channel )
, join:   Function( channel )
, quit:   Function( message )
}

Example:

var superBot = jerk( ... ).connect( options )
// Later...
superBot.say( '#myChan', 'Soup noobs?' )
superBot.join( '#haters' )
superBot.action( '#hates', 'hates all of you!' )

I think everything there is pretty self-explanatory, no?

Running Your Bot

node yourBot9001.js

Run your bot on a remote server:

nohup node yourBot9001.js &

Although I recommend using something like forever to keep your bot running for a while.

Done.

A Better Example

Here's a more practical example, meet protobot. Protobot hangs out on Freenode#prototype all day – stop by and say hi!

A few bots using Jerk:

Wrote a bot with Jerk? Email me and I'll add it to the list!

Credit & Junk

{ "author" : "Gianni Chiappetta <[email protected]> (http://gf3.ca)"
, "contributors" :
  [ "Isaac Z. Schlueter <[email protected]> (http://blog.izs.me)"
  , "Arnaud Berthomier <[email protected]> (http://wtf.cyprio.net)"
  , "Suresh Harikrishnan <[email protected]> (http://www.activesphere.com)"
  , "Tomás Senart <[email protected]> http://about.me/tsenart"
  ]
}

Jerk is UNLICENSED.

jerk's People

Contributors

autonome avatar gf3 avatar hsuresh avatar isaacs avatar michaeltwofish avatar scoates avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jerk's Issues

Add reference to underlying irc-js object

It would be useful to know properties of the irc-js instance at times:

  j.watch_for(/^(.*?): leave/i, function (msg) {
    if (msg.match_data[1] == j.bot.options.nick)
      j.part(msg.source);
  });

At the moment, one has to call e.g. jerkinstance.say("", "foo") to get a reference to the underlying irc-js layer.

A bug in error handling in connect

I am getting this stack trace when I accidentally set server name to x127.0.0.1.

There is a bug in error handling on connect, writing to console throws an exception.

If I replace message.params[0] with just message I get following error message:

There was an error! "Error: ENOTFOUND, Domain name not found"

/home/jsyrjala/node_modules/jerk/lib/jerk.js:39
        console.log( 'There was an error! "' + message.params[0] + '"' )
                                                             ^
TypeError: Cannot read property '0' of undefined
    at IRC.<anonymous> (/home/jsyrjala/node_modules/jerk/lib/jerk.js:39:62)
    at EventEmitter.<anonymous> (native)
    at EventEmitter.emit (events.js:64:17)
    at Socket.<anonymous> (/home/jsyrjala/node_modules/jerk/node_modules/irc-js/lib/irc.js:172:17)
    at Socket.emit (events.js:64:17)
    at net.js:720:14
    at dns.js:198:11
    at IOWatcher.callback (dns.js:74:15)

Remove node_modules directory?

Currently jerk has node_modules directory that contains a version of irc-js library. However the jerk's package.json lists irc-js as dependecy.

I think that npm will download irc-js just based on package.json when installing jerk, so jerk's node_modules directory could be removed complitely.

Multi-line messages out of order

When I send a multiline message (i.e. a message with \n's separating lines of text), the messages often appear out of order in IRC.

Is this an issue with IRC in general, or something specific to Jerk or irc.js?

Issues with UnrealIRCd

I think this is a problem with Unreal, not me, but it can't connect:

callum-imac:Jerk callumacrae$ node index.js
[Mon, 25 Apr 2011 20:07:52 GMT] INFO       [RECV]  ERROR :Closing Link: [88.108.23.26] (Ping timeout)
[Mon, 25 Apr 2011 20:07:52 GMT] INFO       [SENT]  NICK nodeXB
[Mon, 25 Apr 2011 20:07:52 GMT] INFO       [SENT]  USER js-irc 0 * :Javascript Bot

events.js:47
        throw new Error("Uncaught, unspecified 'error' event.");
              ^
Error: Uncaught, unspecified 'error' event.
    at EventEmitter.emit (events.js:47:15)
    at IRC.parseMessage (/Users/callumacrae/node_apps/Jerk/node_modules/.npm/irc-js/0.2.17/package/lib/irc.js:138:15)
    at Socket.<anonymous> (native)
    at Socket.emit (events.js:64:17)
    at Socket._onReadable (net.js:665:31)
    at IOWatcher.onReadable [as callback] (net.js:177:10)

I think this is because Unreal requires the user to respond to pings with a value, eg:

PING blablabla
PONG blablabla

Parse Error with specific User Names

This is probably an issue with IRC-JS, but it may be an issue with Jerk.

If I have a bot in a room and someone with a username like this: davglass\test` joins, it throws a syntax error:

[Thu, 20 Jan 2011 15:31:24 GMT] ERROR [ERROR] Failed parsing ':davglass\test`!~[email protected] JOIN :#yui'

/usr/local/lib/node/.npm/jerk/1.1.1/package/node_modules/.npm/irc-js/0.2.11/package/lib/irc.js:136
throw err
^
Error: treeWalker: parse failed: 0 parse already failed
at err (/usr/local/lib/node/.npm/jerk/1.1.1/package/node_modules/.npm/irc-js/0.2.11/package/node_modules/PanPG/PanPG_util.js:157:9)
at Object.treeWalker (/usr/local/lib/node/.npm/jerk/1.1.1/package/node_modules/.npm/irc-js/0.2.11/package/node_modules/PanPG/PanPG_util.js:104:10)
at Object.walk (/usr/local/lib/node/.npm/jerk/1.1.1/package/node_modules/.npm/irc-js/0.2.11/package/lib/walker.js:36:21)
at Object.compile (/usr/local/lib/node/.npm/jerk/1.1.1/package/node_modules/.npm/irc-js/0.2.11/package/lib/compiler.js:8:17)
at IRC.parseMessage (/usr/local/lib/node/.npm/jerk/1.1.1/package/node_modules/.npm/irc-js/0.2.11/package/lib/irc.js:116:28)
at Stream. (native)
at Stream.emit (events:27:15)
at IOWatcher.callback (net:484:33)
at node.js:773:9

I can stop this by setting options.log: false in the config, but that doesn't help me since I like my logging.

Get rid of `require.paths`

Error: require.paths is removed. Use node_modules folders, or the NODE_PATH environment variable instead.

(Node v0.5.5)

.connect() takes a long time to join channels

hacking together a bot for some internal use and jerk(function() {}).connect(options) connects (to freenode) quickly, but takes a very long time to join the channels listed in options['channels'] - making it not that fun to hack on.

I'm not sure if this is an issue in jerk or irc-js...

Multiple instances

I see that you've coded this in such a way that, even if you try to initialise it twice and pass two different sets of options, it will use the same instance.

I'm porting a bot of mine from python to JS, it involves having two connections running simultaneously and the two 'bots' communicating with each other.

Is it possible for you to alter it so this is possible?

Error: print() has moved. Use require('sys') to bring it back.

with

[josh@eevee lib]$ node --version
v0.1.100-9-gb1901cd

got this:

node> require('./jerk')
Error: print() has moved. Use require('sys') to bring it back.
    at node.js:11:11
    at Function.bind (native)
    at new Jerk (/home/josh/lulzbot/lib/Jerk/lib/jerk.js:11:26)
    at Object.<anonymous> (/home/josh/lulzbot/lib/Jerk/lib/jerk.js:8:12)
    at Module._compile (module:424:23)
    at Module._loadScriptSync (module:434:8)
    at Module.loadSync (module:306:10)
    at loadModule (module:251:12)
    at require (module:374:12)
    at cwdRequire (repl:26:10)

Honestly, I have no clue where to start with this one.

Long messages seem to be truncated

I'm assuming this is a problem with the bot and not my client or the server because I'm able to manually send messages that are longer.

The messages seem to be cut off at around 400 - 500 characters in length. I will see if I can work around this by manually breaking the message apart, but I was hoping that jerk might do this for me instead?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.