This repository was archived by the owner on Jan 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
256 lines (203 loc) · 7.05 KB
/
commands.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
const Promise = require('bluebird')
const { Op } = require('sequelize')
const constants = require('./constants')
const moment = require('moment')
const {
isAdmin,
formatDisplayName,
formatUsername,
capitalise,
} = require('./lib/bot-utils')
module.exports = ({
bot,
models,
}) => {
bot.onText(/^\/me$/, async msg => {
const {
from: {
id: telegram_id,
first_name,
last_name,
username
}
} = msg
const user = await models.User.findOne({
where: {
telegram_id,
},
include: [
models.Category,
]
})
if (!user) {
return bot.sendMessage(msg.chat.id, 'Non sei registrato! Usa /registra per registrarti')
}
const data = [
`Name: ${formatDisplayName(first_name, last_name)}`,
`Username: ${formatUsername(username)}`,
`Categorie: ${user.Categories.length ? user.Categories.map(cat => cat.title).join(' ') : 'None'}`,
`Registrato il ${moment(user.created_at).toISOString()}`
].join('\n')
return bot.sendMessage(msg.chat.id, data)
})
bot.onText(/^\/iscrivi\s+([a-z\s]+)$/i, async (msg, match) => {
const categories = match[1].split(' ')
const {
from: {
id: telegram_id,
}
} = msg
const response = []
if (!categories.length) {
return bot.sendMessage(msg.chat.id, 'Nessuna categoria specificata!')
}
const user = await models.User.findOne({
where: {
telegram_id,
}
})
if (!user) {
return bot.sendMessage(msg.chat.id, 'Non sei registrato! Usa /registra per registrarti')
}
const categoryInstances = await models.Category.findAll({
where: {
name: {
[Op.in]: categories
}
}
})
const notFoundCategories = categories.reduce((acc, category) => {
return categoryInstances.find(instance => instance.name === category) ? acc : [ ...acc, category ]
}, [])
if (notFoundCategories.length < categoryInstances.length) {
response.push('Aggiungo alle categorie: ' + categoryInstances.map(cat => cat.name).join(' '))
}
if (notFoundCategories.length) {
response.push('Categorie non trovate: ' + notFoundCategories.join(' '))
}
console.log(categoryInstances.length, notFoundCategories.length)
await Promise.map(categoryInstances, category => {
return models.UserCategory.findOrCreate({
where: {
user_id: user.id,
category_id: category.id,
}
})
})
return bot.sendMessage(msg.chat.id, response.join('\n'))
})
bot.onText(/^\/disiscrivi\s+([a-z\s]+)$/i, async (msg, match) => {
const categories = match[1].split(' ')
const {
from: {
id: telegram_id,
}
} = msg
const response = []
if (!categories.length) {
return bot.sendMessage(msg.chat.id, 'Nessuna categoria specificata!')
}
const user = await models.User.findOne({
where: {
telegram_id,
}
})
if (!user) {
return bot.sendMessage(msg.chat.id, 'Non sei registrato! Usa /registra per registrarti')
}
const categoryInstances = await models.Category.findAll({
where: {
name: {
[Op.in]: categories
}
}
})
const notFoundCategories = categories.reduce((acc, category) => {
return categoryInstances.find(instance => instance.name === category) ? acc : [ ...acc, category ]
}, [])
if (notFoundCategories.length < categoryInstances.length) {
response.push('Disiscritto da: ' + categoryInstances.map(cat => cat.name).join(' '))
}
if (notFoundCategories.length) {
response.push('Categorie non trovate: ' + notFoundCategories.join(' '))
}
console.log(categoryInstances.length, notFoundCategories.length)
await models.UserCategory.destroy({
where: {
user_id: user.id,
category_id: {
[Op.in]: categoryInstances.map(instance => instance.id)
},
}
})
return bot.sendMessage(msg.chat.id, response.join('\n'))
})
bot.onText(/^\/aggiungi_categoria\s+([a-z]+)\s+([a-z]+)/i, async (msg, match) => {
const [nyaasu, name, title] = match
const category = await models.Category.findOne({
where: {
[Op.or]: {
name,
title,
}
}
})
if (category) {
return bot.sendMessage(msg.chat.id, 'La categoria già esiste!')
}
await models.Category.create({
name,
title,
})
return bot.sendMessage(msg.chat.id, `Categoria ${name} creata!`)
})
bot.onText(/^\/registra$/i, async msg => {
const {
from: {
id: telegram_id,
first_name,
last_name,
username,
}
} = msg
if (!username) {
return bot.sendMessage(msg.chat.id, 'Imposta un username per registrarti!')
}
const existingUser = await models.User.findOne({
where: {
telegram_id,
}
})
if (existingUser) {
return bot.sendMessage(msg.chat.id, 'Sei già iscritto!')
}
await models.User.create({
telegram_id,
first_name,
last_name,
username,
})
return bot.sendMessage(msg.chat.id, 'Registrato! BUON GIORNISSIMO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! KAFFEEEEEEEeeëeéeè?????????????????????')
})
bot.onText(/^\/chiama\s+([a-z]+)$/, async (msg, match) => {
const categoryName = match[1]
const category = await models.Category.findOne({
where: {
[Op.or]: {
title: categoryName,
name: categoryName,
}
},
include: [models.User]
})
if (!category) {
return bot.sendMessage(msg.chat.id, `La categoria ${categoryName} non esiste!`)
}
const response = `${capitalise(category.title)}, a rapporto! ${category.Users.map(user => formatUsername(user.username)).sort().join(' ')}`
return bot.sendMessage(msg.chat.id, response)
})
bot.onText(/^\/categorie$/, async msg => {
const categories = await models.Category.findAll()
return bot.sendMessage(msg.chat.id, categories.map(category => category.name).join(' '))
})
}