This commit is contained in:
parent
0da130995c
commit
cc9693ebd7
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "usetether",
|
||||
"version": "1.1.6",
|
||||
"version": "1.1.7",
|
||||
"author": "Braydon (Rainnny) <braydonrainnny@gmail.com>",
|
||||
"description": "An API designed to provide real-time access to a user's Discord data.",
|
||||
"keywords": [
|
||||
|
@ -2,6 +2,7 @@ import { useEffect, useState } from "react";
|
||||
import { Snowflake } from "@/types/snowflake";
|
||||
import { TetherConfig } from "@/types/config";
|
||||
import { DiscordUser } from "@/types/user";
|
||||
import { SocketPacket, UserStatusPacket } from "@/types/socket";
|
||||
|
||||
export const useTetherWS = (
|
||||
snowflake: Snowflake,
|
||||
@ -11,7 +12,7 @@ export const useTetherWS = (
|
||||
}
|
||||
): DiscordUser | undefined => {
|
||||
const url: string = `ws${secure && "s"}://${endpoint}/gateway`;
|
||||
const [user] = useState<DiscordUser | undefined>();
|
||||
const [user, setUser] = useState<DiscordUser | undefined>();
|
||||
|
||||
useEffect(() => {
|
||||
// Prevent from running on the server
|
||||
@ -26,8 +27,10 @@ export const useTetherWS = (
|
||||
const connect = () => {
|
||||
console.log("[Tether] Connecting to the WebSocket server...");
|
||||
socket = new WebSocket(url); // Connect to the gateway
|
||||
|
||||
// Track the user when the WS connects
|
||||
socket.addEventListener("open", () => {
|
||||
socket.send(JSON.stringify({ op: 0, snowflake: snowflake })); // Track the user
|
||||
socket.send(JSON.stringify({ op: 0, snowflake: snowflake }));
|
||||
console.log(
|
||||
"[Tether] WebSocket connection established!",
|
||||
snowflake
|
||||
@ -36,7 +39,13 @@ export const useTetherWS = (
|
||||
socket.addEventListener("close", connect); // Reconnect on close
|
||||
|
||||
socket.addEventListener("message", (event) => {
|
||||
console.log("data:", event.data);
|
||||
const packet: SocketPacket = JSON.parse(
|
||||
event.data
|
||||
) as SocketPacket;
|
||||
if (packet.op === 1) {
|
||||
setUser((packet as UserStatusPacket).user);
|
||||
console.log("user status update", user);
|
||||
}
|
||||
});
|
||||
};
|
||||
connect();
|
||||
|
@ -1,6 +1,15 @@
|
||||
import { DiscordUser } from "@/types/user";
|
||||
|
||||
export type SocketPacket = {
|
||||
/**
|
||||
* The OP code for this packet.
|
||||
*/
|
||||
op: number;
|
||||
};
|
||||
|
||||
export type UserStatusPacket = SocketPacket & {
|
||||
/**
|
||||
* The user the status is for.
|
||||
*/
|
||||
user: DiscordUser;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user