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 { send, stackTrace, wait } from "../utils/utils";
import { send, messageLinks, wait } from "../utils/utils";
const maxRecurseLength = 10;
function najit(id: string, lvl = 0) {
if (!stackTrace.has(id) || lvl >= maxRecurseLength) return lvl;
function getDepth(id: string, lvl = 0) {
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) {
if (!stackTrace.has(id)) return;
const kam = messageLinks.get(id);
if (!kam) return;
const kam = stackTrace.get(id)!;
stackTrace.delete(id);
messageLinks.delete(id);
return clear(kam);
}
@ -21,9 +24,9 @@ function clear(id: string) {
// Garbage collector
const garbageCollectDelay = 60_000;
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)
stackTrace.delete(klic);
messageLinks.delete(klic);
}
}, garbageCollectDelay);
@ -33,18 +36,17 @@ const exp: Modul = {
fun: async mes => {
if (mes.author.id != mes.client.user.id) return false;
await wait(mes.content);
console.log(mes.content, najit(mes.id), mes.id, stackTrace);
await wait(mes);
if (najit(mes.id) == maxRecurseLength) {
if (getDepth(mes.id) == maxRecurseLength) {
clear(mes.id);
send(mes, "nemam rat sarandu");
return true;
};
}
return false;
}
},
}
};
module.exports = exp;

View File

@ -93,7 +93,7 @@ export function log(...content: unknown[]) {
});
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) {
@ -198,24 +198,24 @@ export const safeQuery = <T>(query: string, parametry?: string[]) => new Promise
});
});
export const stackTrace = new Map<string, string>();
const nezarazeny = new Map<string, Promise<void>>();
export const messageLinks = new Map<string, string>();
export async function wait(kontent: string) {
if (nezarazeny.has(kontent))
await nezarazeny.get(kontent);
/**
* Resolvne zpráva s daným ID bude zařazena do messageLinks
* @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) {
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);
stackTrace.set(nova.id, mes.id);
nezarazeny.delete(klic);
res();
messageLinks.set(nova.id, mes.id);
return nova;
}