Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve README.md and add more detailed middleware example #298

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 60 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,83 +15,91 @@ A better IRC framework for node.js. For bots and full clients. Read the [documen
~~~javascript
var bot = new IRC.Client();
bot.connect({
host: 'irc.freenode.net',
port: 6667,
nick: 'prawnsbot'
host: 'irc.freenode.net',
port: 6697,
tls: true,
nick: 'prawnsbot'
});

bot.on('message', function(event) {
if (event.message.indexOf('hello') === 0) {
event.reply('Hi!');
}
if (event.message.match(/^!join /)) {
var to_join = event.message.split(' ');
event.reply('Joining ' + to_join + '..');
bot.join(to_join);
}
if (event.message.indexOf('hello') === 0) {
event.reply('Hi!');
}

if (event.message.match(/^!join #\S+/)) {
var to_join = event.message.split(' ')[1];
event.reply('Joining ' + to_join + '..');
bot.join(to_join);
}
});


// Or a quicker to match messages...
bot.matchMessage(/^hi/, function(event) {
event.reply('hello there!');
event.reply('hello there!');
});
~~~

#### Channel/buffer objects. Great for building clients
~~~javascript
var bot = new IRC.Client();
bot.connect({
host: 'irc.freenode.net',
port: 6667,
nick: 'prawnsbot'
host: 'irc.freenode.net',
port: 6697,
tls: true,
nick: 'prawnsbot'
});

var buffers = [];
bot.on('registered', function() {
var channel = bot.channel('#prawnsalad');
buffers.push(channel);
channel.join();
channel.say('Hi!');
channel.updateUsers(function() {
console.log(channel.users);
});

// Or you could even stream the channel messages elsewhere
var stream = channel.stream();
stream.pipe(process.stdout);
var channel = bot.channel('#prawnsalad');
buffers.push(channel);

channel.join();
channel.say('Hi!');

channel.updateUsers(function() {
console.log(channel.users);
});

// Or you could even stream the channel messages elsewhere
var stream = channel.stream();
stream.pipe(process.stdout);
});
~~~


#### Middleware
~~~javascript
function ExampleMiddleware() {
return function(client, raw_events, parsed_events) {
parsed_events.use(theMiddleware);
}


function theMiddleware(command, event, client, next) {
if (command === 'registered') {
if (client.options.nickserv) {
var options = client.options.nickserv;
client.say('nickserv', 'identify ' + options.account + ' ' + options.password);
}
}

if (command === 'message' && client.caseCompare(event.event.nick, 'nickserv')) {
// Handle success/retries/failures
}

next();
}
return function(client, rawEvents, parsedEvents) {
rawEvents.use(rawMiddleware)
parsedEvents.use(parsedMiddleware);
}

function rawMiddleware(command, event, rawLine, client, next) {
if (command === '254') {
console.log('This network has', event.params[1], 'channels');
}

next();
}

function parsedMiddleware(command, event, client, next) {
if (command === 'registered') {
if (client.options.nickserv) {
var options = client.options.nickserv;
client.say('nickserv', 'identify ' + options.account + ' ' + options.password);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps using nickserv like this is not a good example (as servers have /ns alias to avoid nickserv being taken over)

}
}

if (command === 'message' && client.caseCompare(event.event.nick, 'nickserv')) {
// Handle success/retries/failures
}

next();
}
}


var irc_bot = new IRC.Client();
irc_bot.use(ExampleMiddleware());
var bot = new IRC.Client();
bot.use(ExampleMiddleware());
~~~