Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
91 changes: 91 additions & 0 deletions bot/controllers/poll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const BaseController = require('../baseController.js');
const Command = require('../baseCommand.js');
const util = require('apex-util');

class PollController extends BaseController {
constructor(message) {
// Call BaseController constructor
super(message);

// Aliasing 'this' as controller to allow for binding in actions
const controller = this;

// Array of all commands, see baseCommand.js for prototype
this.commands = [
new Command(
'!poll',
'!poll <ask-a-question> <option_1> <option_2> <option_3>',
'Posts a Question users can vote on',
'Posts a Question users can vote on, use "-" to separate words in your question',
this.pollAction.bind(controller),
),
];
}

pollAction() {
const { message } = this;
const question = message.parsed[1];
const questionSpaced = question.split('-').join(' ');
const content = message.content;

Choose a reason for hiding this comment

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

I'm picking up a linting issue here, even though it seems to have gone through the tests successfully. However, I do recommend de-structuring it:

const { content } = messsage;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure why you're getting that linting issue, I don't see it and like you said it does pass the CircleCI test. I'm using content to convert the message.content into a usable string, but still need to use const {message} = message} so I get the initial information from Discord.

const count = content.split(' ').length;

let options = '';

if (count >= 2 && count <= 11) {
for (let i = 2; i < count; i += 1) {
if (i === 2) {
options += `\r \u0031\u20E3${message.parsed[i]}`;
} else if (i === 3) {
options += `\r \u0032\u20E3${message.parsed[i]}`;
} else if (i === 4) {
options += `\r \u0033\u20E3${message.parsed[i]}`;
} else if (i === 5) {
options += `\r \u0034\u20E3${message.parsed[i]}`;
} else if (i === 6) {
options += `\r \u0035\u20E3${message.parsed[i]}`;
} else if (i === 7) {
options += `\r \u0036\u20E3${message.parsed[i]}`;
} else if (i === 8) {
options += `\r \u0037\u20E3${message.parsed[i]}`;
} else if (i === 9) {
options += `\r \u0038\u20E3${message.parsed[i]}`;
} else if (i === 10) {
options += `\r ️\u0039\u20E3${message.parsed[i]}`;
}
}
} else {

Choose a reason for hiding this comment

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

Add a space between the special characters and the poll options for minor readability improvement

return 'The poll can not expect 0 or more than 9 options';
}

const query = `"${questionSpaced}" \r ${options}`;
message.channel.send(query)
.then((message) => {
for (let i = 2; i < count; i += 1) {
if (i === 2) {
message.react('\u0031\u20E3');
} else if (i === 3) {
message.react('\u0032\u20E3');
} else if (i === 4) {
message.react('\u0033\u20E3');
} else if (i === 5) {
message.react('\u0034\u20E3');
} else if (i === 6) {
message.react('\u0035\u20E3');
} else if (i === 7) {
message.react('\u0036\u20E3');
} else if (i === 8) {
message.react('\u0037\u20E3');
} else if (i === 9) {
message.react('\u0038\u20E3');
} else if (i === 10) {
message.react('\u0039\u20E3');
}
}

Choose a reason for hiding this comment

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

If I'm reading this correctly, I believe that this can be improved without doing multiple checks? For example:

message.react(`\u003${i}\u20E3`);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

While this is a better way to solve this, the Style Guide restricts incomplete unicode like \u003${i}.

}).catch(() => {
util.error('pollAction reaction failed');
});
return ' wants to know.';
}
}

module.exports = PollController;
41 changes: 41 additions & 0 deletions bot/controllers/question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const BaseController = require('../baseController.js');
const Command = require('../baseCommand.js');
const util = require('apex-util');

class QuestionController extends BaseController {
constructor(message) {
// Call BaseController constructor
super(message);

// Aliasing 'this' as controller to allow for binding in actions
const controller = this;

// Array of all commands, see baseCommand.js for prototype
this.commands = [
new Command(
'!question',
'!question <ask-a-question>',
'Posts a Question users can vote on',
'Posts a Question users can vote on, use "-" to separate words in your question',
this.questionsAction.bind(controller),
),
];
}

questionsAction() {
const { message } = this;
const question = message.parsed[1];
const query = `"${question.split('-').join(' ')}?"`;
message.channel.send(query)
.then((message) => {
message.react('👍');
message.react('🤔');
message.react('👎');
}).catch(() => {
util.error('questionAction reaction failed');
});
return ' wants to know.';
}
}

module.exports = QuestionController;