Skip to content

Commit

Permalink
Improved look of stdout by using my logging lib
Browse files Browse the repository at this point in the history
  • Loading branch information
3urobeat committed Jul 17, 2022
1 parent 480ca90 commit 23466c7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
steam-idler.code-workspace
output.txt
46 changes: 27 additions & 19 deletions idler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@ const fs = require("fs");
const SteamUser = require('steam-user');
const SteamTotp = require("steam-totp");
const SteamID = require('steamid');
const logger = require("output-logger");

const config = require("./config.json");

var nextacc = 0
var relogQueue = []; //queue tracking disconnected accounts to relog them after eachother with a delay

//Configure my logging lib
logger.options({
msgstructure: `[${logger.Const.ANIMATION}] [${logger.Const.DATE} | ${logger.Const.TYPE}] ${logger.Const.MESSAGE}`,
paramstructure: [logger.Const.TYPE, logger.Const.MESSAGE, "nodate", "remove", logger.Const.ANIMATION],
outputfile: "./output.txt",
exitmessage: "Goodbye!"
})


/* ------------ Functions: ------------ */
/**
* Imports logininformation from accounts.txt
* @returns logininfo object
*/
function importLogininfo(callback) {
console.log("")
console.log("Loading logininfo from accounts.txt...")
logger("info", "Loading logininfo from accounts.txt...")

var logininfo = {}

Expand All @@ -27,7 +35,7 @@ function importLogininfo(callback) {
if (data[0].startsWith("//Comment")) data = data.slice(1); //Remove comment from array

if (data == "") {
console.log("No accounts found in accounts.txt! Aborting...")
logger("error", "No accounts found in accounts.txt! Aborting...")
process.exit(1)
}

Expand All @@ -40,7 +48,7 @@ function importLogininfo(callback) {
if (i == data.length - 1) callback(logininfo) //callback finished obj on last iteration
})
} else {
console.log("No accounts found in accounts.txt! Aborting...")
logger("error", "No accounts found in accounts.txt! Aborting...")
process.exit(1)
}
}
Expand All @@ -53,19 +61,18 @@ function importLogininfo(callback) {
function loginAcc(logOnOptions, index) {
let bot = new SteamUser({ autoRelogin: false });

console.log(`Logging in ${logOnOptions.accountName}...`)

bot.logOn(logOnOptions) //log in with logOnOptions
logger("info", `Logging in ${logOnOptions.accountName} in ${config.loginDelay / 1000} seconds...`)
setTimeout(() => bot.logOn(logOnOptions), config.loginDelay); //log in with logOnOptions

//Attach event listeners
bot.on('loggedOn', () => { //this account is now logged on
console.log(`[${logOnOptions.accountName}] Logged in and idling games.`)
console.log("")
logger("info", `[${logOnOptions.accountName}] Logged in and idling games.\n`)

nextacc = index + 1 //the next index can start

//If this is a relog then remove this account from the queue and let the next account be able to relog
if (relogQueue.includes(index)) {
console.log(`[${logOnOptions.accountName}] Relog successful.`)
logger("info", `[${logOnOptions.accountName}] Relog successful.`)

relogQueue.splice(relogQueue.indexOf(index), 1) //remove this loginindex from the queue
}
Expand All @@ -77,10 +84,10 @@ function loginAcc(logOnOptions, index) {
bot.on('friendMessage', (steamID, message) => {
var steamID64 = new SteamID(String(steamID)).getSteamID64()

console.log(`[${logOnOptions.accountName}] Friend message from ${steamID64}: ${message}`)
logger("info", `[${logOnOptions.accountName}] Friend message from ${steamID64}: ${message}`)

if (config.afkMessage.length > 0) {
console.log("Responding with: " + config.afkMessage)
logger("info", "Responding with: " + config.afkMessage)

bot.chat.sendFriendMessage(steamID, config.afkMessage)
}
Expand All @@ -89,7 +96,7 @@ function loginAcc(logOnOptions, index) {
bot.on("disconnected", (eresult, msg) => { //handle relogging
if (relogQueue.includes(index)) return; //don't handle this event if account is already waiting for relog

console.log(`[${logOnOptions.accountName}] Lost connection to Steam. Message: ${msg}. Trying to relog in ${config.relogDelay / 1000} seconds...`);
logger("info", `[${logOnOptions.accountName}] Lost connection to Steam. Message: ${msg}. Trying to relog in ${config.relogDelay / 1000} seconds...`);
relogQueue.push(index);

//Check if it's our turn to relog every 1 sec after waiting relogDelay ms
Expand All @@ -100,7 +107,7 @@ function loginAcc(logOnOptions, index) {
clearInterval(relogInterval) //prevent any retries
bot.logOff()

console.log(`[${logOnOptions.accountName}] It is now my turn. Relogging in ${config.loginDelay / 1000} seconds...`)
logger("info", `[${logOnOptions.accountName}] It is now my turn. Relogging in ${config.loginDelay / 1000} seconds...`)

//Generate steam guard code again if user provided a shared_secret
if (logOnOptions["sharedSecretForRelog"]) {
Expand All @@ -109,7 +116,7 @@ function loginAcc(logOnOptions, index) {

//Attach relogdelay timeout
setTimeout(() => {
console.log(`[${logOnOptions.accountName}] Logging in...`)
logger("info", `[${logOnOptions.accountName}] Logging in...`)

bot.logOn(logOnOptions)
}, config.loginDelay);
Expand All @@ -122,8 +129,11 @@ function loginAcc(logOnOptions, index) {


/* ------------ Start all accounts: ------------ */
logger("", "", true, true)
logger("info", "Simple steam-idler by 3urobeat v1.1\n")

importLogininfo((logininfo) => {
console.log("\nSimple steam-idler by 3urobeat v1.1\n")
logger("", "", true)

Object.values(logininfo).forEach((e, i) => {
setTimeout(() => {
Expand All @@ -147,8 +157,6 @@ importLogininfo((logininfo) => {
loginAcc(logOnOptions, i)
}
}, 250)

}, config.loginDelay);

}, 1000);
})
})
39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Simple cross-platform Steam game idler.",
"main": "idler.js",
"dependencies": {
"output-logger": "^2.2.2",
"steam-totp": "^2.1.2",
"steam-user": "^4.24.5"
},
Expand Down

0 comments on commit 23466c7

Please sign in to comment.