-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
74 lines (61 loc) · 2.33 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
import { Bot, InlineKeyboard } from "https://deno.land/x/[email protected]/mod.ts";
const BOT_TOKEN = Deno.env.get("BOT_TOKEN");
if (!BOT_TOKEN) throw new Error("No BOT_TOKEN env var found");
const bot = new Bot(BOT_TOKEN);
const SLOT_EMOJIS = ["🔤", "🫐", "🍋", "7️⃣"];
function resolveDice(value: number) {
return [0, 1, 2].map((i) => ((value - 1) >> (i * 2)) & 0x03);
}
bot.on("chat_member", (ctx) => {
const { old_chat_member, new_chat_member: member, from } = ctx.chatMember;
if (from.is_bot) return;
if (member.status !== "member") return;
if (
old_chat_member.status !== "left" &&
old_chat_member.status !== "kicked"
) return;
});
bot.command("captcha", async (ctx) => {
const { dice: { value }, message_id } = await ctx.replyWithDice("🎰");
const optionValues = [value];
while (optionValues.length < 4) {
const randomValue = Math.floor(Math.random() * 64) + 1;
if (randomValue !== value) optionValues.push(randomValue);
}
const options = optionValues.sort((v) => v > 32 ? -1 : 1).map((value) =>
resolveDice(value).map((i) => SLOT_EMOJIS[i]).join(" ")
);
const keyboard = new InlineKeyboard();
for (let i = 0; i < options.length; i++) {
keyboard.row().text(options[i], `choose:${i}`);
// if (i !== 0 && i % 2 === 0) keyboard.row();
}
await ctx.reply(`What do you see?`, {
parse_mode: "HTML",
reply_to_message_id: message_id,
reply_markup: keyboard,
});
});
bot.callbackQuery(/choose:(\d)/, async (ctx) => {
if (!ctx.match) return ctx.answerCallbackQuery("Weird 1.");
const [_, index] = ctx.match;
const value = ctx.callbackQuery.message?.reply_to_message?.dice?.value;
if (value === undefined) return ctx.answerCallbackQuery("Weird 2.");
const correct = resolveDice(value).map((i) => SLOT_EMOJIS[i]).join(" ");
const keyboard = ctx.callbackQuery.message?.reply_markup?.inline_keyboard
?.[Number(index)]?.[0];
if (!keyboard) return ctx.answerCallbackQuery("Weird 3.");
await ctx.answerCallbackQuery(
keyboard.text === correct
? "Captcha cleared successfully"
: "You missed it, sorry.",
);
await ctx.deleteMessage();
await ctx.api.deleteMessage(
ctx.chat?.id!,
ctx.callbackQuery.message?.reply_to_message?.message_id!,
);
});
bot.catch(console.error);
bot.start();
// bot.start({ allowed_updates: ["callback_query", "chat_member"] });