diff options
| author | Christopher Gwynn <[email protected]> | 2021-02-06 05:45:23 -0500 |
|---|---|---|
| committer | Christopher Gwynn <[email protected]> | 2021-02-06 05:45:23 -0500 |
| commit | 9d642618010bf8d0d5ccffb18eaf36f9df45c786 (patch) | |
| tree | 089eb1c43342509f19c263e5dc3e18605e5f5792 | |
| parent | add discord rpc capability (diff) | |
| download | wizard101-rpc-9d642618010bf8d0d5ccffb18eaf36f9df45c786.tar.xz wizard101-rpc-9d642618010bf8d0d5ccffb18eaf36f9df45c786.zip | |
auto reconnect
| -rw-r--r-- | index.js | 51 | ||||
| -rw-r--r-- | package.json | 4 | ||||
| -rw-r--r-- | settings.json | 4 | ||||
| -rw-r--r-- | yarn.lock | 7 |
4 files changed, 50 insertions, 16 deletions
@@ -2,9 +2,9 @@ const fs = require("fs"); const path = require("path"); const readLastLines = require("read-last-lines"); const DiscordRPC = require("discord-rpc"); -const rpc = new DiscordRPC.Client({ transport: "ipc" }); const logPath = "C:\\ProgramData\\KingsIsle Entertainment\\Wizard101\\Bin\\WizardClient.log"; const zones = require(path.join(__dirname, "./zones.json")); +let rpc; console.log(" _ _ __ ___ __ \n (_) | /_ |/ _ \\/_ | \n __ ___ ______ _ _ __ __| || | | | || |______ _ __ _ __ ___ \n \\ \\ /\\ / / |_ / _` | \'__/ _` || | | | || |______| \'__| \'_ \\ / __|\n \\ V V /| |/ / (_| | | | (_| || | |_| || | | | | |_) | (__ \n \\_/\\_/ |_/___\\__,_|_| \\__,_||_|\\___/ |_| |_| | .__/ \\___|\n | | \n "); @@ -15,25 +15,31 @@ fs.watchFile(logPath, { interval: 100 }, async (_curr, _prev) => { let currentLineCount = await countLines(); readLastLines.read(logPath, currentLineCount - oldLineCount).then(async (logChunk) => { // Get zone - try { - const zoneRegex = new RegExp(/zone = (.+),|CHARACTER LIST/gm); - let tempZone = logChunk.match(zoneRegex); - if(tempZone) { - tempZone = zoneRegex.exec(tempZone[tempZone.length - 1]); - zone = tempZone[1] || tempZone[0]; + const zoneRegex = new RegExp(/zone = (.+),|CHARACTER LIST/gm); + let tempZone = logChunk.match(zoneRegex); - world = /= (.+?)\/(.+)/.exec(tempZone); - world = world && zones.zoneNames[world[1]] ? world[1] : "WizardCity"; - } - } catch(err) { console.log(err); } + if(tempZone) { + tempZone = zoneRegex.exec(tempZone[tempZone.length - 1]); + zone = tempZone[1] || tempZone[0]; + + world = /= (.+?)\/(.+)/.exec(tempZone); + world = world && zones.zoneNames[world[1]] ? world[1] : "WizardCity"; + } // Get health const healthRegex = new RegExp(/WizClientGameEf (.+): Updating health globe \(new health: (\d+), new health max: (\d+)\)/gm); let tempHealth = logChunk.match(healthRegex); + if(tempHealth) { health = healthRegex.exec(tempHealth[tempHealth.length - 1]); health = health[3] < health[2] ? [health[2], health[2]] : [health[2], health[3]]; } + + // Check if the game has been quit + if(logChunk.match(/GameClient::HandleQuit\(\)|Logout due to Away From Keyboard/)) { + rpc.destroy(); + process.exit(); + } }).catch((err) => { console.log(err); }); oldLineCount = currentLineCount; @@ -49,7 +55,6 @@ fs.watchFile(logPath, { interval: 100 }, async (_curr, _prev) => { instance: false }; rpc.setActivity(activity); - console.log("Activity set!"); } }); @@ -69,6 +74,24 @@ function countLines() { }); } -rpc.on("ready", () => { console.log("Successfully connected to Discord!"); }); +let reconnect; +const connectDiscord = () => { + console.log("Connecting to your Discord app..."); -rpc.login({ clientId: "799404226081849345" }).catch(console.error); + rpc = new DiscordRPC.Client({ transport: "ipc" }); + + rpc.once("ready", () => { + console.log("Successfully connected to Discord!"); + }); + + rpc.login({ clientId: "799404226081849345" }).then(() => { + clearInterval(reconnect); + }).catch((err) => { + console.log(`${err}\nMake sure Discord is running. You probably need to restart Discord (Ctrl+R on Discord).\n`); + if(!reconnect) { + reconnect = setInterval(connectDiscord, 10050); + connectDiscord(); + } + }); +}; +connectDiscord(); diff --git a/package.json b/package.json index 5418f5c..dcb430f 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "wizard101-rpc", - "version": "0.3.0", + "version": "0.5.0", "description": "Discord Rich Presence for Wizard101!", "main": "index.js", "author": "Christopher Gwynn", "license": "MIT", "dependencies": { - "discord-rpc": "^3.1.4", + "discord-rpc": "discordjs/rpc", "read-last-lines": "^1.8.0" } } diff --git a/settings.json b/settings.json index e69de29..d6998d5 100644 --- a/settings.json +++ b/settings.json @@ -0,0 +1,4 @@ +{ + "path": "", + "startup": "" +}
\ No newline at end of file @@ -15,6 +15,13 @@ discord-rpc@^3.1.4: node-fetch "^2.6.1" ws "^7.3.1" +discord-rpc@discordjs/rpc: + version "3.1.4" + resolved "https://codeload.github.com/discordjs/rpc/tar.gz/cbdd31f98b20de0222a793ae0b14018f8119cfb2" + dependencies: + node-fetch "^2.6.1" + ws "^7.3.1" + mz@^2.7.0: version "2.7.0" resolved "https://registry.yarnpkg.com/mz/-/mz-2.7.0.tgz#95008057a56cafadc2bc63dde7f9ff6955948e32" |