Skip to content

Commit c08038c

Browse files
authored
Merge pull request #3 from Folody-Team/1-problem-from-discord-gateway
1 problem from discord gateway
2 parents e9d636a + a2a44dd commit c08038c

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

packages/@core/events/READY.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const READY = {
2+
event: 'READY',
3+
callback: (...args: any[]) => {
4+
console.log(args);
5+
},
6+
};

packages/client/index.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,41 @@ import ws = require('ws');
1313
import {WebSocket} from '../server/WebSocket';
1414
// eslint-disable-next-line new-cap, object-curly-spacing, no-unused-vars
1515
import {DiscordGateway} from '../@api/link';
16+
import {eventsType} from '../constants';
1617
import {setWsHeartbeat} from "ws-heartbeat/client";
18+
import {EventEmitter} from "events";
19+
1720

1821
type options = {
1922
token: string;
2023
intents: string[] | number[];
2124
};
2225

26+
type ValueOf<T> = T[keyof T];
27+
28+
export declare interface Client extends EventEmitter {
29+
on(event: ValueOf<eventsType>, listener: (...args: any[]) => void): this;
30+
}
2331
// eslint-disable-next-line require-jsdoc
24-
export class Client {
32+
export class Client extends EventEmitter {
33+
private ws: WebSocket | undefined;
2534
private options: options | undefined;
2635
private gateway: string = DiscordGateway.init(10);
36+
private data: string = '{}';
2737

2838
// eslint-disable-next-line require-jsdoc
2939
/**
3040
*
3141
* @param option
3242
*/
3343
constructor(option: options) {
44+
super();
3445
Object.assign(this, {options: option});
3546
}
3647
// eslint-disable-next-line require-jsdoc
37-
public login(): Promise<void> {
48+
public 'sự kiện' = this.on;
49+
// eslint-disable-next-line require-jsdoc
50+
public 'kích hoạt'(): Promise<void> {
3851
return new Promise((res, rej) => {
3952
const {token, intents} = this.options!;
4053
this.active(token, intents);
@@ -57,15 +70,13 @@ export class Client {
5770
const {op, d, t} = JSON.parse(data.toString());
5871
switch (op) {
5972
case 0:
60-
console.log('Authentication');
6173
break;
6274
case 10:
6375
const {heartbeat_interval} = d;
6476
this.keepAlive(ws, heartbeat_interval);
6577
}
66-
if (t === 'MESSAGE_CREATE') {
67-
console.log(JSON.parse(data.toString()));
68-
}
78+
79+
if (t) this.data = data.toString();
6980
}
7081
/**
7182
*
@@ -107,10 +118,12 @@ export class Client {
107118
private async active(token: string, intents: string[] | number[]) {
108119
const ws = new WebSocket(this.gateway);
109120
const payload = await this.payload(token, intents);
121+
this.ws = ws;
110122
ws.on('open', () => this.open(ws, payload));
111123
ws.on('message', (data) => {
112124
this.message(ws, data, payload);
113-
console.log(JSON.parse(data.toString()));
125+
const {t} = JSON.parse(data.toString());
126+
if (t) this.emit(eventsType[t as keyof typeof eventsType], JSON.parse(data.toString()) as ws.Data);
114127
});
115128
}
116129
};

packages/constants/eventsType.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export interface eventsType {
2+
'READY': 'Đã kích hoạt';
3+
'MESSAGE_CREATE': 'Tạo tin nhắn';
4+
};
5+
6+
export const eventsType: eventsType = {
7+
'READY': 'Đã kích hoạt',
8+
'MESSAGE_CREATE': 'Tạo tin nhắn',
9+
};

packages/constants/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './eventsType';

packages/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ const client = new Client({
66
intents: [5, 1, 3],
77
});
88

9-
client.login();
9+
client['sự kiện']('Tạo tin nhắn', (message) => {
10+
console.log(message);
11+
console.log('true');
12+
});
13+
14+
client['kích hoạt']();

packages/key/keyOf.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)