-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
109 lines (93 loc) · 2.92 KB
/
index.ts
File metadata and controls
109 lines (93 loc) · 2.92 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
import "dotenv/config";
import { search, sites } from "booru";
import { TelegramClient } from "@mtcute/node";
import { readdirSync } from "fs";
import storage from "./storage/main.js";
import { generateResults } from "./utils/post.js";
import { parsePost } from "./utils/utils.js";
import { Caching } from "./utils/caching.js";
const tg = new TelegramClient({
apiId: Number.parseInt(process.env.API_ID || "17349"),
apiHash: process.env.API_HASH || "344583e45741c457fe1862106095a5eb",
storage,
});
tg.onError.add((err) => console.error(err));
const manager = new Caching(tg, storage);
interface Command {
commandName: string;
execute(...args: any): any;
}
const command = new Map<string, Command>();
const commandFiles = readdirSync(new URL("commands/", import.meta.url)).filter(
(file) => !file.endsWith(".map"),
);
for (const file of commandFiles) {
import(new URL("commands/" + file, import.meta.url).toString()).then((cmd) =>
command.set(cmd.default.commandName, cmd.default),
);
}
tg.onNewMessage.add((msg) => {
if (process.env.PASSIVE_CACHING && msg.viaBot?.isSelf) {
const post = parsePost(msg);
if (post?.file_id) storage.postCache.add(post);
return;
}
if (!msg.text.startsWith("/")) return;
const cmd = command.get(msg.text.slice(1));
if (!cmd) return;
cmd.execute(tg, msg, manager);
});
const allSites = Object.values(sites).flatMap((site) => site.aliases);
const limit = 50;
const cacheTime = 60 * 60 * 1;
tg.onInlineQuery.add(async (inlineQuery) => {
if (!inlineQuery.query || !inlineQuery.query.trim()) {
return tg.answerInlineQuery(inlineQuery, []);
}
const page = parseInt(inlineQuery.offset || "0", 16) || 0;
const tags = inlineQuery.query
.trim()
.split(/\s+/)
.filter((t) => t);
const isSOURCE = allSites.includes(tags[0]);
const data = await search(
isSOURCE ? tags[0] : "safe",
isSOURCE ? tags.slice(1) : tags,
{ limit, page, showUnavailable: true },
).catch((err) => {
console.log(err);
return null;
});
if (!data || data.posts.length === 0) {
return tg
.answerInlineQuery(inlineQuery, [], { cacheTime })
.catch((err) => console.log(err));
}
const caches = await storage.postCache.get(
data.booru.domain,
data.posts.map((p) => Number(p.id)),
);
const result = await generateResults(
{ domain: data.booru.domain, aliases: data.booru.site.aliases[0] },
data.posts,
caches,
manager,
);
await tg
.answerInlineQuery(inlineQuery, result, {
cacheTime,
nextOffset:
limit === data.posts.length ? (page + 1).toString(16) : undefined,
})
.catch((err) => console.log(err));
});
tg.start({
botToken: process.env.TG_TOKEN,
}).then((self) => console.log(`✨ зашел в ${self.displayName}`));
import http from "node:http";
http
.createServer((req, res) => {
res.write("1");
res.end();
})
.listen(process.env.PORT || 3000, () => console.log("Сервер всети"));