-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.ts
64 lines (58 loc) · 1.76 KB
/
helpers.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
import { Types } from "./deps.ts";
import { ERRORS, PREFIXES } from "./errors.ts";
import {
defaultChatAdministratorRights,
defaultChatPermissions,
} from "./constants.ts";
import { ChatAdministratorRights, ChatPermissions } from "./types.ts";
/** API related properties */
export const api = {
/** Current Bot API version */
BOT_API_VERSION: "6.6",
/** Return API result */
result<T>( // extends Awaited<ReturnType<RawApi[keyof RawApi]>>
result: T,
): Promise<Types.ApiSuccess<T>> {
return Promise.resolve({ ok: true, result });
},
/** Return API error */
error(
error: keyof typeof ERRORS,
message = "",
parameters?: Types.ResponseParameters,
): Promise<Types.ApiError> {
const err = ERRORS[error];
return Promise.resolve({
ok: false,
error_code: err[0],
description: `${PREFIXES[err[0]] ? PREFIXES[err[0]] : ""}${err[1]}${
message ? `: ${message}` : ""
}`,
...(parameters ? { parameters } : {}),
});
},
};
/**
* Collection of functions for generating real-looking random
* values for using in Telegram API requests and responses.
*/
export const rand = {
botId: () => randomNumberInBetween(1000000000, 9999999999),
};
function randomNumberInBetween(start: number, end: number) {
return Math.floor(start + Math.random() * end);
}
/** Returns current time in Telegram's format. */
export function date() {
return Math.trunc(Date.now() / 1000);
}
export function isChatPermission(
permission: string,
): permission is ChatPermissions {
return Object.keys(defaultChatPermissions).includes(permission);
}
export function isChatAdministratorRight(
permission: string,
): permission is ChatAdministratorRights {
return Object.keys(defaultChatAdministratorRights).includes(permission);
}