-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
156 lines (140 loc) · 4.63 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { Client, User } from "@mtkruto/mtkruto";
import { getUsername } from "./util.ts";
import env from "./env.ts";
const kv = await Deno.openKv(env.KV_PATH == "" ? undefined : env.KV_PATH);
const client = new Client();
await client.importAuthString(env.AUTH_STRING);
const startTime = Date.now();
let whispersMade = 0;
const SHRUG = "¯\\_(ツ)_/¯";
const DEV = Deno.env.get("DEV") !== undefined;
function nextId() {
return Array.from(crypto.getRandomValues(new Uint8Array(15))).map((v) =>
v.toString(16).toUpperCase().padStart(2, "0")
).join("");
}
client.on("inlineQuery", async (ctx) => {
let { query } = ctx.inlineQuery;
query = query.trim();
const username = query.split(/\s/).slice(-1)[0].toLowerCase();
if (!username.startsWith("@")) {
await ctx.answerInlineQuery([{
id: crypto.randomUUID(),
type: "article",
title: "No Username Provided",
description: "Write someone\u2019s username at the end of your message.",
messageContent: { type: "text", text: SHRUG },
}], { isPersonal: false, cacheTime: DEV ? 0 : 3600 }); // none : 1 hour
return;
}
let wasId = false;
try {
const withoutAt = username.slice(1);
if (
!/^[0-9]+$/.test(withoutAt) ||
String(withoutAt) != String(Number(withoutAt)) &&
Number(withoutAt) >= 1
) {
getUsername(username);
} else {
wasId = true;
}
} catch {
await ctx.answerInlineQuery([{
id: crypto.randomUUID(),
type: "article",
title: "Invalid Username",
description: "The username you provided is invalid.",
messageContent: { type: "text", text: SHRUG },
}], { isPersonal: false, cacheTime: DEV ? 0 : 3600 });
return;
}
const whisper = query.slice(0, username.length * -1).trim();
if (whisper.length == 0 || whisper.length > 200) {
await ctx.answerInlineQuery([{
id: crypto.randomUUID(),
type: "article",
title: "Invalid Whisper Text",
description: `The whisper text is too ${
whisper.length == 0 ? "short" : "long"
}.`,
messageContent: { type: "text", text: SHRUG },
}], { isPersonal: false, cacheTime: DEV ? 0 : 3600 });
return;
}
const id = nextId();
await kv.set(["whispers", id], {
username,
whisper,
date: new Date(),
from: ctx.inlineQuery.from,
});
++whispersMade;
const target = wasId ? `user with the ID ${username.slice(1)}` : username;
await ctx.answerInlineQuery([{
id: crypto.randomUUID(),
type: "article",
title: `Whisper to ${target}`,
description: whisper,
messageContent: {
type: "text",
text: `Whisper to ${target}`,
},
replyMarkup: { inlineKeyboard: [[{ text: "View", callbackData: id }]] },
}], { isPersonal: true, cacheTime: DEV ? 0 : 3600 });
});
client.on("callbackQuery", async (ctx) => {
if (!ctx.callbackQuery.inlineMessageId || !ctx.callbackQuery.data) {
return;
}
const { value } = await kv.get<
{ whisper: string; username: string; from?: User }
>([
"whispers",
ctx.callbackQuery.data,
]);
if (value != null) {
let { whisper, username } = value;
username = username.toLowerCase().slice(1);
const id = Number(username) || 0;
const accesptableUsernames = [
username,
value.from?.username,
...(value.from?.also ?? []),
].filter((v): v is NonNullable<typeof v> => !!v).map((v) =>
v.toLowerCase()
);
const usernameAcceptable = ctx.from.id == id || (ctx.from.username &&
accesptableUsernames.includes(ctx.from.username.toLowerCase())) ||
ctx.from.also?.map((v) => v.toLowerCase()).some((v) =>
accesptableUsernames.includes(v)
);
const willBeRead = ctx.from.id == id ||
ctx.from.username?.toLowerCase() === username ||
ctx.from.also?.map((v) => v.toLowerCase()).some((v) => v == username);
if (!usernameAcceptable) {
await ctx.answerCallbackQuery({
text: "This is not for you.",
alert: true,
});
} else {
await ctx.answerCallbackQuery({ text: whisper, alert: true });
if (willBeRead) {
const target = id ? `user with the ID ${id}` : "@" + username;
const text = `Whisper to ${target}`;
await ctx.editInlineMessageText(text, {
entities: [{ type: "strikethrough", offset: 0, length: text.length }],
});
}
}
}
});
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) / 1_000 / 60 / 60
}h\nMemory used: ${memoryUsed} MB\nWhispers made: ~${whispersMade}`,
);
});
await client.start();