Skip to content

Commit

Permalink
Version 2.10.1 (read commit description)
Browse files Browse the repository at this point in the history
Fixed unfriend check logging an steam64id undefined error (#81). Removed unnecessary process event listeners (This could also fix #78). Added check if all friends are in lastcomment database. Fixed resetcooldown not being able to reset global cooldown in one try. Fixed comment log msg displaying even on error when more than 1 comment was requested. Minor other changes.
  • Loading branch information
3urobeat committed Mar 11, 2021
1 parent b6b1a98 commit f2db506
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
32 changes: 22 additions & 10 deletions src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports.run = async (logOnOptions, loginindex) => {
var maxLogOnRetries = 1 //How often a failed logOn will be retried
var logger = controller.logger
var lang = controller.lang
var commentedrecently = false; //global cooldown for the comment command
var commentedrecently = 0; //global cooldown for the comment command
var lastmessage = {}
var lastcommentrequestmsg = []
var failedcomments = []
Expand Down Expand Up @@ -125,13 +125,7 @@ module.exports.run = async (logOnOptions, loginindex) => {
}
}

/* ------------ Login & Events: ------------ */
//Should keep the bot at least from crashing
process.on('unhandledRejection', (reason) => {
logger(`Unhandled Rejection Error! Reason: ${reason.stack}`, true) });
process.on('uncaughtException', (reason) => {
logger(`Uncaught Exception Error! Reason: ${reason.stack}`, true) });

/* ------------ Login & Events: ------------ */
let logOnTries = 0;

function logOnAccount() {
Expand Down Expand Up @@ -260,6 +254,24 @@ module.exports.run = async (logOnOptions, loginindex) => {
if (loginindex == 0) bot.gamesPlayed(config.playinggames); //set game only for the main bot
if (loginindex != 0) bot.gamesPlayed(config.childaccplayinggames) //set games for child accounts that are set in the config

//Check if all friends are in lastcomment database
if (loginindex == 0) {
controller.lastcomment.find({}, (err, docs) => {
Object.keys(bot.myFriends).forEach(e => {

if (bot.myFriends[e] == 3 && !docs.find(el => el.id == e)) {
let lastcommentobj = {
id: e,
time: Date.now() - (config.commentcooldown * 60000) //subtract commentcooldown so that the user is able to use the command instantly
}

controller.lastcomment.insert(lastcommentobj, (err) => { if (err) logger("Error inserting existing user into lastcomment.db database! Error: " + err) })
}
})
})
}


controller.communityobject[loginindex] = community //export this community instance to the communityobject to access it from controller.js
controller.botobject[loginindex] = bot //export this bot instance to the botobject to access it from controller.js

Expand Down Expand Up @@ -293,7 +305,7 @@ module.exports.run = async (logOnOptions, loginindex) => {
controller.lastcomment.remove({ id: Object.keys(bot.myFriends)[i] }, {}, (err) => { if (err) logger("Error removing duplicate steamid from lastcomment.db on offline friend accept! Error: " + err) }) //remove any old entries
controller.lastcomment.insert(lastcommentobj, (err) => { if (err) logger("Error inserting new user into lastcomment.db database! Error: " + err) })

if (configgroup64id.length > 1 && Object.keys(bot.myGroups).includes(configgroup64id)) {
if (configgroup64id && configgroup64id.length > 1 && Object.keys(bot.myGroups).includes(configgroup64id)) {
bot.inviteToGroup(Object.keys(bot.myFriends)[i], new SteamID(configgroup64id));

if (configgroup64id !== "103582791464712227") { //https://steamcommunity.com/groups/3urobeatGroup
Expand Down Expand Up @@ -527,7 +539,7 @@ module.exports.run = async (logOnOptions, loginindex) => {

if (args[0]) {
if (args[0] == "global") { //Check if user wants to reset the global cooldown
commentedrecently -= config.globalcommentcooldown //subtract cooldown on top the stored time
commentedrecently = 0
return chatmsg(steamID, lang.resetcooldowncmdglobalreset)
}

Expand Down
10 changes: 5 additions & 5 deletions src/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module.exports.run = (logger, chatmsg, lang, community, thisbot, steamID, args,
for (let i in controller.botobject) {
if (Number(i) + 1 <= numberofcomments && Number(i) + 1 <= Object.keys(controller.botobject).length) { //only check if this acc is needed for a comment
try {
if (controller.botobject[i].limitations.limited == true && !Object.keys(controller.botobject[i].myFriends).includes(new SteamID(String(steamID)).getSteamID64())) {
if (controller.botobject[i].limitations && controller.botobject[i].limitations.limited == true && !Object.keys(controller.botobject[i].myFriends).includes(new SteamID(String(steamID)).getSteamID64())) {
accstoadd[requesterSteamID].push(`\n 'https://steamcommunity.com/profiles/${new SteamID(String(controller.botobject[i].steamID)).getSteamID64()}'`) }
} catch (err) {
logger("Error checking if comment requester is friend with limited bot accounts: " + err) } } //This error check was implemented as a temporary solution to fix this error (and should be fine since it seems that this error is rare and at least prevents from crashing the bot): https://github.com/HerrEurobeat/steam-comment-service-bot/issues/54
Expand Down Expand Up @@ -177,15 +177,15 @@ module.exports.run = (logger, chatmsg, lang, community, thisbot, steamID, args,
/* --------- Try to comment --------- */

//Function to get random quote that wasn't chosen for a comment that is more recent than 5 comments
function getQuote(callback) {
function getQuote(quotecallback) {
var randomstring = arr => arr[Math.floor(Math.random() * arr.length)]; //smol function to get random string from array
let selection = randomstring(quoteselection); //get random quote for this iteration

if (lastquotes.length > 4) lastquotes.splice(0, 1) //remove first element from array if we have more than 4 in it
if (lastquotes.includes(selection)) getQuote(cb => { callback(cb) }); //call this function again to get a new quote and pass cb to get callback from another execution back to the first one
if (lastquotes.includes(selection)) getQuote(cb => { quotecallback(cb) }); //call this function again to get a new quote and pass cb to get callback from another execution back to the first one
else {
if (quoteselection.length > 5) lastquotes.push(selection) //push this comment to lastquotes array to not get it the next 5 times if the quotes.txt has more than 5 quotes
callback(selection) }
quotecallback(selection) }
}

getQuote(comment => { //get a random quote to comment with and wait for callback to ensure a quote has been found before trying to comment
Expand Down Expand Up @@ -275,7 +275,7 @@ module.exports.run = (logger, chatmsg, lang, community, thisbot, steamID, args,
})

} else { //Stuff below should only run for child accounts
logger(`[${thisbot}] Comment on ${new SteamID(String(steamID)).getSteamID64()}: ${String(comment).split("\n")[0]}`) //splitting \n to only get first line of multi line comments
if (!error) logger(`[${thisbot}] Comment on ${new SteamID(String(steamID)).getSteamID64()}: ${String(comment).split("\n")[0]}`) //splitting \n to only get first line of multi line comments
}


Expand Down
16 changes: 5 additions & 11 deletions src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ var logger = (str, nodate, remove) => { //Custom logger

var steamGuardInputTimeFunc = (arg) => { steamGuardInputTime += arg } //small function to return new value from bot.js

//Should keep the bot at least from crashing
process.on('unhandledRejection', (reason) => {
logger(`Unhandled Rejection Error! Reason: ${reason.stack}`, true) });
process.on('uncaughtException', (reason) => {
logger(`Uncaught Exception Error! Reason: ${reason.stack}`, true) });

//Either use logininfo.json or accounts.txt:
if (fs.existsSync("./accounts.txt")) {
var data = fs.readFileSync("./accounts.txt", "utf8").split("\n")
Expand Down Expand Up @@ -570,17 +564,17 @@ var readyinterval = setInterval(() => { //log startup to console
if (lastcommentUnfriendCheck + 30000 > Date.now()) return; //last check is more recent than 30 seconds

lastcomment.find({ time: { $lte: Date.now() - (config.unfriendtime * 86400000) } }, (err, docs) => { //until is a date in ms, so we check if it is less than right now
if (docs.length < 1) return; //nothing found
lastcommentUnfriendCheck = Date.now()
if (docs.length < 1) return; //nothing found

docs.forEach((e) => { //take action for all results
Object.keys(botobject).forEach((f, j) => {
if (f.myFriends[e.id] == 3) { //check if the targeted user is still friend
if (botobject[f].myFriends[e.id] == 3 && !config.ownerid.includes(e.id)) { //check if the targeted user is still friend
if (j == 0) botobject[0].chat.sendFriendMessage(new SteamID(e.id), `You have been unfriended for being inactive for ${config.unfriendtime} days.\nIf you need me again, feel free to add me again!`)

f.removeFriend(new SteamID(e.id)) //unfriend user with each bot
logger(`Unfriended ${e.id} after ${config.unfriendtime} days of inactivity.`)
}
botobject[f].removeFriend(new SteamID(e.id)) //unfriend user with each bot
logger(`Unfriended ${e.id} after ${config.unfriendtime} days of inactivity.`)
}

if (!config.ownerid.includes(e.id)) lastcomment.remove({ id: e.id }) //entry gets removed no matter what but we are nice and let the owner stay. Thank me later! <3
})
Expand Down
6 changes: 3 additions & 3 deletions src/data.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "2100",
"versionstr": "2.10.0",
"version": "2101",
"versionstr": "2.10.1",
"branch": "master",
"filetostart": "./src/updater.js",
"filetostarturl": "https://raw.githubusercontent.com/HerrEurobeat/steam-comment-service-bot/master/src/updater.js",
Expand All @@ -9,7 +9,7 @@
"aboutstr": "This bot was created by 3urobeat.\nGitHub: https://github.com/HerrEurobeat/steam-comment-service-bot \nSteam: https://steamcommunity.com/id/3urobeat \nIf you like my work, any donation would be appreciated! https://paypal.me/3urobeat",
"firststart": true,
"compatibilityfeaturedone": false,
"whatsnew": "New lastcomment storage system. Added custom language support. Errors won't crash the bot anymore. Added account randomization option and better childacc play status support. Many other changes...",
"whatsnew": "Fixed steam64id undefined error spam caused by unfriend check. Fixed resetcooldown not resetting global cooldown in one try. Added check to add missing users to lastcomment db. Minor other changes listed on GitHub.",
"urlrequestsecretkey": "",
"timesloggedin": 0,
"totallogintime": 0
Expand Down

0 comments on commit f2db506

Please sign in to comment.