-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
executable file
·80 lines (67 loc) · 2.49 KB
/
utils.js
File metadata and controls
executable file
·80 lines (67 loc) · 2.49 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
/* Functions that are not inherently part of Telegram bots. These are used for convenience. */
import { Update, Context } from "./base.js";
import { Permissions } from "./constants.js";
function filterObject(obj){
return Object.fromEntries(
Object.entries(obj).filter(([key, value]) => value !== null && value !== undefined)
);
}
/**
*
* @param {Permissions} requiredPermissions
* @returns
*/
function accessControl(requiredPermissions = Permissions.MEMBER) {
const hasAllPerms = (userPerms, requiredPerms) => (userPerms & requiredPerms) === requiredPerms;
return (handler) => async (update, context) => {
const userId = update.effective_user?.id;
const chatId = update.effective_chat?.id;
let userPermissions = Permissions.MEMBER;
try {
const admins = await context.bot.getChatAdministrators({ chat_id: chatId });
if (admins?.some(a => a?.status === "creator" && a?.user?.id === userId)) {
userPermissions |= Permissions.OWNER | Permissions.ADMIN;
}
else if (admins?.some(a => a?.status === "administrator" && a?.user?.id === userId)) {
userPermissions |= Permissions.ADMIN;
}
} catch (e) {
console.error("getChatAdministrators failed:", e);
}
if (hasAllPerms(userPermissions, requiredPermissions)) {
return handler(update, context);
}
await denyAccess(update, "You do not have the necessary permission to perform this action.");
};
}
/**
* Helper to send a denied access message.
*/
async function denyAccess(update, reason) {
await update.effective_chat.sendMessage({
text: `<b>Access Denied</b>\n${reason}`,
});
}
function parseCommand(input) {
const commandRegex = /^\/(\S+)(?:\s+(.*))?$/;
const match = input.match(commandRegex);
if (!match) return [];
const argsPart = match[2] || '';
// Updated regex to handle both curly quotes (“ ”) and straight quotes (" ")
const tokenRegex = /“([^”]*)”|"(.*?)"|(\S+)/g; // Matches both curly and straight quotes
const args = [];
let tokenMatch;
while ((tokenMatch = tokenRegex.exec(argsPart)) !== null) {
const arg = tokenMatch[1] !== undefined ? tokenMatch[1] :
tokenMatch[2] !== undefined ? tokenMatch[2] :
tokenMatch[3];
args.push(arg);
}
return args;
}
export {
filterObject,
accessControl,
denyAccess,
parseCommand
}