-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.js
More file actions
153 lines (134 loc) · 4.29 KB
/
helpers.js
File metadata and controls
153 lines (134 loc) · 4.29 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
const getCommandName = (text, botTag, commandsInfo) => {
if (text.includes(botTag)) {
const noTagCommand = text.slice(1, text.indexOf(botTag));
return noTagCommand;
}
const { common, admin, owner } = commandsInfo;
const commands = [...common.keys(), ...admin.keys(), ...owner.keys()];
for (const command of commands) {
if (text.startsWith(command)) return command.replace('/', '');
}
};
const cutInputText = (text, botTag, command) => {
let infoFromCommand = text.replace(command, '');
if (infoFromCommand.includes(botTag)) {
infoFromCommand = infoFromCommand.replace(botTag, '');
}
infoFromCommand = infoFromCommand.trim();
return infoFromCommand;
};
const getUpdatesType = (text, types) => {
const existingTypes = types.filter((type) => text.includes(type));
if (existingTypes.length === 0) return 'Ви не вказали тип';
if (existingTypes.length !== 1)
return 'Має бути 1 тип версії \'major\', \'minor\' або \'patch\'';
return existingTypes[0];
};
const generateNextVersionNumber = (previousNumber, versionTypes, type) => {
if (!previousNumber) return '1.0.0';
const typeNumber = versionTypes.indexOf(type);
const previousNumbers = previousNumber.split('.');
previousNumbers[typeNumber]++;
for (let i = 0; i < previousNumbers.length; i++) {
if (i > typeNumber) previousNumbers[i] = 0;
}
return previousNumbers.join('.');
};
const getDataOptions = (data) => data.split(':');
const hasUserAccess = (userId, ...collections) => {
for (const collection of collections) {
if (collection.map((user) => user.id).includes(userId)) return true;
}
return false;
};
const indexOfUser = (userId, collection) =>
collection.map((user) => user.id).indexOf(Number(userId));
const checker = (collection) => {
let errorMsg = undefined;
for (const obj of collection) {
if (!obj.check) {
errorMsg = obj.msg;
break;
}
}
return errorMsg;
};
const queueNameChecker = (queueName) => {
const chars = '{}[]/?|~!@#$^;:&*()+';
if (!queueName) {
return 'Ви не ввели назву черги!';
}
for (const char of chars) {
if (queueName.includes(char)) {
return `Символи ${chars} є недопустимими`;
}
}
};
const callFunctionWithArgs = (commandsFunctions, command, params, values) => {
const commandParams = params.get(command);
if (!commandParams) return;
const valuesArray = commandParams.map((param) => values[param]);
return commandsFunctions[command](...valuesArray);
};
const isBotLeftGroup = (msg, botId) => msg?.left_chat_member?.id === botId;
const isBotJoinedGroup = (msg, botId) => msg?.new_chat_member?.id === botId;
const getValuesFromMessage = (msg, botData) => {
const chatId = msg.chat.id;
const { botId, tag, commandsInfo } = botData;
if (isBotJoinedGroup(msg, botId)) return ['botJoinedToChat', { chatId }];
if (isBotLeftGroup(msg, botId)) return ['botLeftTheChat', { chatId }];
if (msg.text && msg.text.startsWith('/')) {
const text = msg.text;
const commandName = getCommandName(text, tag, commandsInfo);
const command = '/' + commandName;
const { id, username } = msg.from;
const message = cutInputText(text, tag, command);
const values = {
chatId,
message,
userId: id,
queuesLimit: 10,
userTag: username,
};
return [commandName, values];
}
};
const getCommandsDescription = (commands) => {
let result = '';
for (const command of commands.entries()) {
result += `${command.join(' ')}\n`;
}
return result;
};
const validateVersionNumber = (message) => {
const lines = message.split('.');
for (const line of lines) {
if (String(Number(line)) !== line) return false;
}
return true;
};
const isIdValid = (id) => String(Number(id)) === id;
const formattedUserInfo = (id, tag, description) => (
`<b>id</b> - <i>${id}</i>\n` +
`<b>tag</b> - <i>@${tag}</i>\n` +
`<b>description</b> - <i>${description}</i>\n\n`);
module.exports = {
getCommandName,
getUpdatesType,
generateNextVersionNumber,
getDataOptions,
checker,
queueNameChecker,
callFunctionWithArgs,
isBotLeftGroup,
isBotJoinedGroup,
getValuesFromMessage,
hasUserAccess,
indexOfUser,
getCommandsDescription,
validateVersionNumber,
isIdValid,
cutInputText,
formattedUserInfo,
};