Prettier
This commit is contained in:
parent
4f6a55bec3
commit
a4b47a37e6
3 changed files with 285 additions and 201 deletions
340
index.js
340
index.js
|
@ -1,124 +1,125 @@
|
||||||
require('v8-compile-cache');
|
require('v8-compile-cache');
|
||||||
const {app, components} = require('electron'),
|
const { app, components } = require('electron'),
|
||||||
{resolve, join} = require("path"),
|
{ resolve, join } = require('path'),
|
||||||
CiderBase = require('./src/main/cider-base');
|
CiderBase = require('./src/main/cider-base');
|
||||||
|
|
||||||
const comps = components;
|
const comps = components;
|
||||||
|
|
||||||
|
|
||||||
// Analytics for debugging.
|
// Analytics for debugging.
|
||||||
const ElectronSentry = require("@sentry/electron");
|
const ElectronSentry = require('@sentry/electron');
|
||||||
ElectronSentry.init({dsn: "https://68c422bfaaf44dea880b86aad5a820d2@o954055.ingest.sentry.io/6112214"});
|
ElectronSentry.init({
|
||||||
|
dsn: 'https://68c422bfaaf44dea880b86aad5a820d2@o954055.ingest.sentry.io/6112214'
|
||||||
|
});
|
||||||
|
|
||||||
const configDefaults = {
|
const configDefaults = {
|
||||||
"general": {
|
general: {
|
||||||
"close_behavior": 0, // 0 = close, 1 = minimize, 2 = minimize to tray
|
close_behavior: 0, // 0 = close, 1 = minimize, 2 = minimize to tray
|
||||||
"startup_behavior": 0, // 0 = nothing, 1 = open on startup
|
startup_behavior: 0, // 0 = nothing, 1 = open on startup
|
||||||
"discord_rpc": 1, // 0 = disabled, 1 = enabled as Cider, 2 = enabled as Apple Music
|
discord_rpc: 1, // 0 = disabled, 1 = enabled as Cider, 2 = enabled as Apple Music
|
||||||
"discordClearActivityOnPause": 1, // 0 = disabled, 1 = enabled
|
discordClearActivityOnPause: 1, // 0 = disabled, 1 = enabled
|
||||||
"volume": 1
|
volume: 1
|
||||||
},
|
},
|
||||||
"home": {
|
home: {
|
||||||
"followedArtists": [],
|
followedArtists: [],
|
||||||
"favoriteItems": []
|
favoriteItems: []
|
||||||
},
|
},
|
||||||
"audio": {
|
audio: {
|
||||||
"quality": "990",
|
quality: '990',
|
||||||
"seamless_audio": true,
|
seamless_audio: true,
|
||||||
"normalization": false,
|
normalization: false,
|
||||||
"spatial": false,
|
spatial: false,
|
||||||
"spatial_properties": {
|
spatial_properties: {
|
||||||
"presets": [],
|
presets: [],
|
||||||
"gain": 0.8,
|
gain: 0.8,
|
||||||
"listener_position": [0, 0, 0],
|
listener_position: [0, 0, 0],
|
||||||
"audio_position": [0, 0, 0],
|
audio_position: [0, 0, 0],
|
||||||
"room_dimensions": {
|
room_dimensions: {
|
||||||
"width": 32,
|
width: 32,
|
||||||
"height": 12,
|
height: 12,
|
||||||
"depth": 32
|
depth: 32
|
||||||
},
|
},
|
||||||
"room_materials": {
|
room_materials: {
|
||||||
"left": 'metal',
|
left: 'metal',
|
||||||
"right": 'metal',
|
right: 'metal',
|
||||||
"front": 'brick-bare',
|
front: 'brick-bare',
|
||||||
"back": 'brick-bare',
|
back: 'brick-bare',
|
||||||
"down": 'acoustic-ceiling-tiles',
|
down: 'acoustic-ceiling-tiles',
|
||||||
"up": 'acoustic-ceiling-tiles',
|
up: 'acoustic-ceiling-tiles'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"visual": {
|
visual: {
|
||||||
"theme": "",
|
theme: '',
|
||||||
"scrollbars": 0, // 0 = show on hover, 2 = always hide, 3 = always show
|
scrollbars: 0, // 0 = show on hover, 2 = always hide, 3 = always show
|
||||||
"refresh_rate": 0,
|
refresh_rate: 0,
|
||||||
"animated_artwork": "limited", // 0 = always, 1 = limited, 2 = never
|
animated_artwork: 'limited', // 0 = always, 1 = limited, 2 = never
|
||||||
"animated_artwork_qualityLevel": 1,
|
animated_artwork_qualityLevel: 1,
|
||||||
"bg_artwork_rotation": false,
|
bg_artwork_rotation: false,
|
||||||
"hw_acceleration": "default", // default, webgpu, disabled
|
hw_acceleration: 'default', // default, webgpu, disabled
|
||||||
"window_transparency": "disabled"
|
window_transparency: 'disabled'
|
||||||
},
|
},
|
||||||
"lyrics": {
|
lyrics: {
|
||||||
"enable_mxm": false,
|
enable_mxm: false,
|
||||||
"mxm_karaoke": false,
|
mxm_karaoke: false,
|
||||||
"mxm_language": "en",
|
mxm_language: 'en',
|
||||||
"enable_yt": false,
|
enable_yt: false
|
||||||
},
|
},
|
||||||
"lastfm": {
|
lastfm: {
|
||||||
"enabled": false,
|
enabled: false,
|
||||||
"scrobble_after": 30,
|
scrobble_after: 30,
|
||||||
"auth_token": "",
|
auth_token: '',
|
||||||
"enabledRemoveFeaturingArtists": true,
|
enabledRemoveFeaturingArtists: true,
|
||||||
"NowPlaying": "true"
|
NowPlaying: 'true'
|
||||||
},
|
},
|
||||||
"advanced": {
|
advanced: {
|
||||||
"AudioContext": false,
|
AudioContext: false
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
const merge = (target, source) => {
|
const merge = (target, source) => {
|
||||||
// Iterate through `source` properties and if an `Object` set property to merge of `target` and `source` properties
|
// Iterate through `source` properties and if an `Object` set property to merge of `target` and `source` properties
|
||||||
for (const key of Object.keys(source)) {
|
for (const key of Object.keys(source)) {
|
||||||
if (source[key] instanceof Object) Object.assign(source[key], merge(target[key], source[key]))
|
if (source[key] instanceof Object)
|
||||||
|
Object.assign(source[key], merge(target[key], source[key]));
|
||||||
}
|
}
|
||||||
// Join `target` and modified `source`
|
// Join `target` and modified `source`
|
||||||
Object.assign(target || {}, source)
|
Object.assign(target || {}, source);
|
||||||
return target
|
return target;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
const Store = require('electron-store');
|
||||||
const Store = require("electron-store");
|
|
||||||
app.cfg = new Store({
|
app.cfg = new Store({
|
||||||
defaults: configDefaults
|
defaults: configDefaults
|
||||||
});
|
});
|
||||||
let currentCfg = app.cfg.get()
|
let currentCfg = app.cfg.get();
|
||||||
app.cfg.set(merge(configDefaults, currentCfg))
|
app.cfg.set(merge(configDefaults, currentCfg));
|
||||||
|
|
||||||
app.paths = {
|
app.paths = {
|
||||||
ciderCache: resolve(app.getPath("userData"), "CiderCache"),
|
ciderCache: resolve(app.getPath('userData'), 'CiderCache'),
|
||||||
themes: resolve(app.getPath("userData"), "Themes"),
|
themes: resolve(app.getPath('userData'), 'Themes'),
|
||||||
plugins: resolve(app.getPath("userData"), "Plugins"),
|
plugins: resolve(app.getPath('userData'), 'Plugins')
|
||||||
}
|
};
|
||||||
|
|
||||||
switch (app.cfg.get("visual.hw_acceleration")) {
|
switch (app.cfg.get('visual.hw_acceleration')) {
|
||||||
default:
|
default:
|
||||||
case "default":
|
case 'default':
|
||||||
app.commandLine.appendSwitch('enable-accelerated-mjpeg-decode')
|
app.commandLine.appendSwitch('enable-accelerated-mjpeg-decode');
|
||||||
app.commandLine.appendSwitch('enable-accelerated-video')
|
app.commandLine.appendSwitch('enable-accelerated-video');
|
||||||
app.commandLine.appendSwitch('disable-gpu-driver-bug-workarounds')
|
app.commandLine.appendSwitch('disable-gpu-driver-bug-workarounds');
|
||||||
app.commandLine.appendSwitch('ignore-gpu-blacklist')
|
app.commandLine.appendSwitch('ignore-gpu-blacklist');
|
||||||
app.commandLine.appendSwitch('enable-native-gpu-memory-buffers')
|
app.commandLine.appendSwitch('enable-native-gpu-memory-buffers');
|
||||||
app.commandLine.appendSwitch('enable-accelerated-video-decode');
|
app.commandLine.appendSwitch('enable-accelerated-video-decode');
|
||||||
app.commandLine.appendSwitch('enable-gpu-rasterization');
|
app.commandLine.appendSwitch('enable-gpu-rasterization');
|
||||||
app.commandLine.appendSwitch('enable-native-gpu-memory-buffers');
|
app.commandLine.appendSwitch('enable-native-gpu-memory-buffers');
|
||||||
app.commandLine.appendSwitch('enable-oop-rasterization');
|
app.commandLine.appendSwitch('enable-oop-rasterization');
|
||||||
break;
|
break;
|
||||||
case "webgpu":
|
case 'webgpu':
|
||||||
console.info("WebGPU is enabled.");
|
console.info('WebGPU is enabled.');
|
||||||
app.commandLine.appendSwitch('enable-unsafe-webgpu')
|
app.commandLine.appendSwitch('enable-unsafe-webgpu');
|
||||||
break;
|
break;
|
||||||
case "disabled":
|
case 'disabled':
|
||||||
console.info("Hardware acceleration is disabled.");
|
console.info('Hardware acceleration is disabled.');
|
||||||
app.commandLine.appendSwitch('disable-gpu')
|
app.commandLine.appendSwitch('disable-gpu');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,13 +131,13 @@ function CreateWindow() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** CIDER **/
|
/** CIDER **/
|
||||||
const ciderwin = require("./src/main/cider-base")
|
const ciderwin = require('./src/main/cider-base');
|
||||||
app.win = ciderwin
|
app.win = ciderwin;
|
||||||
app.win.Start()
|
app.win.Start();
|
||||||
/** CIDER **/
|
/** CIDER **/
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.platform === "linux") {
|
if (process.platform === 'linux') {
|
||||||
app.commandLine.appendSwitch('disable-features', 'MediaSessionService');
|
app.commandLine.appendSwitch('disable-features', 'MediaSessionService');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,37 +145,36 @@ app.commandLine.appendSwitch('no-sandbox');
|
||||||
// app.commandLine.appendSwitch('js-flags', '--max-old-space-size=1024')
|
// app.commandLine.appendSwitch('js-flags', '--max-old-space-size=1024')
|
||||||
|
|
||||||
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
* App Event Handlers
|
* App Event Handlers
|
||||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
||||||
|
|
||||||
app.whenReady().then(async () => {
|
app.whenReady().then(async () => {
|
||||||
if (process.platform === "win32") {
|
if (process.platform === 'win32') {
|
||||||
app.commandLine.appendSwitch('high-dpi-support', 'true')
|
app.commandLine.appendSwitch('high-dpi-support', 'true');
|
||||||
app.commandLine.appendSwitch('force-device-scale-factor', '1')
|
app.commandLine.appendSwitch('force-device-scale-factor', '1');
|
||||||
app.commandLine.appendSwitch('disable-pinch');
|
app.commandLine.appendSwitch('disable-pinch');
|
||||||
}
|
}
|
||||||
if (comps == null) {
|
if (comps == null) {
|
||||||
app.on("widevine-ready", () => {
|
app.on('widevine-ready', () => {
|
||||||
console.log('[Cider] Application is Ready. Creating Window.')
|
console.log('[Cider] Application is Ready. Creating Window.');
|
||||||
if (!app.isPackaged) {
|
if (!app.isPackaged) {
|
||||||
console.info('[Cider] Running in development mode.')
|
console.info('[Cider] Running in development mode.');
|
||||||
require('vue-devtools').install()
|
require('vue-devtools').install();
|
||||||
}
|
}
|
||||||
CreateWindow()
|
CreateWindow();
|
||||||
})
|
});
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
await comps.whenReady();
|
await comps.whenReady();
|
||||||
console.log('components ready:', comps.status());
|
console.log('components ready:', comps.status());
|
||||||
|
|
||||||
console.log('[Cider] Application is Ready. Creating Window.')
|
console.log('[Cider] Application is Ready. Creating Window.');
|
||||||
if (!app.isPackaged) {
|
if (!app.isPackaged) {
|
||||||
console.info('[Cider] Running in development mode.')
|
console.info('[Cider] Running in development mode.');
|
||||||
require('vue-devtools').install()
|
require('vue-devtools').install();
|
||||||
}
|
}
|
||||||
CreateWindow()
|
CreateWindow();
|
||||||
})
|
});
|
||||||
|
|
||||||
|
|
||||||
app.on('before-quit', () => {
|
app.on('before-quit', () => {
|
||||||
console.warn(`${app.getName()} exited.`);
|
console.warn(`${app.getName()} exited.`);
|
||||||
|
@ -183,73 +183,123 @@ app.on('before-quit', () => {
|
||||||
// Widevine Stuff
|
// Widevine Stuff
|
||||||
app.on('widevine-ready', (version, lastVersion) => {
|
app.on('widevine-ready', (version, lastVersion) => {
|
||||||
if (null !== lastVersion) {
|
if (null !== lastVersion) {
|
||||||
console.log('[Cider][Widevine] Widevine ' + version + ', upgraded from ' + lastVersion + ', is ready to be used!')
|
console.log(
|
||||||
|
'[Cider][Widevine] Widevine ' +
|
||||||
|
version +
|
||||||
|
', upgraded from ' +
|
||||||
|
lastVersion +
|
||||||
|
', is ready to be used!'
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
console.log('[Cider][Widevine] Widevine ' + version + ' is ready to be used!')
|
console.log(
|
||||||
|
'[Cider][Widevine] Widevine ' + version + ' is ready to be used!'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
app.on('widevine-update-pending', (currentVersion, pendingVersion) => {
|
app.on('widevine-update-pending', (currentVersion, pendingVersion) => {
|
||||||
console.log('[Cider][Widevine] Widevine ' + currentVersion + ' is ready to be upgraded to ' + pendingVersion + '!')
|
console.log(
|
||||||
})
|
'[Cider][Widevine] Widevine ' +
|
||||||
|
currentVersion +
|
||||||
|
' is ready to be upgraded to ' +
|
||||||
|
pendingVersion +
|
||||||
|
'!'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
app.on('widevine-error', (error) => {
|
app.on('widevine-error', (error) => {
|
||||||
console.log('[Cider][Widevine] Widevine installation encountered an error: ' + error)
|
console.log(
|
||||||
app.exit()
|
'[Cider][Widevine] Widevine installation encountered an error: ' + error
|
||||||
})
|
);
|
||||||
|
app.exit();
|
||||||
|
});
|
||||||
|
|
||||||
if (process.defaultApp) {
|
if (process.defaultApp) {
|
||||||
if (process.argv.length >= 2) {
|
if (process.argv.length >= 2) {
|
||||||
app.setAsDefaultProtocolClient('cider', process.execPath, [resolve(process.argv[1])])
|
app.setAsDefaultProtocolClient('cider', process.execPath, [
|
||||||
app.setAsDefaultProtocolClient('ame', process.execPath, [resolve(process.argv[1])])
|
resolve(process.argv[1])
|
||||||
app.setAsDefaultProtocolClient('itms', process.execPath, [resolve(process.argv[1])])
|
]);
|
||||||
app.setAsDefaultProtocolClient('itmss', process.execPath, [resolve(process.argv[1])])
|
app.setAsDefaultProtocolClient('ame', process.execPath, [
|
||||||
app.setAsDefaultProtocolClient('musics', process.execPath, [resolve(process.argv[1])])
|
resolve(process.argv[1])
|
||||||
app.setAsDefaultProtocolClient('music', process.execPath, [resolve(process.argv[1])])
|
]);
|
||||||
|
app.setAsDefaultProtocolClient('itms', process.execPath, [
|
||||||
|
resolve(process.argv[1])
|
||||||
|
]);
|
||||||
|
app.setAsDefaultProtocolClient('itmss', process.execPath, [
|
||||||
|
resolve(process.argv[1])
|
||||||
|
]);
|
||||||
|
app.setAsDefaultProtocolClient('musics', process.execPath, [
|
||||||
|
resolve(process.argv[1])
|
||||||
|
]);
|
||||||
|
app.setAsDefaultProtocolClient('music', process.execPath, [
|
||||||
|
resolve(process.argv[1])
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
app.setAsDefaultProtocolClient('cider') // Custom AME Protocol
|
app.setAsDefaultProtocolClient('cider'); // Custom AME Protocol
|
||||||
app.setAsDefaultProtocolClient('ame') // Custom AME Protocol
|
app.setAsDefaultProtocolClient('ame'); // Custom AME Protocol
|
||||||
app.setAsDefaultProtocolClient('itms') // iTunes HTTP Protocol
|
app.setAsDefaultProtocolClient('itms'); // iTunes HTTP Protocol
|
||||||
app.setAsDefaultProtocolClient('itmss') // iTunes HTTPS Protocol
|
app.setAsDefaultProtocolClient('itmss'); // iTunes HTTPS Protocol
|
||||||
app.setAsDefaultProtocolClient('musics') // macOS Client Protocol
|
app.setAsDefaultProtocolClient('musics'); // macOS Client Protocol
|
||||||
app.setAsDefaultProtocolClient('music') // macOS Client Protocol
|
app.setAsDefaultProtocolClient('music'); // macOS Client Protocol
|
||||||
}
|
}
|
||||||
|
|
||||||
app.on('open-url', (event, url) => {
|
app.on('open-url', (event, url) => {
|
||||||
event.preventDefault()
|
event.preventDefault();
|
||||||
if (url.includes('ame://') || url.includes('itms://') || url.includes('itmss://') || url.includes('musics://') || url.includes('music://')) {
|
if (
|
||||||
CiderBase.LinkHandler(url)
|
url.includes('ame://') ||
|
||||||
|
url.includes('itms://') ||
|
||||||
|
url.includes('itmss://') ||
|
||||||
|
url.includes('musics://') ||
|
||||||
|
url.includes('music://')
|
||||||
|
) {
|
||||||
|
CiderBase.LinkHandler(url);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
app.on('second-instance', (_e, argv) => {
|
app.on('second-instance', (_e, argv) => {
|
||||||
console.warn(`[InstanceHandler][SecondInstanceHandler] Second Instance Started with args: [${argv.join(', ')}]`)
|
console.warn(
|
||||||
|
`[InstanceHandler][SecondInstanceHandler] Second Instance Started with args: [${argv.join(
|
||||||
|
', '
|
||||||
|
)}]`
|
||||||
|
);
|
||||||
|
|
||||||
// Checks if first instance is authorized and if second instance has protocol args
|
// Checks if first instance is authorized and if second instance has protocol args
|
||||||
argv.forEach((value) => {
|
argv.forEach((value) => {
|
||||||
if (value.includes('ame://') || value.includes('itms://') || value.includes('itmss://') || value.includes('musics://') || value.includes('music://')) {
|
if (
|
||||||
console.warn(`[InstanceHandler][SecondInstanceHandler] Found Protocol!`)
|
value.includes('ame://') ||
|
||||||
|
value.includes('itms://') ||
|
||||||
|
value.includes('itmss://') ||
|
||||||
|
value.includes('musics://') ||
|
||||||
|
value.includes('music://')
|
||||||
|
) {
|
||||||
|
console.warn(
|
||||||
|
`[InstanceHandler][SecondInstanceHandler] Found Protocol!`
|
||||||
|
);
|
||||||
CiderBase.LinkHandler(value);
|
CiderBase.LinkHandler(value);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
if (argv.includes("--force-quit")) {
|
if (argv.includes('--force-quit')) {
|
||||||
console.warn('[InstanceHandler][SecondInstanceHandler] Force Quit found. Quitting App.');
|
console.warn(
|
||||||
|
'[InstanceHandler][SecondInstanceHandler] Force Quit found. Quitting App.'
|
||||||
|
);
|
||||||
// app.isQuiting = true
|
// app.isQuiting = true
|
||||||
app.quit()
|
app.quit();
|
||||||
} else if (CiderBase.win && true) { // If a Second Instance has Been Started
|
} else if (CiderBase.win && true) {
|
||||||
console.warn('[InstanceHandler][SecondInstanceHandler] Showing window.');
|
// If a Second Instance has Been Started
|
||||||
app.win.show()
|
console.warn(
|
||||||
app.win.focus()
|
'[InstanceHandler][SecondInstanceHandler] Showing window.'
|
||||||
|
);
|
||||||
|
app.win.show();
|
||||||
|
app.win.focus();
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
if (!app.requestSingleInstanceLock() && true) {
|
if (!app.requestSingleInstanceLock() && true) {
|
||||||
console.warn("[InstanceHandler] Existing Instance is Blocking Second Instance.");
|
console.warn(
|
||||||
|
'[InstanceHandler] Existing Instance is Blocking Second Instance.'
|
||||||
|
);
|
||||||
app.quit();
|
app.quit();
|
||||||
// app.isQuiting = true
|
// app.isQuiting = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,11 @@ const CiderBase = {
|
||||||
|
|
||||||
let win = null;
|
let win = null;
|
||||||
const options = {
|
const options = {
|
||||||
icon: join(__dirname, `../../resources/icons/icon.` + (process.platform === "win32" ? "ico" : "png")),
|
icon: join(
|
||||||
|
__dirname,
|
||||||
|
`../../resources/icons/icon.` +
|
||||||
|
(process.platform === 'win32' ? 'ico' : 'png')
|
||||||
|
),
|
||||||
width: mainWindowState.width,
|
width: mainWindowState.width,
|
||||||
height: mainWindowState.height,
|
height: mainWindowState.height,
|
||||||
x: mainWindowState.x,
|
x: mainWindowState.x,
|
||||||
|
|
|
@ -1,35 +1,42 @@
|
||||||
const {app} = require('electron'),
|
const { app } = require('electron'),
|
||||||
DiscordRPC = require('discord-rpc')
|
DiscordRPC = require('discord-rpc');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connects to Discord RPC
|
* Connects to Discord RPC
|
||||||
* @param {string} clientId
|
* @param {string} clientId
|
||||||
*/
|
*/
|
||||||
connect: function (clientId) {
|
connect: function (clientId) {
|
||||||
app.discord = {isConnected: false};
|
app.discord = { isConnected: false };
|
||||||
if (app.cfg.get('general.discord_rpc') == 0 || app.discord.isConnected) return;
|
if (app.cfg.get('general.discord_rpc') == 0 || app.discord.isConnected)
|
||||||
|
return;
|
||||||
|
|
||||||
DiscordRPC.register(clientId) // Apparently needed for ask to join, join, spectate etc.
|
DiscordRPC.register(clientId); // Apparently needed for ask to join, join, spectate etc.
|
||||||
const client = new DiscordRPC.Client({transport: "ipc"});
|
const client = new DiscordRPC.Client({ transport: 'ipc' });
|
||||||
app.discord = Object.assign(client, {error: false, activityCache: null, isConnected: false});
|
app.discord = Object.assign(client, {
|
||||||
|
error: false,
|
||||||
|
activityCache: null,
|
||||||
|
isConnected: false
|
||||||
|
});
|
||||||
|
|
||||||
// Login to Discord
|
// Login to Discord
|
||||||
app.discord.login({clientId})
|
app.discord
|
||||||
|
.login({ clientId })
|
||||||
.then(() => {
|
.then(() => {
|
||||||
app.discord.isConnected = true;
|
app.discord.isConnected = true;
|
||||||
})
|
})
|
||||||
.catch((e) => console.error(`[DiscordRPC][connect] ${e}`));
|
.catch((e) => console.error(`[DiscordRPC][connect] ${e}`));
|
||||||
|
|
||||||
app.discord.on('ready', () => {
|
app.discord.on('ready', () => {
|
||||||
console.log(`[DiscordRPC][connect] Successfully Connected to Discord. Authed for user: ${client.user.username} (${client.user.id})`);
|
console.log(
|
||||||
})
|
`[DiscordRPC][connect] Successfully Connected to Discord. Authed for user: ${client.user.username} (${client.user.id})`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
// Handles Errors
|
// Handles Errors
|
||||||
app.discord.on('error', err => {
|
app.discord.on('error', (err) => {
|
||||||
console.error(`[DiscordRPC] ${err}`);
|
console.error(`[DiscordRPC] ${err}`);
|
||||||
this.disconnect()
|
this.disconnect();
|
||||||
app.discord.isConnected = false;
|
app.discord.isConnected = false;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
@ -38,15 +45,21 @@ module.exports = {
|
||||||
* Disconnects from Discord RPC
|
* Disconnects from Discord RPC
|
||||||
*/
|
*/
|
||||||
disconnect: function () {
|
disconnect: function () {
|
||||||
if (app.cfg.get('general.discord_rpc') == 0 || !app.discord.isConnected) return;
|
if (app.cfg.get('general.discord_rpc') == 0 || !app.discord.isConnected)
|
||||||
|
return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
app.discord.destroy().then(() => {
|
app.discord
|
||||||
|
.destroy()
|
||||||
|
.then(() => {
|
||||||
app.discord.isConnected = false;
|
app.discord.isConnected = false;
|
||||||
console.log('[DiscordRPC][disconnect] Disconnected from discord.')
|
console.log(
|
||||||
}).catch((e) => console.error(`[DiscordRPC][disconnect] ${e}`));
|
'[DiscordRPC][disconnect] Disconnected from discord.'
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.catch((e) => console.error(`[DiscordRPC][disconnect] ${e}`));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -58,85 +71,102 @@ module.exports = {
|
||||||
if (app.cfg.get('general.discord_rpc') == 0) return;
|
if (app.cfg.get('general.discord_rpc') == 0) return;
|
||||||
|
|
||||||
if (!app.discord.isConnected) {
|
if (!app.discord.isConnected) {
|
||||||
app.discord.clearActivity().catch((e) => console.error(`[DiscordRPC][updateActivity] ${e}`));
|
app.discord
|
||||||
|
.clearActivity()
|
||||||
|
.catch((e) =>
|
||||||
|
console.error(`[DiscordRPC][updateActivity] ${e}`)
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log('[DiscordRPC][updateActivity] Updating Discord Activity.')
|
// console.log('[DiscordRPC][updateActivity] Updating Discord Activity.')
|
||||||
|
|
||||||
const listenURL = `https://applemusicelectron.com/p?id=${attributes.playParams.id}`
|
const listenURL = `https://applemusicelectron.com/p?id=${attributes.playParams.id}`;
|
||||||
//console.log(attributes)
|
//console.log(attributes)
|
||||||
let ActivityObject = {
|
let ActivityObject = {
|
||||||
details: attributes.name,
|
details: attributes.name,
|
||||||
state: `by ${attributes.artistName}`,
|
state: `by ${attributes.artistName}`,
|
||||||
startTimestamp: attributes.startTime,
|
startTimestamp: attributes.startTime,
|
||||||
endTimestamp: attributes.endTime,
|
endTimestamp: attributes.endTime,
|
||||||
largeImageKey: (attributes.artwork.url.replace('{w}', '1024').replace('{h}', '1024')) ?? 'cider',
|
largeImageKey:
|
||||||
|
attributes.artwork.url
|
||||||
|
.replace('{w}', '1024')
|
||||||
|
.replace('{h}', '1024') ?? 'cider',
|
||||||
largeImageText: attributes.albumName,
|
largeImageText: attributes.albumName,
|
||||||
smallImageKey: (attributes.status ? 'play' : 'pause'),
|
smallImageKey: attributes.status ? 'play' : 'pause',
|
||||||
smallImageText: (attributes.status ? 'Playing' : 'Paused'),
|
smallImageText: attributes.status ? 'Playing' : 'Paused',
|
||||||
instance: true,
|
instance: true,
|
||||||
buttons: [
|
buttons: [{ label: 'Listen on Cider', url: listenURL }]
|
||||||
{label: "Listen on Cider", url: listenURL},
|
|
||||||
]
|
|
||||||
};
|
};
|
||||||
if (ActivityObject.largeImageKey == "" || ActivityObject.largeImageKey == null) {
|
if (
|
||||||
ActivityObject.largeImageKey = (app.cfg.get("general.discord_rpc") == 1) ? "cider" : "logo"
|
ActivityObject.largeImageKey == '' ||
|
||||||
|
ActivityObject.largeImageKey == null
|
||||||
|
) {
|
||||||
|
ActivityObject.largeImageKey =
|
||||||
|
app.cfg.get('general.discord_rpc') == 1 ? 'cider' : 'logo';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the pause/play icon and test for clear activity on pause
|
// Remove the pause/play icon and test for clear activity on pause
|
||||||
if (app.cfg.get('general.discordClearActivityOnPause') == 1) {
|
if (app.cfg.get('general.discordClearActivityOnPause') == 1) {
|
||||||
delete ActivityObject.smallImageKey
|
delete ActivityObject.smallImageKey;
|
||||||
delete ActivityObject.smallImageText
|
delete ActivityObject.smallImageText;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes the timestamp if its not greater than 0
|
// Deletes the timestamp if its not greater than 0
|
||||||
if (!((new Date(attributes.endTime)).getTime() > 0)) {
|
if (!(new Date(attributes.endTime).getTime() > 0)) {
|
||||||
delete ActivityObject.startTimestamp
|
delete ActivityObject.startTimestamp;
|
||||||
delete ActivityObject.endTimestamp
|
delete ActivityObject.endTimestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Artist check
|
// Artist check
|
||||||
if (!attributes.artistName) {
|
if (!attributes.artistName) {
|
||||||
delete ActivityObject.state
|
delete ActivityObject.state;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Album text check
|
// Album text check
|
||||||
if (!ActivityObject.largeImageText || ActivityObject.largeImageText.length < 2) {
|
if (
|
||||||
delete ActivityObject.largeImageText
|
!ActivityObject.largeImageText ||
|
||||||
|
ActivityObject.largeImageText.length < 2
|
||||||
|
) {
|
||||||
|
delete ActivityObject.largeImageText;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks if the name is greater than 128 because some songs can be that long
|
// Checks if the name is greater than 128 because some songs can be that long
|
||||||
if (ActivityObject.details.length > 128) {
|
if (ActivityObject.details.length > 128) {
|
||||||
ActivityObject.details = ActivityObject.details.substring(0, 125) + '...'
|
ActivityObject.details =
|
||||||
|
ActivityObject.details.substring(0, 125) + '...';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Check if its pausing (false) or playing (true)
|
// Check if its pausing (false) or playing (true)
|
||||||
if (!attributes.status) {
|
if (!attributes.status) {
|
||||||
if (app.cfg.get('general.discordClearActivityOnPause') == 1) {
|
if (app.cfg.get('general.discordClearActivityOnPause') == 1) {
|
||||||
app.discord.clearActivity().catch((e) => console.error(`[DiscordRPC][clearActivity] ${e}`));
|
app.discord
|
||||||
ActivityObject = null
|
.clearActivity()
|
||||||
|
.catch((e) =>
|
||||||
|
console.error(`[DiscordRPC][clearActivity] ${e}`)
|
||||||
|
);
|
||||||
|
ActivityObject = null;
|
||||||
} else {
|
} else {
|
||||||
delete ActivityObject.startTimestamp
|
delete ActivityObject.startTimestamp;
|
||||||
delete ActivityObject.endTimestamp
|
delete ActivityObject.endTimestamp;
|
||||||
ActivityObject.smallImageKey = 'pause'
|
ActivityObject.smallImageKey = 'pause';
|
||||||
ActivityObject.smallImageText = 'Paused'
|
ActivityObject.smallImageText = 'Paused';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
if (ActivityObject && ActivityObject !== app.discord.activityCache && ActivityObject.details && ActivityObject.state) {
|
ActivityObject &&
|
||||||
|
ActivityObject !== app.discord.activityCache &&
|
||||||
|
ActivityObject.details &&
|
||||||
|
ActivityObject.state
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
// console.log(`[DiscordRPC][setActivity] Setting activity to ${JSON.stringify(ActivityObject)}`);
|
// console.log(`[DiscordRPC][setActivity] Setting activity to ${JSON.stringify(ActivityObject)}`);
|
||||||
app.discord.setActivity(ActivityObject)
|
app.discord.setActivity(ActivityObject);
|
||||||
app.discord.activityCache = ActivityObject
|
app.discord.activityCache = ActivityObject;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`[DiscordRPC][setActivity] ${err}`)
|
console.error(`[DiscordRPC][setActivity] ${err}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue