mirror of
https://git.sanhost.net/sanasol/hytale-f2p
synced 2026-02-26 18:41:48 -03:00
fix: discord RPC destroy error if not connected
This commit is contained in:
74
main.js
74
main.js
@@ -107,9 +107,41 @@ async function toggleDiscordRPC(enabled) {
|
|||||||
} else if (!enabled && discordRPC) {
|
} else if (!enabled && discordRPC) {
|
||||||
try {
|
try {
|
||||||
console.log('Disconnecting Discord RPC...');
|
console.log('Disconnecting Discord RPC...');
|
||||||
discordRPC.clearActivity();
|
|
||||||
|
// Check if Discord RPC is still connected before trying to use it
|
||||||
|
if (discordRPC && discordRPC.transport && discordRPC.transport.socket) {
|
||||||
|
// Add timeout to prevent hanging
|
||||||
|
const clearActivityPromise = discordRPC.clearActivity();
|
||||||
|
const timeoutPromise = new Promise((_, reject) =>
|
||||||
|
setTimeout(() => reject(new Error('Discord RPC clearActivity timeout')), 1000)
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await Promise.race([clearActivityPromise, timeoutPromise]);
|
||||||
await new Promise(r => setTimeout(r, 100));
|
await new Promise(r => setTimeout(r, 100));
|
||||||
discordRPC.destroy();
|
} catch (timeoutErr) {
|
||||||
|
console.log('Discord RPC clearActivity timed out:', timeoutErr.message);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log('Discord RPC already disconnected');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destroy - wrap in try-catch to handle library errors
|
||||||
|
if (discordRPC) {
|
||||||
|
try {
|
||||||
|
if (typeof discordRPC.destroy === 'function') {
|
||||||
|
const destroyPromise = discordRPC.destroy();
|
||||||
|
if (destroyPromise && typeof destroyPromise.catch === 'function') {
|
||||||
|
destroyPromise.catch(err => {
|
||||||
|
console.log('Discord RPC destroy error (ignored):', err.message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (destroyErr) {
|
||||||
|
console.log('Error destroying Discord RPC (ignored):', destroyErr.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
console.log('Discord RPC disconnected successfully');
|
console.log('Discord RPC disconnected successfully');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error disconnecting Discord RPC:', error.message);
|
console.error('Error disconnecting Discord RPC:', error.message);
|
||||||
@@ -424,9 +456,43 @@ async function cleanupDiscordRPC() {
|
|||||||
if (!discordRPC) return;
|
if (!discordRPC) return;
|
||||||
try {
|
try {
|
||||||
console.log('Cleaning up Discord RPC...');
|
console.log('Cleaning up Discord RPC...');
|
||||||
discordRPC.clearActivity();
|
|
||||||
|
// Check if Discord RPC is still connected before trying to use it
|
||||||
|
if (discordRPC && discordRPC.transport && discordRPC.transport.socket) {
|
||||||
|
// Add timeout to prevent hanging if Discord is unresponsive
|
||||||
|
const clearActivityPromise = discordRPC.clearActivity();
|
||||||
|
const timeoutPromise = new Promise((_, reject) =>
|
||||||
|
setTimeout(() => reject(new Error('Discord RPC clearActivity timeout')), 1000)
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await Promise.race([clearActivityPromise, timeoutPromise]);
|
||||||
await new Promise(r => setTimeout(r, 100));
|
await new Promise(r => setTimeout(r, 100));
|
||||||
discordRPC.destroy();
|
} catch (timeoutErr) {
|
||||||
|
console.log('Discord RPC clearActivity timed out, proceeding with cleanup:', timeoutErr.message);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log('Discord RPC already disconnected, skipping clearActivity');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destroy and cleanup - wrap in try-catch to handle library errors
|
||||||
|
if (discordRPC) {
|
||||||
|
try {
|
||||||
|
if (typeof discordRPC.destroy === 'function') {
|
||||||
|
// destroy() may return a promise that rejects, so handle it
|
||||||
|
const destroyPromise = discordRPC.destroy();
|
||||||
|
if (destroyPromise && typeof destroyPromise.catch === 'function') {
|
||||||
|
// If it's a promise, catch any rejections silently
|
||||||
|
destroyPromise.catch(err => {
|
||||||
|
console.log('Discord RPC destroy error (ignored):', err.message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (destroyErr) {
|
||||||
|
console.log('Error destroying Discord RPC client (ignored):', destroyErr.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
console.log('Discord RPC cleaned up successfully');
|
console.log('Discord RPC cleaned up successfully');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('Error cleaning up Discord RPC:', error.message);
|
console.log('Error cleaning up Discord RPC:', error.message);
|
||||||
|
|||||||
Reference in New Issue
Block a user