small cleanup to how discordrpc does things, fixed the icon for linux

This commit is contained in:
Core 2022-01-07 20:11:30 +00:00
parent eca7d4c78b
commit c85b8d22ff
No known key found for this signature in database
GPG key ID: 1B77805746C47C28
2 changed files with 28 additions and 24 deletions

View file

@ -38,7 +38,7 @@ const CiderBase = {
let win = null
const options = {
icon: join(__dirname, `../../resources/icons/icon.ico`),
icon: join(__dirname, `../../resources/icons/icon.` + (process.platform === "win32" ? "ico" : "png")),
width: mainWindowState.width,
height: mainWindowState.height,
x: mainWindowState.x,

View file

@ -9,7 +9,7 @@ module.exports = {
*/
connect: function (clientId) {
app.discord = {isConnected: false};
if (app.cfg.get('general.discord_rpc') == 0) 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.
const client = new DiscordRPC.Client({transport: "ipc"});
@ -24,11 +24,6 @@ module.exports = {
app.discord.on('ready', () => {
console.log(`[DiscordRPC][connect] Successfully Connected to Discord. Authed for user: ${client.user.username} (${client.user.id})`);
if (app.discord.activityCache) {
client.setActivity(app.discord.activityCache).catch((e) => console.error(e));
app.discord.activityCache = null;
}
})
// Handles Errors
@ -63,11 +58,10 @@ module.exports = {
if (app.cfg.get('general.discord_rpc') == 0) return;
if (!app.discord.isConnected) {
this.connect()
app.discord.clearActivity().catch((e) => console.error(`[DiscordRPC][updateActivity] ${e}`));
return;
}
if (!app.discord.isConnected) return;
// console.log('[DiscordRPC][updateActivity] Updating Discord Activity.')
const listenURL = `https://applemusicelectron.com/p?id=${attributes.playParams.id}`
@ -89,35 +83,43 @@ module.exports = {
if (ActivityObject.largeImageKey == "" || ActivityObject.largeImageKey == null) {
ActivityObject.largeImageKey = (app.cfg.get("general.discord_rpc") == 1) ? "cider" : "logo"
}
// console.log(`[LinkHandler] Listening URL has been set to: ${listenURL}`);
// Remove the pause/play icon and test for clear activity on pause
if (app.cfg.get('general.discordClearActivityOnPause') == 1) {
delete ActivityObject.smallImageKey
delete ActivityObject.smallImageText
}
// Check all the values work
// 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
if (ActivityObject.details.length > 128) {
AcitivityObject.details = ActivityObject.details.substring(0, 125) + '...'
ActivityObject.details = ActivityObject.details.substring(0, 125) + '...'
}
// Clear if if needed
// Check if its pausing (false) or playing (true)
if (!attributes.status) {
if (app.cfg.get('general.discordClearActivityOnPause') == 1) {
app.discord.clearActivity().catch((e) => console.error(`[DiscordRPC][clearActivity] ${e}`));
ActivityObject = null
} else
{
} else {
delete ActivityObject.startTimestamp
delete ActivityObject.endTimestamp
ActivityObject.smallImageKey = 'pause'
@ -125,10 +127,12 @@ module.exports = {
}
}
if (ActivityObject) {
if (ActivityObject && ActivityObject !== app.discord.activityCache && ActivityObject.details && ActivityObject.state) {
try {
// console.log(`[DiscordRPC][setActivity] Setting activity to ${JSON.stringify(ActivityObject)}`);
app.discord.setActivity(ActivityObject)
app.discord.activityCache = ActivityObject
} catch (err) {
console.error(`[DiscordRPC][setActivity] ${err}`)
}