-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcrew3-module.ts
519 lines (479 loc) · 18.3 KB
/
crew3-module.ts
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import * as dotenv from "dotenv"
import axios, { AxiosInstance } from 'axios'
import FormData from 'form-data'
import delay from 'delay'
import { faker } from "@faker-js/faker"
dotenv.config()
const TWITTER_PHRASES = ['great milestone team ❤️🌎', '👍👍👍👍', '👏', 'We need more of this kind of good news 🚀', 'Nice', 'Good', 'go moon', 'Great news 😊', 'Great project', 'awesome', 'Nice project', 'Good news!', 'Very good', '👌', '🚀🚀🚀', 'amazing', 'Cool!']
// Helpers
export const randomInt = (value) => Math.floor(Math.random() * value)
export const sleep = async (value) => delay(value + randomInt(value))
// Crew Module
export class CrewProfile {
instantiatedAt = new Date()
crew3: AxiosInstance
headers
constructor(headers, httpProxyAgent = null) {
headers.origin = "https://crew3.xyz"
this.headers = headers
this.crew3 = axios.create({
baseURL: 'https://api.crew3.xyz/',
headers: this.headers,
httpAgent: httpProxyAgent
})
console.log(`New Crew3 profile constructed`)
}
/**
* @returns User main profile data or false
*/
getUser = async () => await this.crew3
.get('users/me')
.then(r => r.data)
.catch(e => {
console.log('User not connected to Crew3', e?.response?.data)
return false
})
/**
* @returns All user joined communities
*/
getUserCommunities = async () => await this.crew3
.get('users/me/communities')
.then(r => r.data.communities)
.catch(e => {
console.log('User not connected to Crew3', e?.response?.data)
return []
})
/**
* @param subdomain subdomain name from url or API
* @returns invite url for given subdomain
*/
getReferralLink = async (subdomain: string) =>
await this.crew3.get(`communities/${subdomain}/users/me/referral-link`)
.then(r => `https://${subdomain}.crew3.xyz/invite/${r.data.id}`)
.catch(e => `https://${subdomain}.crew3.xyz`)
/**
* @param subdomain of community
* @param invite code from link
* @returns invite url for given subdomain
*/
joinByReferral = (subdomain: string, invite: string) => {
return axios.get(`https://${subdomain}.crew3.xyz/invite/${invite}`, { headers: this.headers })
.then(async (r) =>
this.crew3.post('users/me/accept-invitation', { invitationId: invite }, this.getHeaders(subdomain))
.then(r => {
return `Successfully joined to community!`
})
)
.catch(e => {
return e?.response?.data?.message || `Wrong invite link`
})
}
/**
* @param subdomain of community
* @param email of user
* @returns mail status
*/
sendMagicLink = (subdomain: string, email: string) => {
return this.crew3.post('authentification/email/magic-link', {
email: email,
subdomain: subdomain
}, this.getHeaders(subdomain))
.then(r => {
return `Successfully joined to community!`
})
}
/**
* Parse pages with Crew3 communities
* @param type 'all', 'new', 'hot'
* @param to end page
* @param from start page
* @param timeout sleep time between parsing
* @returns array of communities
*/
getCommunities = async (type = 'new', to = 5, from = 0, timeout = 2000) => {
let communities = []
for (let i = from; i < to; i++) {
console.log(`Parse ${type} page ${i} ...`)
const page = (await this.crew3.get(`communities?page=${i}&category=${type}`)).data.communities
if (page.length > 0 ) communities = [...communities, ...page]
else break
await sleep(timeout)
}
console.log(`Find ${communities.length} ${type} communities...`)
return communities
}
/**
* Join Crew3 communities
* @param communities array of communities
* @param timeout to avoid account ban
* @returns array of communities
*/
joinCommunities = async (communities, timeout = 2000) => {
const report = [`Start join to ${communities.length} communities:`]
for(const community of communities) {
if (community.visibility !== 'private') {
const res = await this.joinCommunity(community.subdomain)
if (res) report.push(`Community ${community.name} was joined successfully ✅.`); else report.push(`Couldn't join ${community.name} ❌.`)
} else report.push(`${community.name} is private! Can't subscribe to it.`)
await sleep(timeout)
}
return report
}
/**
* @returns Get user community stats
*/
getUserCommunityStats = async (community , user) => await this.crew3
.get(`communities/${community?.subdomain || community}/users/${user.id}`)
.then(r => r.data)
.catch(e => {
console.log('Data from community unavailiable', e?.response?.data)
return {}
})
/**
* Notify message for telegram bot
* @param community from API
* @param project for repeat messages
* @returns array of communities
*/
communityMessage = async (community, user = null) => {
const stats = user ? await this.getUserCommunityStats(community, user) : null;
return `*${community.name}*${community.description ? `\n\n${community.description}` : ''}
*Crew3:* [https://${community.subdomain}.crew3.xyz](https://${community.subdomain}.crew3.xyz)
*Discord:* [${community.discord}](${community.discord || 'NONE'})
*Twitter:* [https://twitter.com/${community.twitter}](https://twitter.com/${community.twitter})
*Opensea:* [${community.opensea}](${community.opensea || 'NONE'})
Blockchain: *${community.blockchain.toUpperCase()}*
${community.sector ? `Sector: *${community.sector.toUpperCase()}*` : ''}
Community rank: *${community.rank}*
Quests: *${community.quests}*
Required: *${Object.entries(community.requiredFields).filter(([key, value]) => value)
.map(([key, value]) => `${key.replace('fill', '').replace('link', '')}`).join(', ')}*
${stats ? `
Invites: *${stats.invites}* | Level: *${stats.level}*
Leaderboard rank: *${stats.rank}*
Claimed XP: *${stats.xp}*` : ''}`}
/**
* Join single community without invite link
* @param subdomain from API
* @returns array of communities
*/
joinCommunity = async (subdomain) =>
this.crew3
.post(`communities/${subdomain}/members`)
.then(async (r) => true)
.catch(e => {
console.log(e)
return false
})
/**
* Search first community matched keyword
* @param keyword for search
* @returns array of communities
*/
searchCommunity = async (keyword) =>
this.crew3
.get(`communities?${new URLSearchParams({ search: keyword }).toString()}&limit=10`)
.then(r => r.data.communities[0])
.catch(e => {
console.log(e)
return false
})
/**
* Replace headers for community requests
* @param subdomain from API
* @returns array of communities
*/
getHeaders = (subdomain) => ({ headers: {
origin: `https://${subdomain}.crew3.xyz`,
referer: `https://${subdomain}.crew3.xyz`,
cookie: this?.headers?.cookie?.replace('root', subdomain)
}})
/**
* Replace headers for claim requests by community
* @param subdomain from API
* @returns array of communities
*/
getClaimHeaders = (subdomain) => Object.assign({
"content-type": `multipart/form-data;`
}, this.getHeaders(subdomain))
/**
* Leave community
* @param subdomain from API
* @returns array of communities
*/
leaveCommunity = async (community, user) =>
this.crew3
.delete(`communities/${community.subdomain ? community.subdomain : community}/members/${user.id}`)
.then(() => 'success')
.catch(e => e?.response?.data?.message || `error`)
/**
* Leave list of communities
* @param communities from API
* @param user from API
* @param timeout to avoid account ban
* @returns true after processing
*/
leaveCommunities = async (communities, user, timeout = 1000) => {
for (const community of communities) {
await this.leaveCommunity(community, user)
await sleep(timeout)
}
return true
}
/**
* Get Claimed answers of community
* @param community from API
* @param page
* @param pageSize
* @returns true after processing
*/
getCommunityAnswers = (community, page = 0, pageSize = 10) => {
return this.crew3
.get(`communities/${community.subdomain}/users/me/notifications?page=${page}&page_size=${pageSize}`)
.then(r => {
const answers = r.data.notifications
.filter(note =>
note.status === 'success' &&
note.type === 'claim' &&
['quiz', 'text'].includes(note.events[0].valueType)
)
return ({
community: community.name.replace(/[^a-zA-Z0-9 ]/, ''),
answers: answers.map(answer => ({
question: answer.title.trim(),
answer: answer.events[0].value
}))
})
})
.catch(e => e?.response?.data?.message || `error`)
}
/**
* Get community quests by themes
* @param subdomain from API
* @returns array of communities
*/
getQuests = async (subdomain) =>
this.crew3.get(`communities/${subdomain}/questboard`, this.getHeaders(subdomain))
.then(r => {
return r.data.filter(quests => !quests.deleted)
})
.catch(e => {
console.log(subdomain + ' is private!')
return []
})
/**
* Get all community quests
* @param subdomain from API
* @returns array of quests
*/
getAllQuests = async (subdomain) => {
const quests = []
for (const theme of await this.getQuests(subdomain))
for (const quest of theme.quests)
quests.push(quest)
return quests
}
// Get unlocked and available quests
getUnlockedQuests = (quests) => quests.filter((item) => item.unlocked && !item.inReview && item.open)
// Get invites quests
getInvitesQuests = (quests) => quests.filter((item) => item.submissionType === 'invites')
// Get quests which can give role
getRoleQuests = (quests) => quests.filter((item) => item?.reward[0]?.type === 'role' || item?.reward[1]?.type === 'role')
// Get autovalidate quests
getAutoValidateQuests = (quests) => quests.filter((item) => item.autoValidate)
// Get twitters quests
getTwittersQuests = (quests) => quests.filter((item) => item.submissionType === 'twitter')
/**
* Get twitter tasks for quest
* @param quest from API
* @returns array of pairs { task, link }
*/
getTwitterTasksForQuest = async (quest) => {
const actions = []
const tasks = quest.validationData
if (tasks.actions.includes('follow'))
actions.push({
task: 'follow',
link: `https://twitter.com/intent/user?screen_name=${tasks.twitterHandle}`
})
if (tasks.actions.includes('like'))
actions.push({
task: 'like',
link: `https://twitter.com/intent/like?tweet_id=${tasks.tweetId}`
})
if (tasks.actions.includes('retweet'))
actions.push({
task: 'retweet',
link: `https://twitter.com/intent/retweet?tweet_id=${tasks.tweetId}`
})
if (tasks.actions.includes('reply'))
actions.push({
task: 'reply',
link: `https://twitter.com/intent/tweet?in_reply_to=${tasks.tweetId}&text=${(tasks.defaultReply ? `${encodeURIComponent(tasks.defaultReply)}` : encodeURIComponent(TWITTER_PHRASES[randomInt(TWITTER_PHRASES.length)]))}`
})
if (tasks.actions.includes('tweet'))
actions.push({
task: 'tweet',
link: `https://twitter.com/intent/tweet?&text=${encodeURIComponent(tasks.defaultTweet + ' ' + tasks.tweetWords.join(' '))}`
})
return [...new Set(actions)]
}
// Get discord quests
getDiscordQuests = (quests) => quests.filter((item) => item.submissionType === 'discord')
/**
* Get discord task for community
* @param quests list from API
* @returns array of links to join
*/
getDiscordTasksForQuests = (quests) => {
const discordTasks = []
const discordQuests = this.getDiscordQuests(quests)
for (const quest of discordQuests) {
const tasks = quest.validationData
if (quest.validationData.inviteLink) {
discordTasks.push(`${tasks.inviteLink}`)
}
}
return [...new Set(discordTasks)]
}
/**
* Get quests for communities by submission type
* @param communities list from API
* @returns array of links to join
*/
getQuestsByType = async (communities, types) => {
let quests = []
for (const community of communities) {
const all = await this.getAllQuests(community.subdomain)
const unlocked = this.getUnlockedQuests(all)
quests = [...quests, ...unlocked.filter(item => types.includes(item.submissionType))]
console.log(quests)
}
return [...new Set(quests)]
}
/**
* Get twitter tasks for communities
* @param communities list from API
* @returns array of links to join
*/
getTwitterTasksForCommunities = async (communities) => {
const report = [`Start collect discord quests for ${communities.length} communities:`]
for (const community of communities) {
const all = await this.getAllQuests(community.subdomain)
const unlocked = this.getUnlockedQuests(all)
const quests = unlocked.filter(item => ['discord'].includes(item.submissionType))
console.log(quests)
}
return report
}
/**
* Claim quest for subdomain
* @param subdomain from API
* @param quest from API
* @param answer form answers list, nessesary for text/quiz/url/image types of quests
* @returns boolean
*/
claimQuest = async (subdomain, quest, answer = null) => {
console.log(`\nTry to claim ${subdomain} community quest,\nType: ${quest.submissionType},\nName: ${quest.name}${answer ? `\nAnswer: ${answer}` : ''}`)
const data = new FormData()
if (answer && answer.length > 0)
data.append("value", answer)
else if (['quiz', 'text', 'url', 'image'].includes(quest.submissionType)) {
return `Quest _${quest.name}_ require answer:` + JSON.stringify(quest.validationData.question | quest.validationData)
}
data.append("questId", quest.id)
data.append("type", quest.submissionType)
data.append("token", "03AIIukzihdCbeBYOTilKEJhVc5vbbPPVmhZiHnMEaP00-xb22Ze9ld8R2lQiLEOYOcpIHIYVwbCvW1iPd4YkQcW2njEkT1EZfRi9rKAjz17hwVMYuYZeQvnBrPwMOz1XcIH2u3etW67yH6wUoXq4hpmbJdSEXKokz_tRMeOmScNCaQ7Pwp70yuMj8x0jPiBmkmSnjAmttyEZKN7I7DJcJUwW53v9Vkvne1YlwcpMQ2TOC2RJwTsSpr-gbgWcQXsjinq-81z7JFBv-Pi-iOgk5k416-CYBWkoFjqhk2kHHKkDwwiaZFHpA7SAAIvzKeZubg0gPeAOh_7K0CZPPO3jJojOUU1Iyoj-803vIzHMiuvxz-5LY2a_M2OH7RaKmCnAtFmAMKWiVho_jeTjaBEOUYpLmb1sA-ek6mqq0QT7R1-S-lknxP5uRipYGVkeBXRH6SxBijJ27rFgjPSgzjBAZERj5xB6aUnfw33ATWYK5jO_Q5e9nVUIkJVoReJQxKqwpGQKUpt_xsC3p")
return await this.crew3.post(`communities/${subdomain}/quests/${quest.id}/claim`, data, this.getHeaders(subdomain))
.then(r => {
console.log(`Claim status - ${r.data.status}`)
if (r.data.status === 'success')
return `Claim *${quest.name}*, earn *${r.data.xp}* points`
return `${quest.name} already claimed!`
})
.catch(async (e) => {
if (e?.response?.data?.message) {
console.log('e?.response?.data?.message')
return e?.response?.data?.message
} else if (e?.response?.data?.error) {
console.log('e?.response?.data?.error')
console.log(e?.response?.data)
return e?.response?.data?.error.message || e?.response?.data?.error?.follow || e?.response?.data?.error?.retweet || e?.response?.data?.error?.reply || e?.response?.data?.error?.like
} else {
console.log(e)
}
return `Something wrong with ${quest.name}`
})
}
/**
* Change user settings
* @param user - user data
* @param subdomain - community subdomain
* @param address - new ETH address
* @param blockchain - new blockchain address
* @param username - new username
* @returns boolean
*/
changeSettings = async (subdomain, address = null, blockchain = 'etherium', username = null) => {
console.log(`\nTry to change settings ${subdomain}, ${address}, ${blockchain}, ${username}`)
const data = new FormData()
if (address) {
data.append("address", address)
data.append("blockchain", blockchain)
} else {
data.append("username", username || faker.internet.userName())
data.append("displayedInformation", '["discord", "twitter"]')
}
return await this.crew3.patch(`users/me`, data, this.getHeaders(subdomain))
.then(r => {
console.log(`New User data - ${r.data.name}`)
return r.data
})
.catch(async (e) => {
if (e?.response?.data?.message) {
console.log('e?.response?.data?.message')
return e?.response?.data?.message
} else if (e?.response?.data?.error) {
console.log('e?.response?.data?.error')
console.log(e?.response?.data)
return e?.response?.data?.error.message || e?.response?.data?.error?.follow || e?.response?.data?.error?.retweet || e?.response?.data?.error?.reply || e?.response?.data?.error?.like
} else {
console.log(e)
}
console.log(`Something wrong with changing ${subdomain}`)
return null
})
}
/**
* Claim all quests by type
* @param communities from API
* @param answer form answers list, nessesary for text/quiz/url/image types of quests
* @param timeout to avoid account ban
* @returns array of logs
*/
claimQuestsByType = async (communities, types = ['none'], timeout = 2000, answers) => {
const report = [`Start claim *${types.join(', ')}* quests for ${communities.length} communities:`]
for (const community of communities) {
const all = await this.getAllQuests(community.subdomain)
const unlocked = this.getUnlockedQuests(all)
const quests = unlocked.filter(item => types.includes(item.submissionType))
const communityName = community.name.replace(/[^a-zA-Z0-9 ]/, '')
if (quests.length > 0)
report.push(`*${community.name}* \`${community.subdomain}\` (${quests.length} quests):`)
for (const quest of quests) {
let answer
if (['quiz', 'text', 'url', 'image'].includes(quest.submissionType) && answers[communityName]) {
answer = answers[communityName][quest.name.trim()]
if (answer)
report.push(' - ' + await this.claimQuest(community.subdomain, quest, answer ? answer : null))
} else {
report.push(' - ' + await this.claimQuest(community.subdomain, quest))
}
}
await sleep(timeout)
}
return report.length > 1
? report
: [`No claimable quests with type *"${types.join(',')}"* in ${communities.length} communities!`]
}
}