From 75d576659c65b61873dec380c17bf8076fa88625 Mon Sep 17 00:00:00 2001 From: 3urobeat <35304405+3urobeat@users.noreply.github.com> Date: Sat, 4 May 2024 15:31:25 +0200 Subject: [PATCH] feat(Bot): Potentially finally fix Already logged on errors when relogging for good --- src/bot/bot.js | 38 ++++++++++++++++++++++++++++++------- src/data/fileStructure.json | 2 +- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/bot/bot.js b/src/bot/bot.js index e2bbdb31..e0669312 100644 --- a/src/bot/bot.js +++ b/src/bot/bot.js @@ -4,7 +4,7 @@ * Created Date: 2021-07-09 16:26:00 * Author: 3urobeat * - * Last Modified: 2024-05-03 12:46:26 + * Last Modified: 2024-05-04 15:27:37 * Modified By: 3urobeat * * Copyright (c) 2021 - 2024 3urobeat @@ -192,10 +192,37 @@ Bot.prototype._loginToSteam = async function() { // Count this attempt this.loginData.logOnTries++; - // Always call logOff() before logOn() like an idiot to prevent "Already attempting to log on, cannot log on again" errors - this.user.logOff(); + const currentLogOnTry = this.loginData.logOnTries; - if (this.sessionHandler.session) this.sessionHandler.session.cancelLoginAttempt(); // TODO: This might cause an error as idk if we are polling. Maybe use the timeout event of steam-session + + // Attach loginTimeout handler + this.handleLoginTimeout(); + + + // If logged in, wait for account to be logged off before continuing. This prevents "Already logged in, ..." & "Already attempting to log on, ..." errors + if (this.user.steamID || this.user._connecting) { // https://github.com/DoctorMcKay/node-steam-user/blob/cb8e098969c10555fe41dd9487be140c79006c41/components/09-logon.js#L51 + await (async () => { + return new Promise((resolve) => { + + this.user.logOff(); + if (this.sessionHandler.session) this.sessionHandler.session.cancelLoginAttempt(); // TODO: This might cause an error as idk if we are polling. Maybe use the timeout event of steam-session + + let logOffInterval = setInterval(() => { + logger("warn", `[${this.logPrefix}] Login requested but account seems to be logged in! Attempting log off before continuing...`, true, true); // Cannot log with date to prevent log output file spam + + // Resolve when logged off or when logOnTries has changed (handleLoginTimeout has probably taken action) + if ((!this.user.steamID && !this.user._connecting) || currentLogOnTry != this.loginData.logOnTries) { + clearInterval(logOffInterval); + resolve(); + } + }, 250); + + }); + })(); + } + + // Cancel if not on the same logOnTries value anymore (handleLoginTimeout has probably taken action) + if (currentLogOnTry != this.loginData.logOnTries) return logger("debug", `[${this.logPrefix}] Aborting login because _loginToSteam() was called again from somewhere else. Potentially logging off took too long and handleLoginTimeout took action.`); // Find proxyIndex from steam-user object options instead of loginData to get reliable log data @@ -205,9 +232,6 @@ Bot.prototype._loginToSteam = async function() { if (!thisProxy.proxy) logger("info", `[${this.logPrefix}] Trying to log in without proxy... (Attempt ${this.loginData.logOnTries}/${this.controller.data.advancedconfig.maxLogOnRetries + 1})`, false, true, logger.animation("loading")); else logger("info", `[${this.logPrefix}] Trying to log in with proxy ${thisProxy.proxyIndex}... (Attempt ${this.loginData.logOnTries}/${this.controller.data.advancedconfig.maxLogOnRetries + 1})`, false, true, logger.animation("loading")); - // Attach loginTimeout handler - this.handleLoginTimeout(); - // Call our steam-session helper to get a valid refresh token for us let refreshToken = await this.sessionHandler.getToken(); diff --git a/src/data/fileStructure.json b/src/data/fileStructure.json index 07061993..13b42d8b 100644 --- a/src/data/fileStructure.json +++ b/src/data/fileStructure.json @@ -318,7 +318,7 @@ { "path": "src/bot/bot.js", "url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/src/bot/bot.js", - "checksum": "97b6b1fb055bd3a19c11bbe68147b8c5" + "checksum": "f5ffdab3eaaaeed3035b76200c932c47" }, { "path": "src/bot/events/debug.js",