Skip to content

Commit

Permalink
formatting and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
wont-stream committed Sep 26, 2024
1 parent 8a6eaba commit 82d1593
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 212 deletions.
20 changes: 10 additions & 10 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
const esbuild = require("esbuild");
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
const repl = require("repl");
const fs = require("node:fs");
const path = require("node:path");
const crypto = require("node:crypto");
const repl = require("node:repl");

const plugins = fs.readdirSync("./plugins");
for (const plugin of plugins) {
let pluginPath = path.join("./plugins/", plugin);
const pluginPath = path.join("./plugins/", plugin);

const pluginManifest = JSON.parse(
fs.readFileSync(path.join(pluginPath, "plugin.json"))
fs.readFileSync(path.join(pluginPath, "plugin.json")),
);

const outfile = path.join(pluginPath, "dist/index.js");

esbuild
.build({
entryPoints: [
"./" + path.join(pluginPath, pluginManifest.main ?? "index.js"),
`./${path.join(pluginPath, pluginManifest.main ?? "index.js")}`,
],
bundle: true,
minify: true,
format: "esm",
// Make every node builtin external while still bundling for browsers.
external: [
...repl._builtinLibs,
...repl._builtinLibs.map((m) => "node:" + m),
...repl._builtinLibs.map((m) => `node:${m}`),
"@neptune",
"@plugin",
],
Expand All @@ -44,10 +44,10 @@ for (const plugin of plugins) {
description: pluginManifest.description,
author: pluginManifest.author,
hash: this.read(),
})
}),
);

console.log("Built " + pluginManifest.name + "!");
console.log(`Built ${pluginManifest.name}!`);
});
});
}
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "neptune-plugin-template",
"description": "A neptune plugin monorepo template",
"main": "index.js",
"scripts": {
"build": "node ./build.js"
},
"devDependencies": {
"esbuild": "^0.18.20"
}
"name": "neptune-plugin-template",
"description": "A neptune plugin monorepo template",
"main": "index.js",
"scripts": {
"build": "node ./build.js"
},
"devDependencies": {
"esbuild": "^0.18.20"
}
}
8 changes: 4 additions & 4 deletions plugins/DiscordRPC/plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Discord RPC",
"description": "An opinionated arRPC only Discord RPC. (forked from toonlink's plugin)",
"author": "wont-stream",
"main": "./src/index.js"
"name": "Discord RPC",
"description": "An opinionated arRPC only Discord RPC. (forked from toonlink's plugin)",
"author": "wont-stream",
"main": "./src/index.js"
}
292 changes: 148 additions & 144 deletions plugins/DiscordRPC/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,157 +5,161 @@ const formatLongString = (s) => (s.length >= 128 ? `${s.slice(0, 125)}...` : s);
let programaticPause = false;

function getTrackVibrantColor() {
const sheets = document.styleSheets;

for (let i = 0; i < sheets.length; i++) {
try {
const rules = sheets[i].cssRules;

for (let j = 0; j < rules.length; j++) {
const rule = rules[j];

if (rule.selectorText === ":root") {
const styles = rule.style;
const trackVibrantColor = styles.getPropertyValue(
"--track-vibrant-color"
);
if (trackVibrantColor) {
return trackVibrantColor.trim();
}
}
}
} catch (e) { }
}

return null;
const sheets = document.styleSheets;

for (let i = 0; i < sheets.length; i++) {
try {
const rules = sheets[i].cssRules;

for (let j = 0; j < rules.length; j++) {
const rule = rules[j];

if (rule.selectorText === ":root") {
const styles = rule.style;
const trackVibrantColor = styles.getPropertyValue(
"--track-vibrant-color",
);
if (trackVibrantColor) {
return trackVibrantColor.trim();
}
}
}
} catch (e) {}
}

return null;
}

