Skip to content

Commit

Permalink
Merge pull request #3 from Folody-Team/1-problem-from-discord-gateway
Browse files Browse the repository at this point in the history
1 problem from discord gateway
  • Loading branch information
phatdev12 authored Aug 15, 2022
2 parents e9d636a + a2a44dd commit c08038c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
6 changes: 6 additions & 0 deletions packages/@core/events/READY.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const READY = {
event: 'READY',
callback: (...args: any[]) => {
console.log(args);
},
};
27 changes: 20 additions & 7 deletions packages/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,41 @@ import ws = require('ws');
import {WebSocket} from '../server/WebSocket';
// eslint-disable-next-line new-cap, object-curly-spacing, no-unused-vars
import {DiscordGateway} from '../@api/link';
import {eventsType} from '../constants';
import {setWsHeartbeat} from "ws-heartbeat/client";
import {EventEmitter} from "events";


type options = {
token: string;
intents: string[] | number[];
};

type ValueOf<T> = T[keyof T];

export declare interface Client extends EventEmitter {
on(event: ValueOf<eventsType>, listener: (...args: any[]) => void): this;
}
// eslint-disable-next-line require-jsdoc
export class Client {
export class Client extends EventEmitter {
private ws: WebSocket | undefined;
private options: options | undefined;
private gateway: string = DiscordGateway.init(10);
private data: string = '{}';

// eslint-disable-next-line require-jsdoc
/**
*
* @param option
*/
constructor(option: options) {
super();
Object.assign(this, {options: option});
}
// eslint-disable-next-line require-jsdoc
public login(): Promise<void> {
public 'sự kiện' = this.on;
// eslint-disable-next-line require-jsdoc
public 'kích hoạt'(): Promise<void> {
return new Promise((res, rej) => {
const {token, intents} = this.options!;
this.active(token, intents);
Expand All @@ -57,15 +70,13 @@ export class Client {
const {op, d, t} = JSON.parse(data.toString());
switch (op) {
case 0:
console.log('Authentication');
break;
case 10:
const {heartbeat_interval} = d;
this.keepAlive(ws, heartbeat_interval);
}
if (t === 'MESSAGE_CREATE') {
console.log(JSON.parse(data.toString()));
}

if (t) this.data = data.toString();
}
/**
*
Expand Down Expand Up @@ -107,10 +118,12 @@ export class Client {
private async active(token: string, intents: string[] | number[]) {
const ws = new WebSocket(this.gateway);
const payload = await this.payload(token, intents);
this.ws = ws;
ws.on('open', () => this.open(ws, payload));
ws.on('message', (data) => {
this.message(ws, data, payload);
console.log(JSON.parse(data.toString()));
const {t} = JSON.parse(data.toString());
if (t) this.emit(eventsType[t as keyof typeof eventsType], JSON.parse(data.toString()) as ws.Data);
});
}
};
9 changes: 9 additions & 0 deletions packages/constants/eventsType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface eventsType {
'READY': 'Đã kích hoạt';
'MESSAGE_CREATE': 'Tạo tin nhắn';
};

export const eventsType: eventsType = {
'READY': 'Đã kích hoạt',
'MESSAGE_CREATE': 'Tạo tin nhắn',
};
1 change: 1 addition & 0 deletions packages/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './eventsType';
7 changes: 6 additions & 1 deletion packages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ const client = new Client({
intents: [5, 1, 3],
});

client.login();
client['sự kiện']('Tạo tin nhắn', (message) => {
console.log(message);
console.log('true');
});

client['kích hoạt']();
Empty file added packages/key/keyOf.ts
Empty file.

0 comments on commit c08038c

Please sign in to comment.