From ed4836a0fd0907eb514691cef8b6effee8101ba2 Mon Sep 17 00:00:00 2001 From: Histmy Date: Sat, 5 Oct 2024 18:34:16 +0200 Subject: [PATCH] =?UTF-8?q?novej=20(do=C4=8Dasnej)=20logging=20status?= =?UTF-8?q?=C5=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 4 +- package.json | 2 +- src/app.ts | 19 +++++---- src/utils/statuslog.ts | 92 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 11 deletions(-) create mode 100644 src/utils/statuslog.ts diff --git a/package-lock.json b/package-lock.json index a39b33d..278b9d0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "denim_3001", - "version": "3001.60.2", + "version": "3001.60.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "denim_3001", - "version": "3001.60.2", + "version": "3001.60.3", "license": "ISC", "dependencies": { "@discordjs/voice": "^0.17.0", diff --git a/package.json b/package.json index 8051ae6..8b57fbe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "denim_3001", - "version": "3001.60.2", + "version": "3001.60.3", "description": "Toto je velmi kvalitní bot.", "repository": { "url": "https://github.com/Histmy/Denim-Bot/" diff --git a/src/app.ts b/src/app.ts index a7b9b2a..ff38e8f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,10 +1,11 @@ import { ButtonStyle, ChannelType, Client, CommandInteraction, GatewayIntentBits, InteractionReplyOptions, Message, Partials } from "discord.js"; import { readdirSync } from "fs"; -import { CClient, CUser, HelpServer, Komand, KomandNaExport, KomandRaw, ListenerFunkce, Modul, SRecord, SuperListenerFunkce } from "./utils/types"; +import { CClient, CUser, HelpServer, Komand, KomandNaExport, KomandRaw, ListenerFunkce, Modul, SMessage, SRecord, SuperListenerFunkce } from "./utils/types"; import { adminLog, areStatusesSame, formatCas, log, nabidni, oddiakritikovat, prefix, rand, send } from "./utils/utils.js"; import levenshtein from "js-levenshtein"; import { emouty } from "./utils/emotes"; import { lidiCoMajDenimPremium, setClient } from "./utils/denim-Spravce"; +import { logOOoZS, logSZS, logPZS } from "./utils/statuslog"; const ints = GatewayIntentBits; const client = new Client({ @@ -223,7 +224,7 @@ function renderMessage(expression: string, args: string[]) { } } -function handle(e: unknown, mesOrInt: Message | CommandInteraction) { +function handle(e: unknown, mesOrInt: SMessage | CommandInteraction) { if (process.env.dieOnError) throw e; log("error pri spousteni akce", e as Error); const admin = process.env.adminID; @@ -233,9 +234,9 @@ function handle(e: unknown, mesOrInt: Message | CommandInteraction) { mesOrInt.reply(txt); } -async function runKomand(mes: Message, cmd: Komand, cmdName: string, arg: string): Promise; +async function runKomand(mes: SMessage, cmd: Komand, cmdName: string, arg: string): Promise; async function runKomand(interaction: CommandInteraction, cmd: Komand, cmdName: string): Promise; -async function runKomand(mesOrInt: Message | CommandInteraction, cmd: Komand, cmdName: string, arg?: string) { +async function runKomand(mesOrInt: SMessage | CommandInteraction, cmd: Komand, cmdName: string, arg?: string) { const jeMes = mesOrInt instanceof Message; if (jeMes && cmd.slashRun) { @@ -250,7 +251,7 @@ async function runKomand(mesOrInt: Message | CommandInteraction, cmd: Komand, cm } if (cmd.premium && !lidiCoMajDenimPremium.includes(jeMes ? mesOrInt.author.id : mesOrInt.member?.user.id || "")) { - return send(mesOrInt as Message, "sorka bracho tuto je koamnd jenom pro curaki co platy"); + return send(mesOrInt as SMessage, "sorka bracho tuto je koamnd jenom pro curaki co platy"); } const akce = cmd.run ?? cmd.slashRun!; @@ -292,7 +293,6 @@ client.on("messageCreate", async mes => { } const komandBez = oddiakritikovat(komandSDiakritikou).toLowerCase(); - // const cmdName = client.aliasy[komandBez] ?? komandBez; let cmdName = komandBez; const aliasCelej = client.aliasy[komandBez]; @@ -349,16 +349,19 @@ client.on("presenceUpdate", async (bef, aft) => { if (!user) return; if (!user.presence) user.presence = {}; + logOOoZS(user.id, aft.clientStatus); + if (areStatusesSame(aft.clientStatus, user.presence)) return; + logSZS(user.id, aft.clientStatus); + const naCo = aft.clientStatus || {}; user.presence = naCo; await new Promise(r => setTimeout(r, 1000)); if (!areStatusesSame(naCo, user.presence)) { - log("status se rychle nezmenil pro ", user.username, ": ", naCo, " -> ", user.presence); - adminLog(client, "stalo se to"); + logPZS(user.id, naCo, user.presence); return; } diff --git a/src/utils/statuslog.ts b/src/utils/statuslog.ts new file mode 100644 index 0000000..3cec61c --- /dev/null +++ b/src/utils/statuslog.ts @@ -0,0 +1,92 @@ +import { ClientPresenceStatusData } from "discord.js"; +import { appendFileSync } from "fs"; +import { SRecord } from "./types"; + +const cesta = "statlog.dlog"; + +appendFileSync(cesta, "\n\n\nDLOG3.0\n"); + +let aktualniOffset: number; +const lidiVDLOG3 = new Map(); + +function updateOffset() { + aktualniOffset = Date.now(); + appendFileSync(cesta, `T${aktualniOffset.toString(36)}\n`); +} +setInterval(updateOffset, 27 * 60 * 1000); +updateOffset(); + +export function getDLOGTimestamp() { + return (Date.now() - aktualniOffset).toString(36); +} + +export function getIdInDLOG3(id: string) { + if (lidiVDLOG3.has(id)) // Pokud už je v mapě + return lidiVDLOG3.get(id); + + // Pokud není v mapě vygenerujeme nové ID a uložíme do mapy + let letters = ''; + let num = lidiVDLOG3.size + 1; + while (num > 0) { + num--; // Adjust for 0-indexing + // Get the remainder and convert to a character + letters = String.fromCharCode((num % 26) + 65) + letters; + // Divide by 26 for the next iteration + num = Math.floor(num / 26); + } + + lidiVDLOG3.set(id, letters); + + return `${letters}${id}`; +} + +const platformTable: SRecord = { + desktop: "D", + web: "W", + mobile: "M" +}; + +const statusTable: SRecord = { + online: 1, + idle: 2, + dnd: 3 +}; + +export function statusToDLOG3(status: ClientPresenceStatusData | null) { + if (!status) return "-"; + + let tovje = []; + + for (const [key, value] of Object.entries(status)) { + const platform = platformTable[key] ?? key; + const status = statusTable[value] ?? 0; + if (status) tovje.push(`${platform} ${status}`); + } + + if (!tovje.length) return "-"; + + return tovje.join(" "); +} + +// Logování Obecného oznámení o změně statusu +export function logOOoZS(userId: string, status: ClientPresenceStatusData | null) { + const timestamp = getDLOGTimestamp(); + const id = getIdInDLOG3(userId); + const stat = statusToDLOG3(status); + appendFileSync(cesta, `${timestamp}${id}o${stat}\n`); +} + +// Logování Skutečné změny statusu +export function logSZS(userId: string, status: ClientPresenceStatusData | null) { + const id = getIdInDLOG3(userId); + const stat = statusToDLOG3(status); + appendFileSync(cesta, `${id}s${stat}\n`); +} + +// Logování Podezřelé změny statusu +export function logPZS(userId: string, status1: ClientPresenceStatusData | null, status2: ClientPresenceStatusData | null) { + const id = getIdInDLOG3(userId); + const stat1 = statusToDLOG3(status1); + const stat2 = statusToDLOG3(status2); + appendFileSync(cesta, `${id}p${stat1}|${stat2}\n`); +}