sorry for doing this.
This commit is contained in:
parent
5fc82a8bc7
commit
b3db294485
106 changed files with 86 additions and 259 deletions
|
@ -1,121 +0,0 @@
|
|||
const {app} = require('electron'),
|
||||
DiscordRPC = require('discord-rpc'),
|
||||
{initAnalytics} = require('../utils');
|
||||
initAnalytics();
|
||||
|
||||
module.exports = {
|
||||
connect: function (clientId) {
|
||||
app.discord = {isConnected: false};
|
||||
if (!app.cfg.get('general.discordRPC')) return;
|
||||
|
||||
DiscordRPC.register(clientId) // Apparently needed for ask to join, join, spectate etc.
|
||||
const client = new DiscordRPC.Client({ transport: "ipc" });
|
||||
app.discord = Object.assign(client,{error: false, activityCache: null, isConnected: false});
|
||||
|
||||
// Login to Discord
|
||||
app.discord.login({ clientId })
|
||||
.then(() => {
|
||||
app.discord.isConnected = true;
|
||||
})
|
||||
.catch((e) => console.error(`[DiscordRPC][connect] ${e}`));
|
||||
|
||||
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
|
||||
app.discord.on('error', err => {
|
||||
console.error(`[DiscordRPC] ${err}`);
|
||||
this.disconnect()
|
||||
app.discord.isConnected = false;
|
||||
});
|
||||
},
|
||||
|
||||
disconnect: function () {
|
||||
if (!app.cfg.get('general.discordRPC') || !app.discord.isConnected) return;
|
||||
|
||||
try {
|
||||
app.discord.destroy().then(() => {
|
||||
app.discord.isConnected = false;
|
||||
console.verbose('[DiscordRPC][disconnect] Disconnected from discord.')
|
||||
}).catch((e) => console.error(`[DiscordRPC][disconnect] ${e}`));
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
},
|
||||
|
||||
updateActivity: function (attributes) {
|
||||
if (!app.cfg.get('general.discordRPC') || app.cfg.get('general.incognitoMode')) return;
|
||||
|
||||
if (!app.discord.isConnected) {
|
||||
this.connect()
|
||||
}
|
||||
|
||||
if (!app.discord.isConnected) return;
|
||||
|
||||
console.verbose('[DiscordRPC][updateActivity] Updating Discord Activity.')
|
||||
|
||||
const listenURL = `https://applemusicelectron.com/p?id=${attributes.playParams.id}`
|
||||
|
||||
let ActivityObject = {
|
||||
details: attributes.name,
|
||||
state: `by ${attributes.artistName}`,
|
||||
startTimestamp: attributes.startTime,
|
||||
endTimestamp: attributes.endTime,
|
||||
largeImageKey: ((app.cfg.get('general.discordRPC') === 'am-title') ? 'apple' : 'cider'),
|
||||
largeImageText: attributes.albumName,
|
||||
smallImageKey: (attributes.status ? 'play' : 'pause'),
|
||||
smallImageText: (attributes.status ? 'Playing': 'Paused'),
|
||||
instance: true,
|
||||
buttons: [
|
||||
{label: "Listen on Cider", url: listenURL},
|
||||
]
|
||||
};
|
||||
console.verbose(`[LinkHandler] Listening URL has been set to: ${listenURL}`);
|
||||
|
||||
if (app.cfg.get('general.discordClearActivityOnPause')) {
|
||||
delete ActivityObject.smallImageKey
|
||||
delete ActivityObject.smallImageText
|
||||
}
|
||||
|
||||
// Check all the values work
|
||||
if (!((new Date(attributes.endTime)).getTime() > 0)) {
|
||||
delete ActivityObject.startTimestamp
|
||||
delete ActivityObject.endTimestamp
|
||||
}
|
||||
if (!attributes.artistName) {
|
||||
delete ActivityObject.state
|
||||
}
|
||||
if (!ActivityObject.largeImageText || ActivityObject.largeImageText.length < 2) {
|
||||
delete ActivityObject.largeImageText
|
||||
}
|
||||
|
||||
// Clear if if needed
|
||||
if (!attributes.status) {
|
||||
if (app.cfg.get('general.discordClearActivityOnPause')) {
|
||||
app.discord.clearActivity().catch((e) => console.error(`[DiscordRPC][clearActivity] ${e}`));
|
||||
ActivityObject = null
|
||||
} else {
|
||||
delete ActivityObject.startTimestamp
|
||||
delete ActivityObject.endTimestamp
|
||||
ActivityObject.smallImageKey = 'pause'
|
||||
ActivityObject.smallImageText = 'Paused'
|
||||
}
|
||||
}
|
||||
|
||||
if (ActivityObject) {
|
||||
try {
|
||||
console.verbose(`[DiscordRPC][setActivity] Setting activity to ${JSON.stringify(ActivityObject)}`);
|
||||
app.discord.setActivity(ActivityObject)
|
||||
} catch (err) {
|
||||
console.error(`[DiscordRPC][setActivity] ${err}`)
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue