Totální vylepšení

This commit is contained in:
Histmy 2024-07-14 15:44:48 +02:00
parent 9c6504e309
commit 82aacde0a7
Signed by: Histmy
GPG Key ID: AC2E43C463D8F329
2 changed files with 29 additions and 27 deletions

View File

@ -1,19 +1,22 @@
// "Ulimatní" kapp
import { Modul } from "../utils/types"; import { Modul } from "../utils/types";
import { send, stackTrace, wait } from "../utils/utils"; import { send, messageLinks, wait } from "../utils/utils";
const maxRecurseLength = 10; const maxRecurseLength = 10;
function najit(id: string, lvl = 0) { function getDepth(id: string, lvl = 0) {
if (!stackTrace.has(id) || lvl >= maxRecurseLength) return lvl; const pre = messageLinks.get(id);
if (!pre || lvl >= maxRecurseLength) return lvl;
return najit(stackTrace.get(id)!, lvl + 1); return getDepth(pre, lvl + 1);
} }
function clear(id: string) { function clear(id: string) {
if (!stackTrace.has(id)) return; const kam = messageLinks.get(id);
if (!kam) return;
const kam = stackTrace.get(id)!; messageLinks.delete(id);
stackTrace.delete(id);
return clear(kam); return clear(kam);
} }
@ -21,9 +24,9 @@ function clear(id: string) {
// Garbage collector // Garbage collector
const garbageCollectDelay = 60_000; const garbageCollectDelay = 60_000;
setInterval(() => { setInterval(() => {
for (const klic of stackTrace.keys()) { for (const klic of messageLinks.keys()) {
if (Number(BigInt.asUintN(64, BigInt(klic)) >> 22n) + 1420070400000 < Date.now() - garbageCollectDelay) if (Number(BigInt.asUintN(64, BigInt(klic)) >> 22n) + 1420070400000 < Date.now() - garbageCollectDelay)
stackTrace.delete(klic); messageLinks.delete(klic);
} }
}, garbageCollectDelay); }, garbageCollectDelay);
@ -33,18 +36,17 @@ const exp: Modul = {
fun: async mes => { fun: async mes => {
if (mes.author.id != mes.client.user.id) return false; if (mes.author.id != mes.client.user.id) return false;
await wait(mes.content); await wait(mes);
console.log(mes.content, najit(mes.id), mes.id, stackTrace);
if (najit(mes.id) == maxRecurseLength) { if (getDepth(mes.id) == maxRecurseLength) {
clear(mes.id); clear(mes.id);
send(mes, "nemam rat sarandu"); send(mes, "nemam rat sarandu");
return true; return true;
}; }
return false; return false;
} }
}, }
}; };
module.exports = exp; module.exports = exp;

View File

@ -93,7 +93,7 @@ export function log(...content: unknown[]) {
}); });
const d = new Date(); const d = new Date();
console.log(`[${d.getDate()}.${d.getMonth() + 1}. ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}]:`, ...jo); console.log(`[${d.getDate()}.${d.getMonth() + 1}. ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}.${d.getMilliseconds()}]:`, ...jo);
} }
export async function sendDM(user: User, txt: string) { export async function sendDM(user: User, txt: string) {
@ -198,24 +198,24 @@ export const safeQuery = <T>(query: string, parametry?: string[]) => new Promise
}); });
}); });
export const stackTrace = new Map<string, string>(); export const messageLinks = new Map<string, string>();
const nezarazeny = new Map<string, Promise<void>>();
export async function wait(kontent: string) { /**
if (nezarazeny.has(kontent)) * Resolvne zpráva s daným ID bude zařazena do messageLinks
await nezarazeny.get(kontent); * @param mes Zpráva na kterou se počkat
*/
export async function wait(mes: Message) {
while (true) {
if (messageLinks.has(mes.id))
return;
await new Promise(r => setTimeout(r, 0));
}
} }
export async function send(mes: Message, co: string | MessageCreateOptions) { export async function send(mes: Message, co: string | MessageCreateOptions) {
let res = () => { };
const klic = (typeof co == "string" ? co : co.content ?? "").trim();
nezarazeny.set(klic, new Promise<void>(r => res = r));
const nova = await mes.channel.send(co); const nova = await mes.channel.send(co);
stackTrace.set(nova.id, mes.id); messageLinks.set(nova.id, mes.id);
nezarazeny.delete(klic);
res();
return nova; return nova;
} }