-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.ts
105 lines (94 loc) · 1.92 KB
/
Client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
class Client {
token: string;
name: string;
room: string;
color: string;
uri: string;
mppClient: WebSocket;
constructor(
token: string,
name: string,
room: string,
color: string,
uri: string
) {
this.token = token;
this.room = room;
this.color = color;
this.uri = uri;
this.name = name;
this.mppClient = new WebSocket(this.uri);
setInterval(() => {
this.sendReq([{ m: 't', e: Date.now() }]);
}, 15000);
}
isConnected() {
return this.mppClient.readyState == 1;
}
isDisconnected() {
return this.mppClient.readyState == 3;
}
sendReq(
req: [
{
m:
| 'a'
| 'hi'
| 'bye'
| 'userset'
| 'm'
| 'kickban'
| 'ch'
| 't'
| 'chown'
| 'unban'
| 'dm';
message?: string;
x?: number;
y?: number;
token?: string;
_id?: string;
e?: number;
set?: { name?: string; color?: string };
ms?: number;
}
]
) {
if (this.isConnected()) this.mppClient.send(JSON.stringify(req));
}
chat(a: string) {
this.sendReq([{ m: 'a', message: a }]);
}
moveMouse(x: number, y: number) {
this.sendReq([{ m: 'm', x: x, y: y }]);
}
userset(name = 'MPP Bot', color = '#ff0000') {
this.sendReq([{ m: 'userset', set: { name, color } }]);
}
connect() {
console.log(`Connected to ${this.uri}.`);
this.sendReq([{ m: 'hi', token: this.token }]);
this.sendReq([{ m: 'ch', _id: this.room }]);
this.userset(this.name, this.color);
}
disconnect() {
console.log('Disconnected.');
this.mppClient.close();
}
dropCrown() {
this.sendReq([{ m: 'chown' }]);
}
giveCrown(identifier: string) {
this.sendReq([{ m: 'chown', _id: identifier }]);
}
ban(identifier: string) {
this.sendReq([{ m: 'kickban', _id: identifier }]);
}
unban(identifier: string) {
this.sendReq([{ m: 'unban', _id: identifier }]);
}
dm(identifier: string, a: string) {
this.sendReq([{ m: 'dm', message: a, _id: identifier }]);
}
}
export default Client;