This repository has been archived by the owner on Oct 20, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy paththanks.js
executable file
·131 lines (113 loc) · 4.13 KB
/
thanks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
const Utils = require('../../../lib/utils/Utils');
const HttpWrap = require('../../../lib/utils/HttpWrap');
const TextLib = require('../../../lib/utils/TextLib');
const thanksCommands = {
// messages: {
// wikiHint: function(fromUser) {
// const wikiUrl = '(https://github.com/freecodecamp/' +
// 'freecodecamp/wiki/wiki-style-guide)';
//
// return '\n> hey @' + fromUser + ' if you found this info helpful ' +
// ':point_right: *[consider adding a wiki article!]' + wikiUrl + '*';
// }
// },
thanks: function(input, bot) {
Utils.hasProperty(input, 'message', 'thanks expects an object');
const mentions = input.message.model.mentions;
// just 'thanks' in a message
if (mentions && mentions.length === 0) {
Utils.warn('thanks', 'without any mentions', input.message.model);
return null;
}
const fromUser = input.message.model.fromUser.username.toLowerCase();
const options = {
method: 'POST',
input: input,
bot: bot
};
const namesList = mentions.reduce((userList, mention) => {
const toUser = mention.screenName.toLowerCase();
if (toUser !== fromUser && userList.indexOf(toUser) === -1) {
const apiPath = '/api/users/give-brownie-points?receiver=' + toUser +
'&giver=' + fromUser;
HttpWrap.callApi(apiPath, options, thanksCommands.showInfoCallback);
userList.push(toUser);
}
return userList;
}, []);
if (namesList.length > 0) {
const toUserMessage = namesList.join(' and @');
return '> ' + fromUser + ' sends brownie points to @' + toUserMessage +
' :sparkles: :thumbsup: :sparkles: ';
} else {
return '> sorry ' + fromUser + ', you can\'t send brownie points to ' +
'yourself! :sparkles: :sparkles: ';
}
},
about: function(input, bot) {
const mentions = input.message.model.mentions,
them = mentions[0];
if (!them) {
Utils.warn('about without any mentions', input.message.model);
return null;
}
const name = them.screenName.toLowerCase();
const options = {
method: 'GET',
input: input,
bot: bot
};
const apiPath = '/api/users/about?username=' + name;
HttpWrap.callApi(apiPath, options, thanksCommands.showInfoCallback);
return null;
},
// called back from apiCall so can't use Global GBot here
// blob:
// response
// bot
// input
showInfoCallback: function(blob) {
// in case we want to filter the message
const cleanMessage = message => {
if (message.match(/^FCC: no user/)) {
message = 'hmm, can\'t find that user on the beta site. wait til ' +
'we release new version!';
}
const msgPattern = /^could not find receiver for /;
if (message.match(msgPattern)) {
message = message.replace(
msgPattern,
'@') + '\'s account is not linked with freeCodeCamp' +
'. Please visit [the settings]' +
'(https://freecodecamp.org/settings) and link your ' +
'GitHub account.';
}
message = '> :warning: ' + message;
return message;
};
if (blob.response.error) {
const message = cleanMessage(blob.response.error.message);
Utils.warn('WARN @thanks>', blob.response.error.message,
blob.response.error);
// show the error to the user
blob.bot.say(message, blob.input.message.room);
return false;
}
let str;
try {
const username = blob.response.about.username;
const about = blob.response.about;
const brownieEmoji = about.browniePoints < 1000 ? ':cookie:' : ':star2:';
const uri = 'http://www.freecodecamp.org/' + username;
str = `> ${brownieEmoji} ${about.browniePoints} | @${username} |`;
str += TextLib.mdLink(uri, uri);
} catch (err) {
Utils.error('can\'t create response from API callback', err);
Utils.warn('thanks>', 'blob>', blob);
str = 'api offline';
}
return blob.bot.say(str, blob.input.message.room);
}
};
module.exports = thanksCommands;