Skip to content

Commit 3de1c63

Browse files
committed
Handle rate limits better
1 parent e2825df commit 3de1c63

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

index.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,21 @@ const User = new Steam({
4545
dataDirectory: "SteamData",
4646
});
4747

48+
let playingOnOtherSession = false;
49+
let currentNotification;
50+
let authenticated = false;
51+
let MIN_REQUEST_TIME = 60 * 1000;
52+
let LOG_ON_INTERVAL = 10 * 60 * 1000;
53+
let REFRESH_GAMES_INTERVAL = 5 * 60 * 1000;
54+
let lastGameRefreshTime = new Date(0);
55+
let lastLogOnTime = new Date(0);
56+
let onlyLogInAfter = new Date(0);
57+
4858
const logOn = () => {
59+
if (authenticated) return;
60+
if (Date.now() - lastLogOnTime <= MIN_REQUEST_TIME) return;
61+
if (Date.now() < onlyLogInAfter) return;
62+
console.log("Logging in...");
4963
User.logOn({
5064
accountName: USERNAME,
5165
password: PASSWORD,
@@ -56,22 +70,24 @@ const logOn = () => {
5670
: undefined,
5771
autoRelogin: true,
5872
});
73+
lastLogOnTime = Date.now();
5974
};
6075

6176
const panic = (message = "Exiting...") => {
6277
console.error(message);
6378
process.exit(1);
6479
};
6580

66-
let playingOnOtherSession = false;
67-
let currentNotification;
6881
const refreshGames = () => {
82+
if (!authenticated) return;
6983
let notification;
7084
if (playingOnOtherSession) {
7185
notification = "Farming is paused.";
7286
} else {
87+
if (Date.now() - lastGameRefreshTime <= MIN_REQUEST_TIME) return;
7388
User.gamesPlayed(SHOULD_PLAY);
7489
notification = "Farming...";
90+
lastGameRefreshTime = Date.now();
7591
}
7692
if (currentNotification !== notification) {
7793
currentNotification = notification;
@@ -95,22 +111,36 @@ User.on("playingState", (blocked, app) => {
95111
});
96112

97113
User.on("loggedOn", () => {
114+
authenticated = true;
98115
console.log(`Successfully logged in to Steam with ID ${User.steamID}`);
99116
if (PERSONA !== undefined) User.setPersona(PERSONA);
100-
101-
// Allow time to receive the playingState event to see if playing on another session if that's the case
102-
setTimeout(refreshGames, 5000);
117+
refreshGames();
103118
});
104119

105120
User.on("error", (e) => {
106-
if (e.eresult === Steam.EResult.LoggedInElsewhere) {
107-
console.log("Got kicked by other Steam session. Logging in again...");
108-
logOn();
109-
return;
121+
switch (e.eresult) {
122+
case Steam.EResult.LoggedInElsewhere: {
123+
authenticated = false;
124+
console.log(
125+
"Got kicked by other Steam session. Will log in shortly..."
126+
);
127+
logOn();
128+
return;
129+
}
130+
case Steam.EResult.RateLimitExceeded: {
131+
authenticated = false;
132+
onlyLogInAfter = Date.now() + 31 * 60 * 1000;
133+
console.log(
134+
"Got rate limited by Steam. Will try logging in again in 30 minutes."
135+
);
136+
return;
137+
}
138+
default: {
139+
panic(`Got an error from Steam: "${e.message}".`);
140+
}
110141
}
111-
panic(`Got an error from Steam: "${e.message}".`);
112142
});
113143

114144
logOn();
115-
116-
setInterval(refreshGames, 20 * 1000);
145+
setInterval(logOn, LOG_ON_INTERVAL);
146+
setInterval(refreshGames, REFRESH_GAMES_INTERVAL);

0 commit comments

Comments
 (0)