Skip to content

Commit

Permalink
Merge pull request publiclab#50 from ryzokuken/minor-2
Browse files Browse the repository at this point in the history
Shift behaviors to bot.js
  • Loading branch information
ryzokuken authored Jun 28, 2017
2 parents 702e738 + d949b7f commit 6b8e0d2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plotsbot",
"version": "1.0.2",
"version": "1.0.3",
"description": "A bot for Public Lab",
"main": "src/bot.js",
"repository": "[email protected]:publiclab/plotsbot.git",
Expand Down
15 changes: 10 additions & 5 deletions spec/help-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,37 @@ const helpMessage = require('../src/behaviors/help').helpMessage;

describe('Help Behavior', () => {
const botNick = 'testbot';
const behaviors = new Behaviors(botNick, undefined);
const helpBehavior = require('../src/behaviors/help').helpBehavior;
const behaviors = new Behaviors(botNick, undefined, [], [helpBehavior]);

const chatbotHelp = helpMessage(botNick, 'chatbot');
const invalidHelp = helpMessage(botNick, 'invalid');

it('should print general help', () => {
it('should print general help', (done) => {
behaviors.getResponse(botNick, 'help').then(response => {
expect(response).toBe(chatbotHelp);
done();
});
});

it('should print specific help for existing modules', () => {
it('should print specific help for existing modules', (done) => {
behaviors.getResponse(botNick, 'help chatbot').then(response => {
expect(response).toBe(chatbotHelp);
done();
});
});

it('should print specific help for nonexisting modules', () => {
it('should print specific help for nonexisting modules', (done) => {
behaviors.getResponse(botNick, 'help invalid').then(response => {
expect(response).toBe(invalidHelp);
done();
});
});

it('should print specific help for existing and nonexisting modules combined', () => {
it('should print specific help for existing and nonexisting modules combined', (done) => {
behaviors.getResponse(botNick, 'help chatbot invalid').then(response => {
expect(response).toBe(chatbotHelp + '\n\n' + invalidHelp);
done();
});
});
});
9 changes: 3 additions & 6 deletions src/behaviors/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const utils = require('../utils');

const greetBehavior = require('./greet');
const helpBehavior = require('./help').helpBehavior;

function parseMessage (message) {
return message.split(/[\s,.;:!?]/g).filter(String);
}
Expand Down Expand Up @@ -30,11 +27,11 @@ function messageResponse(botNick, parsed, behaviors) {
}

class Behaviors {
constructor(botNick, client) {
constructor(botNick, client, joinBehaviors, messageBehaviors) {
this.botNick = botNick;
this.client = client;
this.joinBehaviors = [ greetBehavior ];
this.messageBehaviors = [ helpBehavior ];
this.joinBehaviors = joinBehaviors;
this.messageBehaviors = messageBehaviors;
}

addJoinHandler() {
Expand Down
13 changes: 12 additions & 1 deletion src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const fs = require('fs');

const Behaviors = require('./behaviors');

const greetBehavior = require('./behaviors/greet');
const helpBehavior = require('./behaviors/help').helpBehavior;

// Read file synchronously because we'd need this object in later steps anyway.
const config = JSON.parse(fs.readFileSync('config.json', 'utf8'));

Expand All @@ -17,5 +20,13 @@ if (process.env.TEST) {
client = new IrcClient(config.server, config.name, config.channels);
}

const behaviors = new Behaviors(config.name, client);
const joinBehaviors = [
greetBehavior
];

const messageBehaviors = [
helpBehavior
];

const behaviors = new Behaviors(config.name, client, joinBehaviors, messageBehaviors);
behaviors.bootstrap();

0 comments on commit 6b8e0d2

Please sign in to comment.