This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
bot.js
58 lines (54 loc) · 1.92 KB
/
bot.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
const { Telegraf } = require('telegraf')
const mongoose = require('mongoose')
const bcrypt = require('bcrypt')
const { Registration, UserChat } = require('./models')
const bot = new Telegraf(process.env.BOT_TOKEN)
bot.start(async (ctx) => {
const userChat = await UserChat.findOne({ chat_id: ctx.chat.id })
if (userChat) {
ctx.reply('Welcome!')
} else {
ctx.reply('Welcome!\nPlease register using the command:\n/reg <user_id>#<reg_code>')
}
})
bot.command('reg', async (ctx) => {
const userChat = await UserChat.findOne({ chat_id: ctx.chat.id })
if (userChat) {
ctx.reply('You are already registered.')
return
}
const splitted = ctx.message.text.split(/\s+/)
if (splitted.length < 2) {
ctx.reply('You must enter user id and registration code to register.\nUse commands:\n/reg <user_id>#<reg_code>')
} else {
const regInput = splitted[1].split('#')
if (regInput.length < 2) {
ctx.reply('You must enter user id and registration code to register.\nUse commands:\n/reg <user_id>#<reg_code>')
} else {
const reg = await Registration.findOne({ user_id: regInput[0] })
if (reg) {
if (bcrypt.compareSync(regInput[1], reg.reg_code)) {
const result = await UserChat.findOne({ user_id: reg.user_id })
if (result) {
userChat.chat_id = ctx.chat.id
await userChat.save()
} else {
const userChatNew = new UserChat({
_id: new mongoose.Types.ObjectId(),
user_id: reg.user_id,
chat_id: ctx.chat.id
})
await userChatNew.save()
}
await reg.delete()
ctx.reply('You have successfully registered.')
} else {
ctx.reply('Failed to register!\nWrong user id or registration code.')
}
} else {
ctx.reply('Failed to register!\nWrong user id or registration code.')
}
}
}
})
module.exports = bot