-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
76 lines (68 loc) · 2.29 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
import { Client, getColorFromPeerId, getColorName } from "@mtkruto/mtkruto";
import {HOUR} from '@std/datetime/constants'
import env from "./env.ts";
import { getColorCodes } from "./util.ts";
const client = new Client({ authString: env.AUTH_STRING });
const startTime = Date.now();
let requestsProcessed = 0;
function n(color: number) {
const name = getColorName(color) as string;
return name[0].toUpperCase() + name.slice(1);
}
function c(color: number) {
let name = getColorName(color) as string;
name = name[0].toUpperCase() + name.slice(1);
return `${name} ${
getColorCodes(color).map((v) => `<code>${v.toUpperCase()}</code>`).join(
", ",
)
}`;
}
client.use(async (ctx, next) => {
await next();
if (ctx.inlineQuery !== undefined) {
requestsProcessed++;
}
});
function getUrl(
chosen: ReturnType<typeof getColorCodes>,
original: ReturnType<typeof getColorCodes>,
) {
const url = new URL("https://namecolor.deno.dev/g");
url.searchParams.set("data", JSON.stringify([chosen, original]));
return url.toString();
}
client.on("inlineQuery", (ctx) => {
if (ctx.inlineQuery.query.trim().length != 0) {
return Promise.resolve();
}
const color = ctx.from.color;
const idColor = getColorFromPeerId(ctx.from.id);
const url = getUrl(getColorCodes(color), getColorCodes(idColor));
console.log(JSON.stringify({ ...ctx.from, idColor }));
console.log(url);
return ctx.answerInlineQuery([{
id: crypto.randomUUID(),
type: "photo",
url,
thumbnailUrl: url,
title: "My colors",
width: 768,
height: 468,
caption: `My chosen name color: ${c(color)}\nMy original name color: ${
c(idColor)
}\n\n<i>Original name colors are constant colors that depend on the user’s ID. They were used by clients before users were able to choose name colors.</i>`,
description: `${n(color)}, ${n(idColor)}`,
parseMode: "HTML",
}], { cacheTime: 5, isPersonal: true });
});
client.command("stats").filter((ctx) => ctx.chat.id == env.OWNER_ID, (ctx) => {
const memoryUsed = Math.ceil(Deno.memoryUsage().rss / 1024 / 1024);
return ctx.reply(
`Uptime: ${
(Date.now() - startTime) / HOUR
}h\nMemory used: ${memoryUsed} MB\nRequests processed: ${requestsProcessed}`,
);
});
await client.start();
await client.sendMessage(env.OWNER_ID, "Up.");