-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.js
More file actions
129 lines (116 loc) · 3.95 KB
/
bot.js
File metadata and controls
129 lines (116 loc) · 3.95 KB
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
'use strict';
const TelegramApi = require('node-telegram-bot-api');
const {
Queues,
Versions,
Chats,
connectMongoClient,
Admins,
} = require('./mongo');
const {
getDataOptions,
callFunctionWithArgs,
getValuesFromMessage,
} = require('./helpers');
const { botData } = require('./botData');
const { Executor } = require('./onCommand');
const token = process.env.tgToken;
const bot = new TelegramApi(token, { polling: true });
const queuesCollection = new Queues('queues');
const versionCollection = new Versions('versions');
const chatsCollection = new Chats('chats');
const adminsCollection = new Admins('admins');
const versionTypes = ['major', 'minor', 'patch'];
const executor = new Executor(bot, {
queuesCollection,
versionCollection,
chatsCollection,
versionTypes,
adminsCollection,
botData,
});
const PARAMS = new Map([
['start', ['chatId']],
['help', ['chatId', 'userId']],
['info', ['chatId']],
['viewmyqueues', ['chatId']],
['nextVersion', ['chatId', 'userId', 'message']],
['updateVersionDescription', ['chatId', 'userId', 'message']],
['getVersionInfo', ['chatId', 'message']],
['getPreviousVersions', ['chatId', 'message']],
['sendInfoAboutVersion', ['chatId']],
['sendInfoAboutDeveloping', ['chatId']],
['addAdmin', ['chatId', 'userId', 'message']],
['removeAdmin', ['chatId', 'userId', 'message']],
['addOwner', ['chatId', 'userId', 'message']],
['removeOwner', ['chatId', 'userId', 'message']],
['removeFromCustomers', ['chatId', 'userId', 'message']],
['viewCustomers', ['chatId', 'userId']],
['viewAdmins', ['chatId', 'userId']],
['viewOwners', ['chatId', 'userId']],
['addMeToCustomers', ['chatId', 'userId', 'userTag', 'message']],
['removeMeFromCustomers', ['chatId', 'userId']],
['new', ['message', 'chatId', 'userId']],
['look', ['message', 'chatId']],
['find', ['message', 'chatId', 'queuesLimit']],
['delete', ['message', 'chatId', 'userId', 'userTag']],
['addMeToQueue', ['message', 'chatId', 'userId', 'userTag']],
['viewQueue', ['message', 'chatId']],
['tagNext', ['message', 'chatId', 'userId', 'userTag']],
['removeMeFromQueue', ['message', 'chatId', 'userId', 'userTag']],
['lookMyQueues', ['chatId', 'userId', 'userTag', 'queuesLimit']],
['lookMyOwnQueues', ['chatId', 'userId', 'userTag', 'queuesLimit']],
['botJoinedToChat', ['chatId']],
['botLeftTheChat', ['chatId']],
]);
async function start() {
console.log('bot started');
let chatIds = [];
connectMongoClient();
try {
const ids = await chatsCollection.getChatIds();
chatIds = ids.chats.map((obj) => obj.id);
executor.updateNecessaryValues({ chatIds });
const admins = await adminsCollection.getAdminsIds();
await executor.updateNecessaryValues({ creatorsIds: admins });
} catch (e) {
console.log(e);
}
bot.setMyCommands([
{ command: '/start', description: 'Запустити бота' },
{ command: '/info', description: 'Подивитися інформацію про бота' },
{ command: '/help', description: 'Команди для роботи з чергами' },
{ command: '/viewmyqueues', description: 'Подивитися мої черги' },
]);
bot.on('message', async (msg) => {
const parsedMessage = getValuesFromMessage(msg, botData);
if (!parsedMessage) return;
const [commandName, values] = parsedMessage;
if (commandName) {
try {
callFunctionWithArgs(executor, commandName, PARAMS, values);
} catch (error) {
console.log(error);
}
}
});
bot.on('callback_query', async (msg) => {
const data = getDataOptions(msg.data);
const [commandName, message] = data;
const { id, username } = msg.from;
const chatId = msg.message.chat.id;
const values = {
message,
userId: id,
userTag: username,
chatId,
queuesLimit: 10,
};
try {
callFunctionWithArgs(executor, commandName, PARAMS, values);
} catch (error) {
console.log(error);
}
});
}
start();