@@ -13,28 +13,41 @@ import ws = require('ws');
13
13
import { WebSocket } from '../server/WebSocket' ;
14
14
// eslint-disable-next-line new-cap, object-curly-spacing, no-unused-vars
15
15
import { DiscordGateway } from '../@api/link' ;
16
+ import { eventsType } from '../constants' ;
16
17
import { setWsHeartbeat } from "ws-heartbeat/client" ;
18
+ import { EventEmitter } from "events" ;
19
+
17
20
18
21
type options = {
19
22
token : string ;
20
23
intents : string [ ] | number [ ] ;
21
24
} ;
22
25
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
+ }
23
31
// eslint-disable-next-line require-jsdoc
24
- export class Client {
32
+ export class Client extends EventEmitter {
33
+ private ws : WebSocket | undefined ;
25
34
private options : options | undefined ;
26
35
private gateway : string = DiscordGateway . init ( 10 ) ;
36
+ private data : string = '{}' ;
27
37
28
38
// eslint-disable-next-line require-jsdoc
29
39
/**
30
40
*
31
41
* @param option
32
42
*/
33
43
constructor ( option : options ) {
44
+ super ( ) ;
34
45
Object . assign ( this , { options : option } ) ;
35
46
}
36
47
// 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 > {
38
51
return new Promise ( ( res , rej ) => {
39
52
const { token, intents} = this . options ! ;
40
53
this . active ( token , intents ) ;
@@ -57,15 +70,13 @@ export class Client {
57
70
const { op, d, t} = JSON . parse ( data . toString ( ) ) ;
58
71
switch ( op ) {
59
72
case 0 :
60
- console . log ( 'Authentication' ) ;
61
73
break ;
62
74
case 10 :
63
75
const { heartbeat_interval} = d ;
64
76
this . keepAlive ( ws , heartbeat_interval ) ;
65
77
}
66
- if ( t === 'MESSAGE_CREATE' ) {
67
- console . log ( JSON . parse ( data . toString ( ) ) ) ;
68
- }
78
+
79
+ if ( t ) this . data = data . toString ( ) ;
69
80
}
70
81
/**
71
82
*
@@ -107,10 +118,12 @@ export class Client {
107
118
private async active ( token : string , intents : string [ ] | number [ ] ) {
108
119
const ws = new WebSocket ( this . gateway ) ;
109
120
const payload = await this . payload ( token , intents ) ;
121
+ this . ws = ws ;
110
122
ws . on ( 'open' , ( ) => this . open ( ws , payload ) ) ;
111
123
ws . on ( 'message' , ( data ) => {
112
124
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 ) ;
114
127
} ) ;
115
128
}
116
129
} ;
0 commit comments