-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (121 loc) · 3.19 KB
/
index.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const ws = require("ws");
const { Intents } = require("./src/Intents");
const { ActivityType } = require("./src/ActivityType");
class Client {
finalIntents = 0;
gateway = null;
hearthbeat = 0;
properties = {};
token = "";
callbacks = [];
getGuildMembersCallback = null;
members = []
constructor(token, intents, properties) {
if (!Array.isArray(intents)) {
throw new Error("Please provide a valid array of intentions");
}
for (const intent of intents) {
this.finalIntents |= intent;
}
this.token = token;
this.properties = properties;
this.gateway = new ws("wss://gateway.discord.gg/?v=10&encoding=json");
this.gateway.on("message", (message) => {
const json = JSON.parse(message.toString());
if (json.op === 10) {
this.hearthbeat = json.d.heartbeat_interval;
this.StartHearthbeat();
} else if (json.op === 0) {
this.callbacks.forEach((element) => {
if (element.event === json.t) {
element.callbacks.forEach((callback) => {
callback(json.d);
});
}
if (json.t == "GUILD_MEMBERS_CHUNK") {
if (json.d.chunk_index == 0) this.members = []
json.d.members.forEach(member => {
member.presence = json.d.presences.find(obj => obj.user.id === member.user.id)
this.members.push(member)
})
if (json.d.chunk_index + 1 == json.d.chunk_count) {
this.getGuildMembersCallback(this.members)
}
}
});
}
});
this.gateway.on("open", () => {
this.login();
});
}
login() {
const payload = {
op: 2,
d: {
token: this.token,
intents: this.finalIntents,
properties: this.properties || {
os: "linux",
browser: "clyde-gateway",
device: "clyde-gateway",
},
},
};
this.gateway.send(JSON.stringify(payload));
}
on(event, callback) {
let exist = false;
this.callbacks.forEach((element) => {
if (element.event === event) {
element.callbacks.push(callback);
exist = true;
}
});
if (!exist) this.callbacks.push({ event: event, callbacks: [callback] });
}
updatePresence(status, activity, afk = false) {
this.gateway.send(
JSON.stringify({
op: 3,
d: {
since: Date.now(),
activities: [activity],
status: status,
afk: afk,
},
})
);
}
getGuildMembers({ guildId, query, limit, user_ids }, callback) {
this.getGuildMembersCallback = callback
this.gateway.send(
JSON.stringify({
op: 8,
d: {
guild_id: guildId,
query: query || "",
limit: limit || 0,
user_ids: user_ids || null,
presences: true
},
})
);
}
async StartHearthbeat() {
await new Promise((r) => setTimeout(r, this.hearthbeat * 0.8));
const payload = {
op: 1,
d: {
heartbeat_interval: this.hearthbeat,
},
};
this.gateway.send(JSON.stringify(payload));
this.StartHearthbeat();
}
}
module.exports = {
Client,
Intents,
ActivityType
};