forked from Giveth/giveth-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointsbot.js
253 lines (229 loc) · 6.6 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
const dayjs = require('dayjs')
const {
point_types,
domains,
max_points,
sheet_id,
sheet_tab_name,
} = require('./constants')
const BigNumber = require('bignumber.js')
const { google } = require('googleapis')
exports.handlePointGiving = function(
auth,
event,
room,
toStartOfTimeline,
client
) {
if (event.getType() === 'm.room.message' && toStartOfTimeline === false) {
client.setPresence('online')
let message = event.getContent().body
const roomId = room.roomId
// Support for "! dish" command
if (message[1] === ' ') {
message = message.replace(' ', '')
}
const command = message.toLowerCase().split(' ')[0]
if (command == '!help') {
client.sendTextMessage(
roomId,
'dish points using the following format:\n!dish [#of points] [type of points] points to [handle] for [reason]'
)
} else if (command == '!dish') {
handleDish(event, room, client, auth)
} else if (command == '!sheet') {
client.sendTextMessage(
roomId,
`the rewardDAO sheet can be found here: https://docs.google.com/spreadsheets/d/${sheet_id}`
)
}
}
}
function handleDish(event, room, client, auth) {
let message = event.getContent().body
let matched = false
let regex = /!\s*dish\s+(\S+)\s+(\S+)\s+points\s+to\s+(\S+)\s+for\s+([^\n]+)/gi
if (event.getSender() == `@${process.env.BOT_USER}:matrix.org`) {
// we sent the message.
return
}
if (event.getContent().formatted_body) {
message = event.getContent().formatted_body
regex = /!\s*dish\s+(\S+)\s+(\S+)\s+points\s+to\s+([^\n]+)\s+for\s+([^\n]+)/gi
}
if (message.trim()[0] == '>') {
// quoting another user, skip the quoted part
matched = true
message = message.split('\n\n')[1]
}
let match
do {
match = regex.exec(message)
if (match) {
tryDish(
event,
room,
client,
auth,
match[1],
match[2],
match[3].trim(),
match[4]
)
matched = true
}
} while (match)
if (!matched) {
client.sendTextMessage(
room.roomId,
'ERROR, please use the following format:\n!dish [#of points] [type of points] points to [handle] for [reason]'
)
}
}
function tryDish(event, room, client, auth, nPoints, type, user, reason) {
try {
const sender = event.getSender()
const amount = new BigNumber(nPoints)
type = type.toUpperCase()
if (amount.isNaN()) {
const pointError = new Error(
'Invalid number of points 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 points!"
)
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} points each time!`
)
pointError.code = 'POINTS_OVER_MAXIMUM'
throw pointError
}
if (!point_types.includes(type)) {
const typeError = new Error(
`Invalid point type '${type}'. Please use one of ${point_types}.`
)
typeError.code = 'POINT_TYPE_DOES_NOT_EXIST'
throw typeError
}
let { userInRoom, receiver, display_name, multipleUsers } = findReceiver(
room,
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://riot.im/app/#/room/${room.roomId}/${event.getId()}`
const values = [
[
receiver,
sender,
reason,
amount.toFormat(2),
type,
date,
link,
display_name,
],
]
const body = { values }
const sheets = google.sheets({ version: 'v4', auth })
sheets.spreadsheets.values.append(
{
spreadsheetId: sheet_id,
range: sheet_tab_name,
valueInputOption: 'USER_ENTERED',
resource: body,
},
err => {
if (err) return console.log('The API returned an error: ' + err)
client.sendTextMessage(
room.roomId,
`${sender} dished ${amount} ${type} points to ${receiver}`
)
}
)
} catch (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',
]
console.log(err)
if (MANUAL_ERROR_CODES.includes(err.code)) {
client.sendTextMessage(
room.roomId,
'ERROR: ' + err.message + "\nType '!help' for more information."
)
} else {
client.sendTextMessage(
room.roomId,
'ERROR, please use the following format:\n!dish [#of points] [type of points] points to [handle] for [reason]'
)
}
}
}
// Try to intelligently format the receiver field
function findReceiver(room, receiver) {
if (receiver.startsWith('<a href="https://matrix.to/#/')) {
receiver = receiver.substring(29, receiver.indexOf('">'))
}
if (receiver[0] != '@') {
receiver = `@${receiver}`
}
// defaults
let userInRoom = false
let multipleUsers = false
let display_name = ''
if (room.getMember(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,
}
}