class WebSocketTransport {
constructor() {
this.ws = null;
this.tries = 0;
}

async connect() {
const port = 6463 + (this.tries % 10);
this.tries += 1;
this.ws = new WebSocket(
`ws://localhost:${port}/?v=1&client_id=1288341778637918208`
);

this.ws.onopen = this.onOpen.bind(this);

this.ws.onclose = this.onClose.bind(this);

this.ws.onerror = this.onError.bind(this);
}

onOpen() {
unloadables.push(
neptune.intercept("playbackControls/TIME_UPDATE", ([current]) => {
if (programaticPause) return;
const { item: currentlyPlaying, type: mediaType } =
neptune.currentMediaItem;

// TODO: add video support
if (mediaType !== "track") return;

const date = new Date();
const now = (date.getTime() / 1000);
const remaining = date.setSeconds(
date.getSeconds() + (currentlyPlaying.duration - current)
);

const paused =
neptune.store.getState().playbackControls.playbackState ===
"NOT_PLAYING";

this.ws.readyState === 1 &&
this.send({
cmd: "SET_ACTIVITY",
args: {
pid: 2094112,
activity: {
timestamps: {
...(paused
? {}
: {
start: now,
end: remaining,
}),
},
type: 2,
name: formatLongString(currentlyPlaying.title),
details: formatLongString(
`by ${currentlyPlaying.artists.map((a) => a.name).join(", ")}`
),
assets: {
large_image: `https://resources.tidal.com/images/${currentlyPlaying.album.cover
.split("-")
.join("/")}/80x80.jpg`,
large_text: `on ${formatLongString(
currentlyPlaying.album.title
)}`,
small_text:
`${getTrackVibrantColor()}|${neptune.currentMediaItem.item.id}`,
...(paused
? {
small_image: "paused-pause",
}
: {}),
},
buttons: [{ label: "Play Song", url: `https://listen.tidal.com/track/${neptune.currentMediaItem.item.id}?u` }],
},
},
});
})
);

programaticPause = true;
neptune.actions.playbackControls.pause();
programaticPause = false;
}

onClose(event) {
if (!event.wasClean) {
return;
}
}

onError() {
for (const u of unloadables) {
u();
}
try {
this.ws.close();
} catch { } // eslint-disable-line no-empty

setTimeout(() => {
this.connect();
}, 250);
}

send(data) {
this.ws.send(JSON.stringify(data));
}

close() {
this.ws.close();
}
constructor() {
this.ws = null;
this.tries = 0;
}

async connect() {
const port = 6463 + (this.tries % 10);
this.tries += 1;
this.ws = new WebSocket(
`ws://localhost:${port}/?v=1&client_id=1288341778637918208`,
);

this.ws.onopen = this.onOpen.bind(this);

this.ws.onclose = this.onClose.bind(this);

this.ws.onerror = this.onError.bind(this);
}

onOpen() {
unloadables.push(
neptune.intercept("playbackControls/TIME_UPDATE", ([current]) => {
if (programaticPause) return;
const { item: currentlyPlaying, type: mediaType } =
neptune.currentMediaItem;

// TODO: add video support
if (mediaType !== "track") return;

const date = new Date();
const now = date.getTime() / 1000;
const remaining = date.setSeconds(
date.getSeconds() + (currentlyPlaying.duration - current),
);

const paused =
neptune.store.getState().playbackControls.playbackState ===
"NOT_PLAYING";

this.ws.readyState === 1 &&
this.send({
cmd: "SET_ACTIVITY",
args: {
pid: 2094112,
activity: {
timestamps: {
...(paused
? {}
: {
start: now,
end: remaining,
}),
},
type: 2,
name: formatLongString(currentlyPlaying.title),
details: formatLongString(
`by ${currentlyPlaying.artists.map((a) => a.name).join(", ")}`,
),
assets: {
large_image: `https://resources.tidal.com/images/${currentlyPlaying.album.cover
.split("-")
.join("/")}/80x80.jpg`,
large_text: `on ${formatLongString(
currentlyPlaying.album.title,
)}`,
small_text: `${getTrackVibrantColor()}|${neptune.currentMediaItem.item.id}`,
...(paused
? {
small_image: "paused-pause",
}
: {}),
},
buttons: [
{
label: "Play Song",
url: `https://listen.tidal.com/track/${neptune.currentMediaItem.item.id}?u`,
},
],
},
},
});
}),
);

programaticPause = true;
neptune.actions.playbackControls.pause();
programaticPause = false;
}

onClose(event) {
if (!event.wasClean) {
return;
}
}

onError() {
for (const u of unloadables) {
u();
}
try {
this.ws.close();
} catch {} // eslint-disable-line no-empty

setTimeout(() => {
this.connect();
}, 250);
}

send(data) {
this.ws.send(JSON.stringify(data));
}

close() {
this.ws.close();
}
}

const ws = new WebSocketTransport();

ws.connect();

export async function onUnload() {
for (const u of unloadables) {
u();
}

if (ws) {
try {
ws.close();
} catch { }
}
}
for (const u of unloadables) {
u();
}

if (ws) {
try {
ws.close();
} catch {}
}
}
Loading

0 comments on commit 82d1593

Please sign in to comment.