Compare commits

...

2 Commits

Author SHA1 Message Date
Lamp
f716bf9cc3 refactor to indicate errors in discord 2022-05-11 14:29:47 -07:00
Lamp
cebe8dbd42 skip odd minutes 2022-05-11 14:10:39 -07:00
3 changed files with 41 additions and 26 deletions

View File

@ -25,34 +25,48 @@ var status2icon = {
} }
var selfUserId; var selfUserId;
vrc.authenticationApi.getCurrentUser().then(res => { client.login(process.env.TOKEN).then(() => {
console.log("vrc login", res.data.username); console.log("discord login", client.user.tag);
selfUserId = res.data.id; }).catch(error => {
exithook(cb => { console.error("discord login fail", error.stack)
console.log("logging out"); });
client.destroy();
vrc.authenticationApi.logout().then(res => {
console.log(res.data);
cb();
});
});
client.login(process.env.TOKEN).then(() => {
console.log("discord login", client.user.tag);
}).catch(error => console.error("discord login fail", error.stack));
startClock();
}).catch(error => console.error("vrc login fail:", JSON.stringify(error.response.data)));
client.once("ready", async () => { client.once("ready", async () => {
var vrcat = client.channels.resolve(VRCAT); var vrcat = client.channels.resolve(VRCAT);
if (!vrcat) return console.error("category not found"); if (!vrcat) return console.error("category not found");
for (let child of vrcat.children.values()) await child.delete(); for (let child of vrcat.children.values()) await child.delete();
exithook(async cb => {
console.log("discord cleanup");
for (let child of vrcat.children.values()) await child.delete();
client.destroy();
console.log("discord destroyed");
cb();
});
try {
var logindata = await vrc.authenticationApi.getCurrentUser().then(res => res.data);
} catch (error) {
console.error("vrc login fail:", JSON.stringify(error.response.data));
await vrcat.createChannel("login failure", {type: "GUILD_VOICE"});
}
if (!logindata) return;
console.log("vrc login", logindata.username);
selfUserId = logindata.id;
exithook(cb => {
console.log("vrc logout");
vrc.authenticationApi.logout().then(res => {
console.log("vrc", res.data);
cb();
});
});
startClock();
do { do {
try { try {
await syncStat(); await syncStat();
} catch (error) { } catch (error) {
console.error(error.stack); console.error(error.stack);
vrcat.createChannel("error occured", {type: "GUILD_VOICE"}).catch(()=>{});
} }
await new Promise(r => setTimeout(r, 1000*60*5)) await new Promise(r => setTimeout(r, 1000*60*5));
} while (true); } while (true);
}); });
@ -103,9 +117,10 @@ global.syncStat = syncStat;
function startClock() { function startClock() {
(function clock() { (function clock() {
var d = new Date(); var d = new Date();
if (!(d.getMinutes() % 2)) // skip odd minutes to keep average request rate under 1 per minute
vrc.usersApi.updateUser(selfUserId, { vrc.usersApi.updateUser(selfUserId, {
statusDescription: d.toLocaleTimeString([], {timeStyle: 'short'}) statusDescription: d.toLocaleTimeString([], {timeStyle: 'short'})
}); }).catch(error => console.error("clock", error.message));
d.setSeconds(60); d.setSeconds(60);
setTimeout(clock, d - Date.now()); setTimeout(clock, d - Date.now());
})(); })();

14
package-lock.json generated
View File

@ -8,7 +8,7 @@
"async-exit-hook": "^2.0.1", "async-exit-hook": "^2.0.1",
"discord.js": "^13.6.0", "discord.js": "^13.6.0",
"dotenv": "^16.0.0", "dotenv": "^16.0.0",
"vrchat": "^1.6.9" "vrchat": "^1.7.1"
} }
}, },
"node_modules/@discordjs/builders": { "node_modules/@discordjs/builders": {
@ -328,9 +328,9 @@
} }
}, },
"node_modules/vrchat": { "node_modules/vrchat": {
"version": "1.6.9", "version": "1.7.1",
"resolved": "https://registry.npmjs.org/vrchat/-/vrchat-1.6.9.tgz", "resolved": "https://registry.npmjs.org/vrchat/-/vrchat-1.7.1.tgz",
"integrity": "sha512-eV3EzHtObBGIzb5tEOP6vqeZsGIlMLXybO2CwpkSDvGra2ORIoh5hYnUq9JeMnAsmwF/xMpPHrb5yg5ZPDhAoA==", "integrity": "sha512-RuJkqEsouqnUDQBmZCwLgXQSSijdR3yaaew8ajM/j+28NAEXHR3uMyrnWPmWDZUPTV9zASlSTpabft1yq84z9Q==",
"dependencies": { "dependencies": {
"@types/tough-cookie": "^4.0.1", "@types/tough-cookie": "^4.0.1",
"axios": "^0.21.4", "axios": "^0.21.4",
@ -601,9 +601,9 @@
"integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==" "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg=="
}, },
"vrchat": { "vrchat": {
"version": "1.6.9", "version": "1.7.1",
"resolved": "https://registry.npmjs.org/vrchat/-/vrchat-1.6.9.tgz", "resolved": "https://registry.npmjs.org/vrchat/-/vrchat-1.7.1.tgz",
"integrity": "sha512-eV3EzHtObBGIzb5tEOP6vqeZsGIlMLXybO2CwpkSDvGra2ORIoh5hYnUq9JeMnAsmwF/xMpPHrb5yg5ZPDhAoA==", "integrity": "sha512-RuJkqEsouqnUDQBmZCwLgXQSSijdR3yaaew8ajM/j+28NAEXHR3uMyrnWPmWDZUPTV9zASlSTpabft1yq84z9Q==",
"requires": { "requires": {
"@types/tough-cookie": "^4.0.1", "@types/tough-cookie": "^4.0.1",
"axios": "^0.21.4", "axios": "^0.21.4",

View File

@ -3,6 +3,6 @@
"async-exit-hook": "^2.0.1", "async-exit-hook": "^2.0.1",
"discord.js": "^13.6.0", "discord.js": "^13.6.0",
"dotenv": "^16.0.0", "dotenv": "^16.0.0",
"vrchat": "^1.6.9" "vrchat": "^1.7.1"
} }
} }