diff --git a/lib/const.js b/lib/const.js index 23a5d95..0aaa551 100644 --- a/lib/const.js +++ b/lib/const.js @@ -12,8 +12,7 @@ module.exports = { TELL_ME_ABOUT: { CALL: 'tell me about', JASON_MOM: 'jason\'s mom', - }, - SAVE_GIT_NAME: 'link my github name=' + } }, HELP: { CALL: 'help' @@ -22,6 +21,7 @@ module.exports = { OUTPUT: { reaction: (result, point, user, reaction) => 'Congratulations ' + result.userName + ' get ' + point + ' points by :' + reaction + ': from ' + user + ' !', pointTo: (result, point, user) => 'Congratulations ' + result.userName + ' get ' + point + ' points from ' + user + ' !', + reactionCancel: (result, point, user, reaction) => 'Oh... ' + user + ' canceled :' + reaction + ': so Mr.dumbledore take ' + point + ' from ' + result.userName + '.. You have to give the correct answer. :joy:', SAY_HELLO: 'Welcome to Hogwarts everyone!\n Now, in a few moments you will pass through these doors and join your classmates. Your triumphs will earn you points. Any rule breaking, and you will lose points. I Albus Dumbledore will award points on your behalf. Just say `5 points to @benc` to award points. If you want to know the best student, just say `professor best student`. Then good luck everyone!', getBestStudent: record => 'High score (boy/girl): ' + record.userName + ' with ' + record.point + ' points!', getWorstStudent: record => 'Low score (boy/girl): ' + record.userName + ' with ' + record.point + ' points!', @@ -42,6 +42,7 @@ module.exports = { JASON_MOM: 'Not much is known about Jason\'s mom, except that she is thought to be responsible for the great internet unplugging of 2016', }, WHENZERO: 'Hmm... You don\'t have your own points. before giving points to someone, you should answer to other\'s question and get extra points.', + cancelWhenzero: (result) => 'Oh... reaction was canceled... but Mr.dumbledore didn\'t take point from ' + result.userName + ' because ' + result.userName + ' don\'t have point.. :joy:', FAIL_POINT_TO: 'Well... you can\'t award points to me, the professor.' }, DB: { diff --git a/lib/controller/awardPoints.js b/lib/controller/awardPoints.js index ea49c89..a40c40a 100644 --- a/lib/controller/awardPoints.js +++ b/lib/controller/awardPoints.js @@ -16,7 +16,7 @@ async function awardPoints(message) { } const userName = await this.convertToUserName(userId); - // const user = message.item_user; + if (typeof botName === 'undefined' || typeof userId === 'undefined' || typeof point === 'undefined') return; if (userName === botName) return this.slackBot.awardPointsCallback(message, OUTPUT.FAIL_POINT_TO); diff --git a/lib/controller/deductPoints.js b/lib/controller/deductPoints.js new file mode 100644 index 0000000..387578c --- /dev/null +++ b/lib/controller/deductPoints.js @@ -0,0 +1,41 @@ +const Parse = require('parse/node'); +const { DB, OUTPUT } = require('../const'); + +async function deductPoints(message) { + const fromUser = await this.convertToUserName(message.user); + const botName = this.name; + let point = 0; + let userId = ''; + + if (this.isReactionEvent(message)) { + point = 10; + userId = message.item_user; + } + + const userName = await this.convertToUserName(userId); + + if (typeof botName === 'undefined' || typeof userId === 'undefined' || typeof point === 'undefined') return; + if (userName === botName) return this.slackBot.reactionCancelCallback(message, OUTPUT.FAIL_POINT_TO); + + const Student = new Parse.Object(DB.STUDENT.CALL); + const query = new Parse.Query(Student); + query.equalTo(DB.STUDENT.BOT_ID, this.id); + query.equalTo(DB.STUDENT.USER_ID, userId); + + try { + const results = await query.first(); + + if (this.isReactionEvent(message)) { + if (results.attributes.point <= 0) { + return this.slackBot.reactionCancelCallback(message, OUTPUT.cancelWhenzero(results.attributes)); + } + results.increment(DB.STUDENT.POINT, -point); + results.save(); + this.slackBot.reactionCancelCallback(message, OUTPUT.reactionCancel(results.attributes, point, fromUser.name, message.reaction)); + } + } catch (err) { + console.log(err); + } +} + +module.exports = deductPoints; diff --git a/lib/controller/replyWithDumbledore.js b/lib/controller/replyWithDumbledore.js index fc72cb7..85d7dca 100644 --- a/lib/controller/replyWithDumbledore.js +++ b/lib/controller/replyWithDumbledore.js @@ -88,16 +88,6 @@ async function listStudent(originalMessage) { this.slackBot.announcePlainString(originalMessage, OUTPUT.getListStudent(record)); } -function saveGitName(originalMessage) { - const Student = new Parse.Object(DB.STUDENT.CALL); - const query = new Parse.Query(Student); - const gitName = originalMessage.text.split('=')[1]; - const { user } = originalMessage; - query.equalTo('user_id', user); - Student.set('GITHUB_NAME', gitName); - this.slackBot.announcePlainString(originalMessage, OUTPUT.saveGitName(this.convertToUserName(user), gitName)); -} - function replyWithDumbledore(originalMessage) { const text = originalMessage.text.toLowerCase(); @@ -105,8 +95,7 @@ function replyWithDumbledore(originalMessage) { [INPUT.PROFESSOR.GET_BEST_STUDENT]: () => bestStudent.call(this, originalMessage), [INPUT.PROFESSOR.GET_WORST_STUDENT]: () => worstStudent.call(this, originalMessage), [INPUT.PROFESSOR.GET_LIST_STUDENT]: () => listStudent.call(this, originalMessage), - [INPUT.PROFESSOR.TELL_ME_ABOUT.CALL]: () => tellMeAbout.call(this, originalMessage), - [INPUT.PROFESSOR.SAVE_GIT_NAME]: () => saveGitName.call(this, originalMessage) + [INPUT.PROFESSOR.TELL_ME_ABOUT.CALL]: () => tellMeAbout.call(this, originalMessage) }; Object.keys(parseCase).forEach(key => { diff --git a/lib/dumbledore.js b/lib/dumbledore.js index 8875390..62d8da9 100644 --- a/lib/dumbledore.js +++ b/lib/dumbledore.js @@ -15,6 +15,7 @@ class Dumbledore { this.githubChannel = settings.githubChannel || 'null'; this.name = this.slackBot.getName(); this.awardPoints = require('./controller/awardPoints').bind(this); + this.deductPoints = require('./controller/deductPoints').bind(this); this.replyWithDumbledore = require('./controller/replyWithDumbledore').bind(this); this.getUser = require('./controller/getUser').bind(this); this.replyWithGithub = require('./controller/replyWithGithub').bind(this); @@ -102,6 +103,8 @@ class Dumbledore { } else if (this.isReactionEvent(message)) { if (message.type === 'reaction_added') { this.awardPoints(message); + } else if (message.type === 'reaction_removed') { + this.deductPoints(message); } this.saveMessage(message); } @@ -145,6 +148,8 @@ class Dumbledore { if (text.includes(INPUT.POINTS_TO.CALL)) { this.awardPoints(message); + } else if (text.includes(INPUT.POINTS_FROM.CALL) || text.includes(this.name)) { + this.deductPoints(message); } else if (text.includes(INPUT.PROFESSOR.CALL) || text.includes(this.name)) { this.replyWithDumbledore(message); } else if (text.includes(INPUT.HELP.CALL)) { diff --git a/lib/helper/slackBot.js b/lib/helper/slackBot.js index c6cf759..484dbfa 100644 --- a/lib/helper/slackBot.js +++ b/lib/helper/slackBot.js @@ -6,7 +6,8 @@ class SlackBot extends Bot { throw Error('please set token and name of bot'); } super({ token, name }); - + this.reactionPointsCallback = this.reactionPointsCallback.bind(this); + this.reactionCancelCallback = this.reactionCancelCallback.bind(this); this.awardPointsCallback = this.awardPointsCallback.bind(this); this.announcePlainString = this.announcePlainString.bind(this); } @@ -20,6 +21,10 @@ class SlackBot extends Bot { const channel = await this.getChannelById(originalMessage.item.channel); await this.postMessageToChannel(channel.name, message, { as_user: true }); } + async reactionCancelCallback(originalMessage, message) { + const channel = await this.getChannelById(originalMessage.item.channel); + await this.postMessageToChannel(channel.name, message, { as_user: true }); + } async announcePlainString(originalMessage, message) { const channel = await this.getChannelById(originalMessage.channel); diff --git a/spec/helperSpec.js b/spec/helperSpec.js index 59a8567..bcaa5ce 100644 --- a/spec/helperSpec.js +++ b/spec/helperSpec.js @@ -33,6 +33,7 @@ describe('In helper', function () { it('Any functions should not be `undefined` in slackBot', () => { expect(slackBot.reactionPointsCallback).toBeDefined(); + expect(slackBot.reactionCancelCallback).toBeDefined(); expect(slackBot.awardPointsCallback).toBeDefined(); expect(slackBot.announcePlainString).toBeDefined(); expect(slackBot.getUserList).toBeDefined();