From 01f42667c8a83cc9eb9d9688f0a408880e644568 Mon Sep 17 00:00:00 2001 From: Sosokker Date: Fri, 29 Aug 2025 01:14:47 +0700 Subject: [PATCH] refactor: update findRecentDeafen function to handle mute events and localize poll messages --- src/index.ts | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/index.ts b/src/index.ts index e47f4ab..d3c1c01 100644 --- a/src/index.ts +++ b/src/index.ts @@ -69,20 +69,29 @@ async function interactionCallback(id: string, token: string, body: any) { }); } -/** Find most recent MEMBER_UPDATE with deaf=true within window */ -function findRecentDeafen(audit: any): { entry: any } | null { +/** Find most recent MEMBER_UPDATE with deaf=true or mute=true within window */ +function findRecentVoicePunish( + audit: any +): { entry: any; kind: string } | null { const entries = audit?.audit_log_entries ?? []; console.log(`[AUDIT] Fetched ${entries.length} entries`); for (const e of entries) { if (e.action_type !== 24) continue; // MEMBER_UPDATE const changes = e.changes || []; const deafChange = changes.find((c: any) => c.key === "deaf"); - if (!deafChange || deafChange.new_value !== true) continue; + const muteChange = changes.find((c: any) => c.key === "mute"); + + let kind = ""; + if (deafChange?.new_value === true) kind = "deafen"; + if (muteChange?.new_value === true) kind = "mute"; + + if (!kind) continue; + const ageSec = (Date.now() - snowflakeToMs(e.id)) / 1000; console.log( - `[AUDIT] Found deafen event: executor=${e.user_id}, target=${e.target_id}, age=${ageSec}s` + `[AUDIT] Found ${kind} event: executor=${e.user_id}, target=${e.target_id}, age=${ageSec}s` ); - if (ageSec <= MAX_EVENT_AGE_SEC) return { entry: e }; + if (ageSec <= MAX_EVENT_AGE_SEC) return { entry: e, kind }; } return null; } @@ -92,11 +101,11 @@ async function createPoll(channelId: string, content: string) { const body = { content, poll: { - question: { text: "Choose punishment" }, + question: { text: "ลงโทษ(ไอเนก)ยังไงดี?" }, answers: [ - { text: "Change role" }, - { text: "Change nickname" }, - { text: "Do nothing" }, + { text: "เปลี่ยน Role" }, + { text: "เปลี่ยนชื่อเล่น" }, + { text: "ไม่ทำอะไร" }, ], duration: 3600, }, @@ -113,7 +122,7 @@ async function createPoll(channelId: string, content: string) { method: "POST", body: JSON.stringify({ content: - "Poll unavailable. React with: 🧩 = Change role, ✏️ = Change nickname, ✅ = Do nothing.", + "ไม่สามารถสร้างโพลได้ ให้กดอิโมจิแทน: 🧩 = เปลี่ยน Role, ✏️ = เปลี่ยนชื่อเล่น, ✅ = ไม่ทำอะไร", }), }); } @@ -143,7 +152,7 @@ app.post("/admin/register", async (c) => { console.log(`[ADMIN] Registering /checkaudit for guild ${guildId}`); const body = { name: "checkaudit", - description: "Scan recent audit logs for deafen events", + description: "ตรวจสอบ audit log หา mute/deafen", type: 1, }; const r = await discordFetch( @@ -190,7 +199,7 @@ app.post("/interactions", async (c) => { // Immediate ephemeral ack await interactionCallback(id, token, { type: 4, - data: { content: "Scanning audit logs…", flags: 64 }, + data: { content: "กำลังตรวจสอบ... (น่าจะไอเนก)", flags: 64 }, // ephemeral ภาษาไทย }); // Background work @@ -201,32 +210,33 @@ app.post("/interactions", async (c) => { ); if (!logsResp.ok) { console.error("[WORK] Failed to fetch audit logs", logsResp.status); - await sendMessage(channelId, "Failed to read audit logs."); + await sendMessage(channelId, "ไม่สามารถอ่าน audit logs ได้"); return; } const logs = await logsResp.json(); - const hit = findRecentDeafen(logs); + const hit = findRecentVoicePunish(logs); if (!hit) { - console.log("[WORK] No deafen event found in recent logs"); - await sendMessage(channelId, "No recent deafen event found."); + console.log("[WORK] No mute/deafen event found"); + await sendMessage(channelId, "ไม่เจอคน mute/deafen เลยนะ (ล่าสุด)"); return; } const e = hit.entry; const executorId = e.user_id; const targetId = e.target_id; + const kind = hit.kind === "deafen" ? "ปิดหู" : "ปิดไมค์"; console.log( - `[WORK] Deafen event confirmed. Executor=${executorId}, Target=${targetId}` + `[WORK] ${kind} event confirmed. Executor=${executorId}, Target=${targetId}` ); await createPoll( channelId, - `Detected deafen event. Executor: <@${executorId}> → Target: <@${targetId}>` + `มีคนแอบเนียน! ${kind}. คนทำ: <@${executorId}> → คนโดน: <@${targetId}>` ); - await sendMessage(channelId, "Justice incoming."); + await sendMessage(channelId, "โดน!"); }); return c.body(null, 204);