I have an error for discord.js rpc! Can someone please help me?

2.3k Views Asked by At

This is my error:

(node:42087) UnhandledPromiseRejectionWarning: Error: Could not connect
    at Socket.onerror (/Users/vinesh/RPC/node_modules/discord-rpc/src/transports/ipc.js:32:16)
    at Object.onceWrapper (events.js:422:26)
    at Socket.emit (events.js:315:20)
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:42087) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:42087) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

My code:

const RPC = require('discord-rpc');
const client = new RPC.Client({ transport: 'ipc' });
client.on('ready', () =>
{
    client.request('SET_ACTIVITY', {
        pid: process.pid,
        activity: {
            assets: {
                large_image: 'Coding'
            },
            details: 'Coding',
            buttons: [
                { label: 'Emperor', url: 'https://dsc.gg/invite-emperor' },
                { label: 'https://dsc.gg/invite-emperor', url: 'https://dsc.gg/invite-emperor' }
            ]
        }
    });
    console.log('started!');
});
client.login({ clientId: 'My_ID' });

How do I fix this? Is it because the code is old? I don't know why it does that. I did npm init -y, npm install discord, and npm install discord-rpc.

1

There are 1 best solutions below

0
On BEST ANSWER

If you are running the web browser version of Discord, RPC will not work. Instead download Discord for Desktop which is here.

If you are running Discord desktop then try this code. Also remember to add your assets in https://discord.com/developers/applications/Application_ID/rich-presence/assets and make sure they are the same name in application and code. You need large_text so don't remove it.

var rpc = require("discord-rpc")
const client = new rpc.Client({ transport: 'ipc' })
client.on('ready', () => {
    client.request('SET_ACTIVITY', {
        pid: process.pid,
        activity : {
            details : "Coding",
            assets : {
                large_image : "Your Image",
                large_text : "Your Status"
            },
            buttons : [{label : "Emperor" , url : "https://dsc.gg/invite-emperor"},{label : "Invite 2?",url : "https://dsc.gg/invite-emperor"}]
        }
    })
})
client.login({ clientId : "CLIENT_ID" }).catch(console.error);