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