Skip to content

Commit bf575c6

Browse files
authored
Merge pull request #199 from zx6658/master
add reaction cancel situation and update and delete other code
2 parents 4f25eaf + 9cfc23f commit bf575c6

File tree

7 files changed

+58
-16
lines changed

7 files changed

+58
-16
lines changed

Diff for: lib/const.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ module.exports = {
1212
TELL_ME_ABOUT: {
1313
CALL: 'tell me about',
1414
JASON_MOM: 'jason\'s mom',
15-
},
16-
SAVE_GIT_NAME: 'link my github name='
15+
}
1716
},
1817
HELP: {
1918
CALL: 'help'
@@ -22,6 +21,7 @@ module.exports = {
2221
OUTPUT: {
2322
reaction: (result, point, user, reaction) => 'Congratulations ' + result.userName + ' get ' + point + ' points by :' + reaction + ': from ' + user + ' !',
2423
pointTo: (result, point, user) => 'Congratulations ' + result.userName + ' get ' + point + ' points from ' + user + ' !',
24+
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:',
2525
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!',
2626
getBestStudent: record => 'High score (boy/girl): ' + record.userName + ' with ' + record.point + ' points!',
2727
getWorstStudent: record => 'Low score (boy/girl): ' + record.userName + ' with ' + record.point + ' points!',
@@ -42,6 +42,7 @@ module.exports = {
4242
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',
4343
},
4444
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.',
45+
cancelWhenzero: (result) => 'Oh... reaction was canceled... but Mr.dumbledore didn\'t take point from ' + result.userName + ' because ' + result.userName + ' don\'t have point.. :joy:',
4546
FAIL_POINT_TO: 'Well... you can\'t award points to me, the professor.'
4647
},
4748
DB: {

Diff for: lib/controller/awardPoints.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async function awardPoints(message) {
1616
}
1717

1818
const userName = await this.convertToUserName(userId);
19-
// const user = message.item_user;
19+
2020
if (typeof botName === 'undefined' || typeof userId === 'undefined' || typeof point === 'undefined') return;
2121
if (userName === botName) return this.slackBot.awardPointsCallback(message, OUTPUT.FAIL_POINT_TO);
2222

Diff for: lib/controller/deductPoints.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const Parse = require('parse/node');
2+
const { DB, OUTPUT } = require('../const');
3+
4+
async function deductPoints(message) {
5+
const fromUser = await this.convertToUserName(message.user);
6+
const botName = this.name;
7+
let point = 0;
8+
let userId = '';
9+
10+
if (this.isReactionEvent(message)) {
11+
point = 10;
12+
userId = message.item_user;
13+
}
14+
15+
const userName = await this.convertToUserName(userId);
16+
17+
if (typeof botName === 'undefined' || typeof userId === 'undefined' || typeof point === 'undefined') return;
18+
if (userName === botName) return this.slackBot.reactionCancelCallback(message, OUTPUT.FAIL_POINT_TO);
19+
20+
const Student = new Parse.Object(DB.STUDENT.CALL);
21+
const query = new Parse.Query(Student);
22+
query.equalTo(DB.STUDENT.BOT_ID, this.id);
23+
query.equalTo(DB.STUDENT.USER_ID, userId);
24+
25+
try {
26+
const results = await query.first();
27+
28+
if (this.isReactionEvent(message)) {
29+
if (results.attributes.point <= 0) {
30+
return this.slackBot.reactionCancelCallback(message, OUTPUT.cancelWhenzero(results.attributes));
31+
}
32+
results.increment(DB.STUDENT.POINT, -point);
33+
results.save();
34+
this.slackBot.reactionCancelCallback(message, OUTPUT.reactionCancel(results.attributes, point, fromUser.name, message.reaction));
35+
}
36+
} catch (err) {
37+
console.log(err);
38+
}
39+
}
40+
41+
module.exports = deductPoints;

Diff for: lib/controller/replyWithDumbledore.js

+1-12
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,14 @@ async function listStudent(originalMessage) {
8888
this.slackBot.announcePlainString(originalMessage, OUTPUT.getListStudent(record));
8989
}
9090

91-
function saveGitName(originalMessage) {
92-
const Student = new Parse.Object(DB.STUDENT.CALL);
93-
const query = new Parse.Query(Student);
94-
const gitName = originalMessage.text.split('=')[1];
95-
const { user } = originalMessage;
96-
query.equalTo('user_id', user);
97-
Student.set('GITHUB_NAME', gitName);
98-
this.slackBot.announcePlainString(originalMessage, OUTPUT.saveGitName(this.convertToUserName(user), gitName));
99-
}
100-
10191
function replyWithDumbledore(originalMessage) {
10292
const text = originalMessage.text.toLowerCase();
10393

10494
const parseCase = {
10595
[INPUT.PROFESSOR.GET_BEST_STUDENT]: () => bestStudent.call(this, originalMessage),
10696
[INPUT.PROFESSOR.GET_WORST_STUDENT]: () => worstStudent.call(this, originalMessage),
10797
[INPUT.PROFESSOR.GET_LIST_STUDENT]: () => listStudent.call(this, originalMessage),
108-
[INPUT.PROFESSOR.TELL_ME_ABOUT.CALL]: () => tellMeAbout.call(this, originalMessage),
109-
[INPUT.PROFESSOR.SAVE_GIT_NAME]: () => saveGitName.call(this, originalMessage)
98+
[INPUT.PROFESSOR.TELL_ME_ABOUT.CALL]: () => tellMeAbout.call(this, originalMessage)
11099
};
111100

112101
Object.keys(parseCase).forEach(key => {

Diff for: lib/dumbledore.js

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Dumbledore {
1515
this.githubChannel = settings.githubChannel || 'null';
1616
this.name = this.slackBot.getName();
1717
this.awardPoints = require('./controller/awardPoints').bind(this);
18+
this.deductPoints = require('./controller/deductPoints').bind(this);
1819
this.replyWithDumbledore = require('./controller/replyWithDumbledore').bind(this);
1920
this.getUser = require('./controller/getUser').bind(this);
2021
this.replyWithGithub = require('./controller/replyWithGithub').bind(this);
@@ -102,6 +103,8 @@ class Dumbledore {
102103
} else if (this.isReactionEvent(message)) {
103104
if (message.type === 'reaction_added') {
104105
this.awardPoints(message);
106+
} else if (message.type === 'reaction_removed') {
107+
this.deductPoints(message);
105108
}
106109
this.saveMessage(message);
107110
}
@@ -145,6 +148,8 @@ class Dumbledore {
145148

146149
if (text.includes(INPUT.POINTS_TO.CALL)) {
147150
this.awardPoints(message);
151+
} else if (text.includes(INPUT.POINTS_FROM.CALL) || text.includes(this.name)) {
152+
this.deductPoints(message);
148153
} else if (text.includes(INPUT.PROFESSOR.CALL) || text.includes(this.name)) {
149154
this.replyWithDumbledore(message);
150155
} else if (text.includes(INPUT.HELP.CALL)) {

Diff for: lib/helper/slackBot.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ class SlackBot extends Bot {
66
throw Error('please set token and name of bot');
77
}
88
super({ token, name });
9-
9+
this.reactionPointsCallback = this.reactionPointsCallback.bind(this);
10+
this.reactionCancelCallback = this.reactionCancelCallback.bind(this);
1011
this.awardPointsCallback = this.awardPointsCallback.bind(this);
1112
this.announcePlainString = this.announcePlainString.bind(this);
1213
}
@@ -20,6 +21,10 @@ class SlackBot extends Bot {
2021
const channel = await this.getChannelById(originalMessage.item.channel);
2122
await this.postMessageToChannel(channel.name, message, { as_user: true });
2223
}
24+
async reactionCancelCallback(originalMessage, message) {
25+
const channel = await this.getChannelById(originalMessage.item.channel);
26+
await this.postMessageToChannel(channel.name, message, { as_user: true });
27+
}
2328

2429
async announcePlainString(originalMessage, message) {
2530
const channel = await this.getChannelById(originalMessage.channel);

Diff for: spec/helperSpec.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describe('In helper', function () {
3333

3434
it('Any functions should not be `undefined` in slackBot', () => {
3535
expect(slackBot.reactionPointsCallback).toBeDefined();
36+
expect(slackBot.reactionCancelCallback).toBeDefined();
3637
expect(slackBot.awardPointsCallback).toBeDefined();
3738
expect(slackBot.announcePlainString).toBeDefined();
3839
expect(slackBot.getUserList).toBeDefined();

0 commit comments

Comments
 (0)