-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
42 lines (37 loc) · 1.62 KB
/
Copy pathmain.ts
File metadata and controls
42 lines (37 loc) · 1.62 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
import { ChatType, Client, types } from "mtkruto/mod.ts";
import { StorageDenoKV } from "mtkruto/storage/1_storage_deno_kv.ts";
import env from "./env.ts";
const client = new Client(new StorageDenoKV(), env.API_ID, env.API_HASH, { initialDc: "1" }); // the initialDc parameters makes sure that we connect to prod servers
client.on("authorizationState", async ({ authorizationState: { authorized } }) => { // this is called when the client’s connection state is changed, and should be applied before authorizing the client
if (authorized) {
const me = await client.getMe();
console.log(`Running as @${me.username}...`);
}
});
await client.start(env.BOT_TOKEN);
client.use(async (update, next) => {
const chat = (update.callbackQuery?.message ?? update.message ?? update.editedMessage)?.chat;
if (chat && chat.type == ChatType.Private) {
try {
await next();
} catch (err) {
if (err instanceof types.RPCError && err.errorMessage.startsWith("FLOOD_WAIT_")) {
const duration = Number(err.errorMessage.replace("FLOOD_WAIT_", ""));
console.log("Sleeping for", duration, "seconds...");
await new Promise((r) => setTimeout(r, duration * 1000));
try {
await next();
} catch (err) {
console.error(err);
}
} else {
console.error(err);
}
}
}
});
client.use(async (update) => {
const chat = (update.callbackQuery?.message ?? update.message ?? update.editedMessage)?.chat!;
const text = JSON.stringify(update, null, 2);
await client.sendMessage(chat.id, text, { entities: [{ type: "code", offset: 0, length: text.length }] });
});