diff --git a/Automatization/Verification.ts b/Automatization/Verification.ts index daf52cd..d948ede 100755 --- a/Automatization/Verification.ts +++ b/Automatization/Verification.ts @@ -10,6 +10,7 @@ export async function verification(link: string, hero: Hero) { await hero.goto(link, { timeoutMs: 120_000 }); await hero.waitForLoad("AllContentLoaded", { timeoutMs: 120_000 }); } catch (err) { + console.log(`Account not verificated`); console.log(err) return } diff --git a/Helpers/proccessWorkers.ts b/Helpers/proccessWorkers.ts index 65acf87..2f30aaa 100755 --- a/Helpers/proccessWorkers.ts +++ b/Helpers/proccessWorkers.ts @@ -63,7 +63,7 @@ export async function processWorkers(dockerContainer: wrappedContainer, workerCo } continue mainLoop; } - await sleep(Math.random() * 50_000); + // await sleep(Math.random() * 50_000); if ((await telegramWorker.getMessagesByEntity(botEntity)).length == 0) { // first time activation if (worker.refferal !== null) { diff --git a/Helpers/utils.ts b/Helpers/utils.ts index b51b7e8..0c8802c 100755 --- a/Helpers/utils.ts +++ b/Helpers/utils.ts @@ -85,11 +85,11 @@ export function randomInRange(a: number, b: number) { return Math.floor(Math.random() * (b - a + 1) + b); } export class TimeoutError extends Error {} -export async function timeout(func: Promise,timeoutMS=20_000) { - await Promise.race([ +export async function timeout(func: Promise,timeoutMS=20_000,errorToThrow: Error=new TimeoutError(`Timeout Error after waiting ${timeoutMS}`)) { + return await Promise.race([ new Promise((_, reject) => { setTimeout(() => { - reject(new TimeoutError(`Timeout Error after waiting ${timeoutMS}`)); + reject(errorToThrow); }, timeoutMS); }), func diff --git a/Worker/TelegramWorker.ts b/Worker/TelegramWorker.ts index a1f6e93..832e4fa 100755 --- a/Worker/TelegramWorker.ts +++ b/Worker/TelegramWorker.ts @@ -5,7 +5,7 @@ import { BadRequestError, TimedOutError } from "telegram/errors"; import { IWorker } from "./IWorker"; import { LogLevel } from "telegram/extensions/Logger"; import { settings } from "../Databases/JSONDatabase"; -import { UnknownError } from "../Helpers/utils"; +import { timeout, UnknownError } from "../Helpers/utils"; import { db } from "../Databases/SQLDatabase"; import { sleep, TotalList } from "telegram/Helpers"; @@ -57,29 +57,30 @@ export class TelegramWorker extends telegram.TelegramClient { } - async waitForNewMessages(entity: EntityLike, idOfLastMessage: number, waitFor: number | string = 1, timeoutMS = 20 * 1_000) { - const start_time = (new Date()).getTime(); - if (isString(waitFor)) { - while ((await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).filter((message) => { - return message.message.includes(waitFor) - }).length === 0) { - if (((new Date()).getTime() - start_time) >= timeoutMS) { - throw new NoNewMessagesError("No new messages"); + async waitForNewMessages(entity: EntityLike, idOfLastMessage: number, waitFor: number | string = 1, timeoutMS = 20 * 1_000): Promise { + return await timeout( + (async ()=>{ + if (isString(waitFor)) { + while ((await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).filter((message) => { + return message.message.includes(waitFor) + }).length === 0) { + await sleep(500); + } + return (await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).filter((message) => { + return message.message.includes(waitFor) + }) + } else { + while ((await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).length < waitFor) { + + await sleep(500); + } + return (await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).slice(0, waitFor) } - await sleep(500); - } - return (await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).filter((message) => { - return message.message.includes(waitFor) - }) - } else { - while ((await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).length < waitFor) { - if (((new Date()).getTime() - start_time) >= timeoutMS) { - throw new NoNewMessagesError("No new messages"); - } - await sleep(500); - } - return (await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).slice(0, waitFor) - } + })(), + timeoutMS, + new NoNewMessagesError("No new messages") + ) + } async isBotBanned(entity: Required>) { const blockedUsers = (