DiscordRPC Plugin Overhaul
This commit is contained in:
parent
27becacbb7
commit
0cb80539db
6 changed files with 112 additions and 144 deletions
|
@ -15,8 +15,6 @@ export class AppEvents {
|
||||||
private static win: any = null;
|
private static win: any = null;
|
||||||
|
|
||||||
constructor(store: any) {
|
constructor(store: any) {
|
||||||
console.log('App started');
|
|
||||||
|
|
||||||
AppEvents.store = store
|
AppEvents.store = store
|
||||||
AppEvents.start(store);
|
AppEvents.start(store);
|
||||||
}
|
}
|
||||||
|
@ -26,7 +24,7 @@ export class AppEvents {
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
private static start(store: any): void {
|
private static start(store: any): void {
|
||||||
console.log('App started');
|
console.info('[AppEvents] App started');
|
||||||
|
|
||||||
/**********************************************************************************************************************
|
/**********************************************************************************************************************
|
||||||
* Startup arguments handling
|
* Startup arguments handling
|
||||||
|
@ -38,7 +36,7 @@ export class AppEvents {
|
||||||
|
|
||||||
// Verbose Check
|
// Verbose Check
|
||||||
if (electron.app.commandLine.hasSwitch('verbose')) {
|
if (electron.app.commandLine.hasSwitch('verbose')) {
|
||||||
console.log("[Apple-Music-Electron] User has launched the application with --verbose");
|
console.log("[Cider] User has launched the application with --verbose");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log File Location
|
// Log File Location
|
||||||
|
@ -103,7 +101,7 @@ export class AppEvents {
|
||||||
|
|
||||||
public ready(plug: any) {
|
public ready(plug: any) {
|
||||||
AppEvents.plugin = plug
|
AppEvents.plugin = plug
|
||||||
console.log('App ready');
|
console.log('[AppEvents] App ready');
|
||||||
}
|
}
|
||||||
|
|
||||||
public bwCreated(win: Electron.BrowserWindow) {
|
public bwCreated(win: Electron.BrowserWindow) {
|
||||||
|
|
|
@ -44,7 +44,7 @@ export default class PluginHandler {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
console.log('loaded plugins:', JSON.stringify(plugins))
|
console.log('[PluginHandler] Loaded plugins:', Object.keys(plugins));
|
||||||
return plugins;
|
return plugins;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ export default class ExamplePlugin {
|
||||||
constructor(app: any, store: any) {
|
constructor(app: any, store: any) {
|
||||||
this._app = app;
|
this._app = app;
|
||||||
this._store = store;
|
this._store = store;
|
||||||
console.log('Example plugin loaded');
|
console.debug(`[Plugin][${this.name}] Loading Complete.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -29,14 +29,14 @@ export default class ExamplePlugin {
|
||||||
*/
|
*/
|
||||||
onReady(win: any): void {
|
onReady(win: any): void {
|
||||||
this._win = win;
|
this._win = win;
|
||||||
console.log('Example plugin ready');
|
console.debug(`[Plugin][${this.name}] Ready.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs on app stop
|
* Runs on app stop
|
||||||
*/
|
*/
|
||||||
onBeforeQuit(): void {
|
onBeforeQuit(): void {
|
||||||
console.log('Example plugin stopped');
|
console.debug(`[Plugin][${this.name}] Stopped.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,55 +1,93 @@
|
||||||
import * as DiscordRPC from 'discord-rpc'
|
import * as RPC from 'discord-rpc'
|
||||||
|
|
||||||
|
export default class DiscordRichPresence {
|
||||||
|
|
||||||
export default class DiscordRPCPlugin {
|
|
||||||
/**
|
/**
|
||||||
* Private variables for interaction in plugins
|
* Private variables for interaction in plugins
|
||||||
*/
|
*/
|
||||||
private _win: Electron.BrowserWindow | undefined;
|
private static _store: any;
|
||||||
private _app: any;
|
private static _connection: boolean = false;
|
||||||
private _store: any;
|
|
||||||
private _discord: any;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base Plugin Details (Eventually implemented into a GUI in settings)
|
||||||
|
*/
|
||||||
|
public name: string = 'Discord Rich Presence';
|
||||||
|
public description: string = 'Discord RPC plugin for Cider';
|
||||||
|
public version: string = '1.0.0';
|
||||||
|
public author: string = 'vapormusic/Core (Cider Collective)';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plugin Initialization
|
||||||
|
*/
|
||||||
|
private _client: any = null;
|
||||||
|
private _activity: RPC.Presence = {
|
||||||
|
details: '',
|
||||||
|
state: '',
|
||||||
|
largeImageKey: '',
|
||||||
|
largeImageText: '',
|
||||||
|
smallImageKey: '',
|
||||||
|
smallImageText: '',
|
||||||
|
instance: false
|
||||||
|
};
|
||||||
|
private _activityCache: RPC.Presence = {
|
||||||
|
details: '',
|
||||||
|
state: '',
|
||||||
|
largeImageKey: '',
|
||||||
|
largeImageText: '',
|
||||||
|
smallImageKey: '',
|
||||||
|
smallImageText: '',
|
||||||
|
instance: false
|
||||||
|
};
|
||||||
|
|
||||||
|
/*******************************************************************************************
|
||||||
|
* Private Methods
|
||||||
|
* ****************************************************************************************/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect to Discord
|
||||||
|
* @param clientId
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private connect(clientId: any) {
|
private connect(clientId: any) {
|
||||||
this._discord = {isConnected: false};
|
if (DiscordRichPresence._store.general.discord_rpc == 0) {
|
||||||
if (this._store.general.discord_rpc == 0 || this._discord.isConnected) return;
|
return
|
||||||
|
}
|
||||||
|
|
||||||
DiscordRPC.register(clientId) // Apparently needed for ask to join, join, spectate etc.
|
// Apparently needed for ask to join, join, spectate etc.
|
||||||
const client = new DiscordRPC.Client({transport: "ipc"});
|
RPC.register(clientId)
|
||||||
this._discord = Object.assign(client, {error: false, activityCache: null, isConnected: false});
|
|
||||||
|
|
||||||
// Login to Discord
|
// Create the client
|
||||||
this._discord.login({clientId})
|
this._client = new RPC.Client({transport: "ipc"});
|
||||||
.then(() => {
|
|
||||||
this._discord.isConnected = true;
|
|
||||||
})
|
|
||||||
.catch((e: any) => console.error(`[DiscordRPC][connect] ${e}`));
|
|
||||||
|
|
||||||
this._discord.on('ready', () => {
|
// Runs on Ready
|
||||||
console.log(`[DiscordRPC][connect] Successfully Connected to Discord. Authed for user: ${client.user.username} (${client.user.id})`);
|
this._client.on('ready', () => {
|
||||||
|
console.info(`[DiscordRPC][connect] Successfully Connected to Discord. Authed for user: ${this._client.user.id}.`);
|
||||||
})
|
})
|
||||||
|
|
||||||
// Handles Errors
|
// Handles Errors
|
||||||
this._discord.on('error', (err: any) => {
|
this._client.on('error', (err: any) => {
|
||||||
console.error(`[DiscordRPC] ${err}`);
|
console.error(`[DiscordRichPresence] ${err}`);
|
||||||
this.disconnect()
|
this.disconnect()
|
||||||
this._discord.isConnected = false;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Login to Discord
|
||||||
|
this._client.login({clientId})
|
||||||
|
.then(() => {
|
||||||
|
DiscordRichPresence._connection = true;
|
||||||
|
})
|
||||||
|
.catch((e: any) => console.error(`[DiscordRichPresence][connect] ${e}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disconnects from Discord RPC
|
* Disconnects from Discord RPC
|
||||||
*/
|
*/
|
||||||
private disconnect() {
|
private disconnect() {
|
||||||
if (this._store.general.discord_rpc == 0 || !this._discord.isConnected) return;
|
if (!this._client) return;
|
||||||
|
|
||||||
try {
|
this._client.destroy().then(() => {
|
||||||
this._discord.destroy().then(() => {
|
DiscordRichPresence._connection = false;
|
||||||
this._discord.isConnected = false;
|
console.log('[DiscordRPC][disconnect] Disconnected from discord.')
|
||||||
console.log('[DiscordRPC][disconnect] Disconnected from discord.')
|
}).catch((e: any) => console.error(`[DiscordRPC][disconnect] ${e}`));
|
||||||
}).catch((e: any) => console.error(`[DiscordRPC][disconnect] ${e}`));
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -57,141 +95,70 @@ export default class DiscordRPCPlugin {
|
||||||
* @param {object} attributes
|
* @param {object} attributes
|
||||||
*/
|
*/
|
||||||
private updateActivity(attributes: any) {
|
private updateActivity(attributes: any) {
|
||||||
if (this._store.general.discord_rpc == 0) return;
|
if (!this._client) return;
|
||||||
|
|
||||||
if (!this._discord.isConnected) {
|
if (!DiscordRichPresence._connection) {
|
||||||
this._discord.clearActivity().catch((e: any) => console.error(`[DiscordRPC][updateActivity] ${e}`));
|
this._client.clearActivity().catch((e: any) => console.error(`[DiscordRichPresence][clearActivity] ${e}`));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log('[DiscordRPC][updateActivity] Updating Discord Activity.')
|
const listenURL = `https://cider.sh/p?s&id=${attributes.playParams.id}` // cider://play/s/[id] (for song)
|
||||||
|
|
||||||
const listenURL = `https://cider.sh/p?s&id=${attributes.playParams.id}` // cider://play/s/[id] (for song)
|
this._activity = {
|
||||||
//console.log(attributes)
|
|
||||||
|
|
||||||
interface ActObject extends DiscordRPC.Presence {
|
|
||||||
details?: any,
|
|
||||||
state?: any,
|
|
||||||
startTimestamp?: any,
|
|
||||||
endTimestamp?: any,
|
|
||||||
largeImageKey?: any,
|
|
||||||
largeImageText?: any,
|
|
||||||
smallImageKey?: any,
|
|
||||||
smallImageText?: any,
|
|
||||||
instance: true,
|
|
||||||
buttons?: [
|
|
||||||
{
|
|
||||||
label: string,
|
|
||||||
url: string
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
let ActivityObject: ActObject | null = {
|
|
||||||
details: attributes.name,
|
details: attributes.name,
|
||||||
state: `by ${attributes.artistName}`,
|
state: `${attributes.artistName ? `by ${attributes.artistName}` : ''}`,
|
||||||
startTimestamp: attributes.startTime,
|
startTimestamp: ((new Date(attributes.endTime).getTime() < 0) ? null : attributes.startTime),
|
||||||
endTimestamp: attributes.endTime,
|
endTimestamp: ((new Date(attributes.endTime).getTime() < 0) ? null : 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'),
|
instance: false, // Whether the activity is in a game session
|
||||||
smallImageText: (attributes.status ? 'Playing' : 'Paused'),
|
|
||||||
instance: true,
|
|
||||||
buttons: [
|
buttons: [
|
||||||
{label: "Listen on Cider", url: listenURL},
|
{label: "Listen on Cider", url: listenURL},
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
if (ActivityObject.largeImageKey == "" || ActivityObject.largeImageKey == null) {
|
|
||||||
ActivityObject.largeImageKey = (this._store.general.discord_rpc == 1) ? "cider" : "logo"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove the pause/play icon and test for clear activity on pause
|
|
||||||
if (this._store.general.discordClearActivityOnPause == 1) {
|
|
||||||
delete ActivityObject.smallImageKey
|
|
||||||
delete ActivityObject.smallImageText
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deletes the timestamp if its not greater than 0
|
|
||||||
if (!((new Date(attributes.endTime)).getTime() > 0)) {
|
|
||||||
delete ActivityObject.startTimestamp
|
|
||||||
delete ActivityObject.endTimestamp
|
|
||||||
}
|
|
||||||
|
|
||||||
// Artist check
|
|
||||||
if (!attributes.artistName) {
|
|
||||||
delete ActivityObject.state
|
|
||||||
}
|
|
||||||
|
|
||||||
// Album text check
|
|
||||||
if (!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 (this._activity.details && this._activity.details.length > 128) {
|
||||||
ActivityObject.details = ActivityObject.details.substring(0, 125) + '...'
|
this._activity.details = this._activity.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 (this._store.general.discordClearActivityOnPause == 1) {
|
this._client.clearActivity()
|
||||||
this._discord.clearActivity().catch((e: any) => console.error(`[DiscordRPC][clearActivity] ${e}`));
|
.catch((e: any) => console.error(`[DiscordRichPresence][clearActivity] ${e}`));
|
||||||
ActivityObject = null
|
} else if (this._activity && this._activityCache !== this._activity && this._activity.details) {
|
||||||
} else {
|
this._client.setActivity(this._activity)
|
||||||
delete ActivityObject.startTimestamp
|
.catch((e: any) => console.error(`[DiscordRichPresence][updateActivity] ${e}`));
|
||||||
delete ActivityObject.endTimestamp
|
this._activityCache = this._activity;
|
||||||
ActivityObject.smallImageKey = 'pause'
|
|
||||||
ActivityObject.smallImageText = 'Paused'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (ActivityObject && ActivityObject !== this._discord.activityCache && ActivityObject.details && ActivityObject.state) {
|
|
||||||
try {
|
|
||||||
// console.log(`[DiscordRPC][setActivity] Setting activity to ${JSON.stringify(ActivityObject)}`);
|
|
||||||
this._discord.setActivity(ActivityObject)
|
|
||||||
this._discord.activityCache = ActivityObject
|
|
||||||
} catch (err) {
|
|
||||||
console.error(`[DiscordRPC][setActivity] ${err}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*******************************************************************************************
|
||||||
/**
|
* Public Methods
|
||||||
* Base Plugin Details (Eventually implemented into a GUI in settings)
|
* ****************************************************************************************/
|
||||||
*/
|
|
||||||
public name: string = 'DiscordRPCPlugin';
|
|
||||||
public description: string = 'Discord RPC plugin for Cider';
|
|
||||||
public version: string = '0.0.1';
|
|
||||||
public author: string = 'vapormusic / Cider Collective';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs on plugin load (Currently run on application start)
|
* Runs on plugin load (Currently run on application start)
|
||||||
*/
|
*/
|
||||||
constructor(app: any, store: any) {
|
constructor(_app: any, store: any) {
|
||||||
this._app = app;
|
DiscordRichPresence._store = store
|
||||||
this._store = store
|
console.debug(`[Plugin][${this.name}] Loading Complete.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs on app ready
|
* Runs on app ready
|
||||||
*/
|
*/
|
||||||
onReady(win: any): void {
|
onReady(_win: any): void {
|
||||||
this._win = win;
|
this.connect((DiscordRichPresence._store.general.discord_rpc == 1) ? '911790844204437504' : '886578863147192350');
|
||||||
this.connect((this._store.general.discord_rpc == 1) ? '911790844204437504' : '886578863147192350');
|
console.debug(`[Plugin][${this.name}] Ready.`);
|
||||||
// electron.ipcMain.on("forceUpdateRPC", (event, attributes : object) => {
|
|
||||||
// this.updateActivity(attributes)
|
|
||||||
// });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs on app stop
|
* Runs on app stop
|
||||||
*/
|
*/
|
||||||
onBeforeQuit(): void {
|
onBeforeQuit(): void {
|
||||||
console.log('Example plugin stopped');
|
console.debug(`[Plugin][${this.name}] Stopped.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -209,5 +176,4 @@ export default class DiscordRPCPlugin {
|
||||||
onNowPlayingItemDidChange(attributes: object): void {
|
onNowPlayingItemDidChange(attributes: object): void {
|
||||||
this.updateActivity(attributes)
|
this.updateActivity(attributes)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,7 @@ export default class MPRIS {
|
||||||
/**
|
/**
|
||||||
* Blocks non-linux systems from running this plugin
|
* Blocks non-linux systems from running this plugin
|
||||||
* @private
|
* @private
|
||||||
|
* @decorator
|
||||||
*/
|
*/
|
||||||
private static linuxOnly(_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {
|
private static linuxOnly(_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {
|
||||||
if (process.platform !== 'linux') {
|
if (process.platform !== 'linux') {
|
||||||
|
@ -53,7 +54,6 @@ export default class MPRIS {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -157,7 +157,7 @@ export default class MPRIS {
|
||||||
*/
|
*/
|
||||||
constructor(app: any, _store: any) {
|
constructor(app: any, _store: any) {
|
||||||
this._app = app;
|
this._app = app;
|
||||||
console.log(`[${this.name}] plugin loaded`);
|
console.debug(`[Plugin][${this.name}] Loading Complete.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -165,7 +165,7 @@ export default class MPRIS {
|
||||||
*/
|
*/
|
||||||
onReady(win: any): void {
|
onReady(win: any): void {
|
||||||
this._win = win;
|
this._win = win;
|
||||||
console.log(`[${this.name}] plugin ready`);
|
console.debug(`[Plugin][${this.name}] Ready.`);
|
||||||
this.connect()
|
this.connect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,7 +173,7 @@ export default class MPRIS {
|
||||||
* Runs on app stop
|
* Runs on app stop
|
||||||
*/
|
*/
|
||||||
onBeforeQuit(): void {
|
onBeforeQuit(): void {
|
||||||
console.log(`[${this.name}] plugin stopped`);
|
console.debug(`[Plugin][${this.name}] Stopped.`);
|
||||||
this.clearState()
|
this.clearState()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,11 @@
|
||||||
"paths": {
|
"paths": {
|
||||||
"*": ["node_modules/*"]
|
"*": ["node_modules/*"]
|
||||||
},
|
},
|
||||||
"allowSyntheticDefaultImports": true
|
"allowSyntheticDefaultImports": true,
|
||||||
|
"typeRoots": [
|
||||||
|
"node_modules/musickit-typescript",
|
||||||
|
"node_modules/@types"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"src/**/*"
|
"src/**/*"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue