dead8309 / Kizzy

Discord Rich Presence for Android. Made with jetpack compose and material3

Home Page:https://kizzy.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

In javascript for Android

sebastianjnuwu opened this issue · comments

Hello, would you implement it in javascript nodejs? I have a javascript app that accepts js plugins, I wanted to implement riche presence in it, but the existing libs need discord desktop

For javscript, You can copy all the codes from the gateway folder from kizzy-demo-ts repository.

It has some basic things implemented, you can add the rest by yourself.

If by nodejs you meant your app won't be able to access browser apis then you just need to replace browser Websocket with Websocket from ws library.

https://github.com/dead8309/kizzy-demo-ts/blob/6a472f6266030d929664a5d82fbd4325913eb19d/lib/gateway/transport/WebsocketTransport.ts#L7

For javscript, You can copy all the codes from the gateway folder from kizzy-demo-ts repository.

It has some basic things implemented, you can add the rest by yourself.

If by nodejs you meant your app won't be able to access browser apis then you just need to replace browser Websocket with Websocket from ws library.

https://github.com/dead8309/kizzy-demo-ts/blob/6a472f6266030d929664a5d82fbd4325913eb19d/lib/gateway/transport/WebsocketTransport.ts#L7

This repository is being of great help because I still hadn't found anything about it unfortunately I didn't understand the functions very much because I'm not familiar with the typescript

Looking at your code, I was able to contact myself in Javascript but I didn't get where I wanted ;) the custom rich precense can you help me? Follow the code

import WebSocket from 'ws';

const OpCodes = {
  Hello: 10,
  Heartbeat: 1,
  Dispatch: 0,
  Reconnect: 7,
  InvalidSession: 9,
};

const token = 'TOKEN';

const ws = new WebSocket('wss://gateway.discord.gg/?v=9&encoding=json');

let seq = 0;
let session_id = "";
let resume_gateway_url = "";

ws.on("open", () => {
  console.log("Socket Opened");
  ws.send(send(identify(token)));
});

ws.on("message", (event) => {
 const message = event.toString("utf8");
 const { t, op, d } = JSON.parse(message);

  switch (op) {
    case OpCodes.Hello:
     console.log('Got op 10 HELLO');
     const { heartbeat_interval } = d;
     heartbeat(heartbeat_interval, seq);
     send(identify(token));
      break;

        case OpCodes.Heartbeat:
          console.log("Got Op 1 Sending Heartbeat");
          ws.send({
            op: OpCodes.Heartbeat,
            d: seq === 0 ? null : seq.toString(),
          });
          break;

        case OpCodes.Dispatch:
          if (!t) return;
          switch (t) {
            case 'READY':
              console.log('Received READY event');
              const { user } = d;
              ws.emit('open', user);
              session_id = d.session_id;
              resume_gateway_url = d.resume_gateway_url;
              break;
            default:
              break;
          }
          break;

        case OpCodes.Reconnect:
          console.log("Got op 7 Closing and Reconnecting Session");
          ws.close(4000);
          break;

        case OpCodes.InvalidSession:
          console.log("Got Op 9 Reconnect Failed");
          ws.send(identify(token));
          break;

        default:
          console.log("Received unknown message:", parsedData);
      }
});


function heartbeat(ms, seq) {
  setInterval(() => {
    return setInterval(() => {
     send({ op: OpCodes.Heartbeat, d: seq });
    }, ms);
  });
};

function send(data) {
  ws.send(JSON.stringify(data))
}

async function identify(token) {
  if (!token) {
    new Error('Token Not Specified')
  }
  
  return {
    op: 2,
    d: {
      token: token,
      properties: {
        $os: "disco",
        $browser: "chrome",
        $device: "chrome",
      },
      compress: false,
      capabilities: 65,
      largeThreshold: 100
    }
  };
};

Uh, I think you only need to import ws and replace the Websocket with one from ws then you can just use it like what's described in the readme file

import { Client } from "@dead8309/kizzy-rpc";

const client = new Client();

client.on("ready", () => {
    client.user?.setActivity({
        name: "Hey"
    });
});

client.login(TOKEN);

I don't understand anything ,-,

Uh, I think you only need to import ws and replace the Websocket with one from ws then you can just use it like what's described in the readme file

import { Client } from "@dead8309/kizzy-rpc";

const client = new Client();

client.on("ready", () => {
    client.user?.setActivity({
        name: "Hey"
    });
});

client.login(TOKEN);

The token has the scope of everything in rpc:

$ tsnd --respawn --transpile-only rpc.ts [INFO] 01:13:53 ts-node-dev ver. 2.0.0 (using ts-node ver. 10.9.1, typescript ver. 5.1.6)
Socket Opened
Got op 10 HELLO
Authentication failed.

Socket Opened Got op 10 HELLO Authentication failed.

You need to pass in your token

Socket Opened Got op 10 HELLO Autenticação falhou.

Você precisa passar seu token

But I passed, the user token takes it, right? Can you test the code you made and see if the rpc shows up? I made some changes see there

But I passed, the user token takes it, right? Can you test the code you made and see if the rpc shows up? I made some changes see there

Yes it works fine. maybe you did something wrong ?

But I passed, the user token takes it, right? Can you test the code you made and see if the rpc shows up? I made some changes see there

Yes it works fine. maybe you did something wrong ?

So what I did:
• I made the authorization link and marked everything from rpc in discord Developers
• I took the authorization link code and generated the user token
• I put it in the code and ran yarn and yarn dev

Gateway needs user token not oauth token

Gateway needs user token not oauth token

? When you logon to your rpc site it says that user is undefined

I think that's why it's failing authentication all the time;
Putting the email and password on your correct website comes to me:
Screenshot_20230713-113708

const client = new Client();

client.on("ready", (user) => {
console.log(user) // idl
    client.user?.setActivity({
        name: "Hey"
    });
});

Login through email and password is still experimental in that site, you need to use token

O login por e-mail e senha ainda é experimental nesse site, você precisa usar token

Using the token it says error "4000" or "4003" that it was not possible to connect like my code