How can I add Discord RPC to an electron application?

143 Views Asked by At

I've been working on a game using electron and am wanting to add Discord RPC to it, my problem is that the npm package discord-rpc hasn't been updated in 2 years and appears to be the only package that could work. I couldn't find any form of docs for it do I had to try and figure everything out myself, this is what I've written:

    const { app, BrowserWindow } = require('electron')
    const path = require('path');
    const url = require('url');
    const DiscordRPC = require('discord-rpc');
    const clientId = '1179182104257499206';
    DiscordRPC.register(clientId);


    const createWindow = () => {
     const win = new BrowserWindow({
      autoHideMenuBar: true,
      width: 1280,
      height:720,
      icon: path.join(__dirname, 'icons/icon.png'),
      cookiesPath: "~/.local/share/DTF"
     })
    
    win.loadFile('french.html')
    }
    
    app.whenReady().then(() => {
     createWindow()
    });
    
    const rpc = new DiscordRPC.Client({ transport: 'ipc' });
    const startTimestamp = new Date();

    async function setActivity() {
      if (!rpc || !mainWindow) {
        return;
      }
    
      rpc.setActivity({
        details: `Politics Are Funny`,
        state: 'Dodging The French',
        startTimestamp,
        largeImageKey: 'logo',
        largeImageText: 'Frenchman are not sexy',
        smallImageKey: 'logo_large',
        smallImageText: 'Frenchman are not sexy',
        instance: false,
      });
    }

    rpc.on('ready', () => {
      setActivity();

      // activity can only be set every 15 seconds
      setInterval(() => {
        setActivity();
      }, 15e3);
    });
    
    rpc.login({ clientId }).catch(console.error);

I get zero errors in my JavaScript console when I launch the application, the RPC just doesn't show in Discord. I've tried every version of Discord that ships on Linux and it doesn't fix the issue either.

0

There are 0 best solutions below