forked from commons-stack/CommonsStackBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointsbot.js
364 lines (333 loc) · 9.79 KB
/
pointsbot.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
const dayjs = require('dayjs')
const {
dishers,
point_types,
reason_seperators,
max_points,
sheet_id,
sheet_tab_name,
dish_notification_msg,
milestone_automation_trigger_users,
milestone_notification_msg,
} = require('./constants')
const BigNumber = require('bignumber.js')
const { google } = require('googleapis')
exports.handlePointGiving = function(
auth,
input,
privateRooms,
client,
notificationFunction
) {
let message = input.text
const roomId = input.chat.id
const user = input.from.username
if (message.trim()[0] == '>') {
// quoting another user, skip the quoted part
message = message.split('\n\n')[1]
}
// Support for "! dish" command
if (message[1] === ' ') {
message = message.replace(' ', '')
}
const msg = message.split(' ')
const command = msg[0].toLowerCase()
if (command == '!help') {
client.sendMessage(
roomId,
'dish using the following format:\n!dish [type of praise] to [handle, handle, handle] [' +
reason_seperators.toString().replace(/,/g, '/') +
'] [reason]'
)
} else if (command == '!dish') {
if (input.chat.type === 'private') {
client.sendMessage(roomId, `Dishing isn't allowed in private rooms.`)
return
}
if (!dishers.includes(user)) {
client.sendMessage(roomId, `You aren't permitted to dish praise.`)
return
}
handleDish(input, privateRooms, notificationFunction, client, auth)
} else if (command == '!sheet') {
client.sendMessage(
roomId,
`the Praise sheet can be found here: https://docs.google.com/spreadsheets/d/${sheet_id}`
)
} else if (command == '!sendmilestones') {
if (milestone_automation_trigger_users.includes(user)) {
handleMilestoneAutomation(notificationFunction, client, privateRooms)
client.sendMessage(
roomId,
`Sent notifications of milestone creation to all eligible users!`
)
} else {
client.sendMessage(roomId, `Sorry, you're not allowed to do that.`)
}
} else if (command === '/start' && privateRooms[user].room === roomId) {
client.sendMessage(roomId, dish_notification_msg, {
parse_mode: 'Markdown',
})
}
}
function handleMilestoneAutomation(notificationFunc, client, privateRooms) {
for (var user in privateRooms) {
var values = privateRooms[user]
var now = new Date()
if (
values.lastMonthNotified != now.getMonth() &&
values.lastDishMonth == now.getMonth()
) {
var startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1)
var month = now.toLocaleString('en-us', { month: 'long' })
var title =
'RewardDAO ' +
now.toLocaleString('en-us', { month: 'long', year: 'numeric' }) +
': ' +
user
var url = `https://beta.giveth.io/campaigns/5b3d9746329bc64ae74d1424/milestones/propose?title=${encodeURI(
title
)}&date=${encodeURI(startOfMonth)}&isCapped=0&requireReviewer=0`
var deadline = new Date(
now.getFullYear(),
now.getMonth() + 1,
7
).toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
})
notificationFunc(
milestone_notification_msg
.replace('%LINK%', url)
.replace('%MONTH%', month)
.replace('%DEADLINE%', deadline),
user,
client,
null
)
privateRooms[user].lastMonthNotified = now.getMonth()
//privateRooms[milestone[0]].lastMonthNotified = now.getMonth()
}
}
}
function handleDish(msg, privateRooms, notificationFunc, client, auth) {
let message = msg.text
let matched = false
let regex = new RegExp(
'!\\s*dish\\s+(\\S+)\\s+to\\s+(.+?)\\s+(' +
reason_seperators.toString().replace(/,/g, '|') +
')\\s+([^\\n]+)',
'gi'
)
if (msg.from.is_bot) {
// we sent the message.
return
}
let match
do {
match = regex.exec(message)
if (match) {
tryDish(
msg,
client,
auth,
privateRooms,
notificationFunc,
1,
match[1],
match[2],
match[4]
)
matched = true
}
} while (match)
if (!matched) {
client.sendMessage(
msg.chat.id,
'ERROR, please use the following format:\n!dish [type of praise] to [handle, handle, handle] for [reason]'
)
}
}
function tryDish(
msg,
client,
auth,
privateRooms,
notificationFunc,
nPoints,
type,
users,
reason
) {
try {
const sender = msg.from.username
const amount = new BigNumber(nPoints)
type = type.toUpperCase()
if (amount.isNaN()) {
const pointError = new Error(
'Invalid number of praise dished. Please enter a valid number and try again'
)
pointError.code = 'POINTS_NOT_NUMBER'
throw pointError
}
if (amount.isLessThan(1)) {
const pointError = new Error(
"You can't dish negative or zero amount of praise!"
)
pointError.code = 'POINTS_ARE_NEGATIVE_OR_ZERO'
throw pointError
}
if (amount.isGreaterThan(max_points)) {
const pointError = new Error(
`You can't dish more than ${max_points} praise each time!`
)
pointError.code = 'POINTS_OVER_MAXIMUM'
throw pointError
}
if (!point_types.includes(type)) {
const typeError = new Error(
`Invalid type '${type}'. Please use one of ${point_types}.`
)
typeError.code = 'POINT_TYPE_DOES_NOT_EXIST'
throw typeError
}
users = users.split(',')
const sheets = google.sheets({ version: 'v4', auth })
var values = []
users.forEach(user => {
user = user.trim()
let { userInRoom, receiver, display_name, multipleUsers } = findReceiver(
privateRooms,
user
) // try to find user
// handle github users
const BASE_GITHUB_URL = 'https://github.com/'
if (user.split(BASE_GITHUB_URL)[1]) {
receiver = user
;(userInRoom = true), (multipleUsers = false)
}
if (multipleUsers) {
const userError = new Error(`There are multiple users with the name '${receiver}' in this room.
please specify the domain name of the user using the format @[userId]:[domain]`)
userError.code = 'USER_MULTIPLE'
throw userError
}
if (!userInRoom) {
const userError = new Error(`Username '${receiver}' does not exist in this room.
either add this user to the room, or try again using the format @[userId]:[domain]`)
userError.code = 'USER_DOES_NOT_EXIST'
throw userError
}
const date = dayjs().format('DD-MMM-YYYY')
const link = `https://t.me/${msg.chat.username}/${msg.message_id}`
values.push([
receiver,
sender,
reason,
//amount.toFormat(2),
type,
date,
//link,
display_name,
])
})
const body = { values }
sheets.spreadsheets.values.append(
{
spreadsheetId: sheet_id,
range: sheet_tab_name,
valueInputOption: 'USER_ENTERED',
insertDataOption: 'INSERT_ROWS',
resource: body,
},
err => {
if (err) {
return console.log('The API returned an error: ' + err)
}
values.forEach(value => {
let text = `${value[1]} dished ${value[3]} to @${value[0]}`
if (!privateRooms[value[0]].lastDishMonth) {
text +=
"\nIn order to claim the praise, please send me a [direct message](https://t.me/commonsstackbot?start), hit start and I'll send you all the info you need"
} else {
text +=
"\nIn order to claim the praise, please send me a [direct message](https://t.me/commonsstackbot?start) and I'll send you all the info you need"
}
client.sendMessage(msg.chat.id, text, { parse_mode: 'Markdown' })
notificationFunc(
dish_notification_msg
.replace('%DISHER%', value[1])
.replace('%ROOM%', value[6]),
value[0],
client,
null
)
privateRooms[value[0]].lastDishMonth = new Date().getMonth()
})
}
)
} catch (err) {
console.error(err)
const MANUAL_ERROR_CODES = [
'POINTS_NOT_NUMBER',
'USER_DOES_NOT_EXIST',
'POINT_TYPE_DOES_NOT_EXIST',
'USER_MULTIPLE',
'POINTS_ARE_NEGATIVE_OR_ZERO',
'POINTS_OVER_MAXIMUM',
]
if (MANUAL_ERROR_CODES.includes(err.code)) {
client.sendMessage(
msg.chat.id,
'ERROR: ' + err.message + "\nType '!help' for more information."
)
} else {
client.sendMessage(
msg.chat.id,
'ERROR, please use the following format:\n!dish [type of praise] to [handle, handle, handle] for [reason]'
)
}
}
}
// Try to intelligently format the receiver field
function findReceiver(privateRooms, receiver) {
if (receiver.startsWith('<a href="https://matrix.to/#/')) {
receiver = receiver.substring(29, receiver.indexOf('">'))
}
if (receiver[0] == '@') {
receiver = receiver.substr(1)
}
/**if (receiver[0] != '@') {
receiver = `@${receiver}`
}**/
// defaults
let userInRoom = false
let multipleUsers = false
let display_name = ''
if (privateRooms[receiver] != null) {
userInRoom = true
}
/**if (receiver.split(':').length < 2 && !userInRoom) {
for (let domain of domains) {
if (room.getMember(`${receiver}:${domain}`) != null) {
receiver = `${receiver}:${domain}`
display_name = room.getMember(receiver).name
// if a user has already been found under a different domain
if (userInRoom) {
multipleUsers = true
receiver = receiver.split(':')[0]
break
}
// user under this domain was found.
userInRoom = true
}
}
}**/
return {
userInRoom,
receiver,
multipleUsers,
display_name,
}
}