2022-02-02 11:46:42 +02:00
|
|
|
import EventEmitter from "events";
|
|
|
|
import zlib from "zlib";
|
|
|
|
import { WebSocket } from "ws";
|
|
|
|
import fetch from "node-fetch";
|
2022-02-15 14:24:46 +02:00
|
|
|
import { logger } from "./common.js";
|
|
|
|
|
|
|
|
const log = logger("log", "DiscordClient");
|
|
|
|
const logError = logger("error", "DiscordClient");
|
|
|
|
const logWarn = logger("warn", "DiscordClient");
|
2022-02-02 11:46:42 +02:00
|
|
|
|
|
|
|
const opcodes = {
|
|
|
|
EVENT: 0,
|
|
|
|
CLIENT_HEARTBEAT: 1,
|
|
|
|
IDENTIFY: 2,
|
2022-02-15 14:24:46 +02:00
|
|
|
RECONNECT: 7,
|
2022-02-03 20:41:23 +02:00
|
|
|
INVALID_SESSION: 9,
|
2022-02-02 11:46:42 +02:00
|
|
|
HELLO: 10,
|
|
|
|
HEARTBEAT_ACK: 11,
|
|
|
|
};
|
|
|
|
|
2022-02-14 22:18:33 +02:00
|
|
|
const skipReconnectFor = [
|
|
|
|
4004, 4010, 4011, 4012, 4013, 4014
|
2022-02-03 20:41:23 +02:00
|
|
|
];
|
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
class DiscordClient extends EventEmitter {
|
2022-02-14 22:18:33 +02:00
|
|
|
constructor(token, { intents, gatewayUrl="wss://gateway.discord.gg/?v=9&encoding=json&compress=zlib-stream", apiBase="https://discord.com/api/v9" } = {}) {
|
2022-02-02 11:46:42 +02:00
|
|
|
super();
|
|
|
|
|
|
|
|
this.token = token;
|
|
|
|
this.gatewayUrl = gatewayUrl;
|
|
|
|
this.apiBase = apiBase;
|
2022-02-02 20:24:14 +02:00
|
|
|
this.inflate = null;
|
2022-02-02 11:46:42 +02:00
|
|
|
this.ws = null;
|
|
|
|
this.intents = intents;
|
|
|
|
|
|
|
|
this.user = null;
|
|
|
|
this.guilds = [];
|
|
|
|
this.sessionId = null;
|
|
|
|
this.seq = null;
|
|
|
|
this.gotServerHeartbeatACK = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setHeartbeat(interval) {
|
|
|
|
this._heartbeatIntervalTime = interval;
|
|
|
|
if (interval < 0 && this._heartbeatInterval) {
|
|
|
|
clearInterval(this._heartbeatInterval);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._heartbeatInterval = setInterval(() => {
|
|
|
|
if (!this.gotServerHeartbeatACK) {
|
2022-02-02 20:24:14 +02:00
|
|
|
this.ws.close(1000, "No heartbeat ACK.");
|
2022-02-02 11:46:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.gotServerHeartbeatACK = false;
|
|
|
|
|
|
|
|
this.ws.send(JSON.stringify({
|
|
|
|
op: opcodes.CLIENT_HEARTBEAT,
|
|
|
|
d: this.seq
|
|
|
|
}));
|
|
|
|
}, this._heartbeatIntervalTime);
|
|
|
|
}
|
|
|
|
|
2022-02-03 20:41:23 +02:00
|
|
|
_getIdentifyPayload() {
|
|
|
|
return {
|
|
|
|
token: this.token,
|
|
|
|
intents: this.intents,
|
|
|
|
properties: {
|
|
|
|
"$os": "linux",
|
|
|
|
"$browser": "generic",
|
|
|
|
"$device": "generic"
|
|
|
|
},
|
|
|
|
presence: {
|
|
|
|
since: Date.now(),
|
|
|
|
activities: [
|
|
|
|
{
|
|
|
|
type: 2, // LISTENING
|
|
|
|
name: "the voices"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
status: "online",
|
|
|
|
afk: false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
_handleGatewayMessage(ws, message) {
|
|
|
|
try {
|
|
|
|
message = JSON.parse(message);
|
|
|
|
} catch(e) {
|
2022-02-15 14:24:46 +02:00
|
|
|
logError("on 'message': failed to parse incoming message as JSON", e);
|
2022-02-02 11:46:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.s) {
|
|
|
|
this.seq = message.s;
|
|
|
|
}
|
|
|
|
|
|
|
|
const payload = message.d;
|
|
|
|
|
|
|
|
switch (message.op) {
|
|
|
|
case opcodes.HELLO: {
|
2022-02-15 14:24:46 +02:00
|
|
|
log(`HELLO; heartbeat_interval=${payload.heartbeat_interval}`);
|
2022-02-02 11:46:42 +02:00
|
|
|
this._setHeartbeat(payload.heartbeat_interval);
|
|
|
|
|
2022-02-14 22:18:33 +02:00
|
|
|
ws.send(JSON.stringify({
|
|
|
|
op: opcodes.IDENTIFY,
|
|
|
|
d: this._getIdentifyPayload()
|
|
|
|
}));
|
2022-02-02 11:46:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case opcodes.EVENT: {
|
|
|
|
switch (message.t) {
|
|
|
|
case "READY": {
|
2022-02-15 14:24:46 +02:00
|
|
|
log("READY");
|
2022-02-02 11:46:42 +02:00
|
|
|
this.user = payload.user;
|
|
|
|
this.sessionId = payload.session_id;
|
|
|
|
this.guilds = payload.guilds;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "GUILD_CREATE": {
|
|
|
|
const targetGuildIndex = this.guilds.findIndex(e => e.id === payload.id);
|
|
|
|
if (targetGuildIndex < 0) {
|
|
|
|
this.guilds.push(payload);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// The guild already exists in our array. This means that
|
|
|
|
// this GUILD_CREATE event is completing our `Unavailable Guild`
|
|
|
|
// objects that we got from the initial READY.
|
|
|
|
this.guilds[targetGuildIndex] = payload;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "GUILD_UPDATE": {
|
|
|
|
const targetGuildIndex = this.guilds.findIndex(e => e.id === payload.id);
|
|
|
|
if (targetGuildIndex < 0) {
|
|
|
|
// tried to update a guild that doesn't exist???
|
|
|
|
this.emit("warn", "got GUILD_UPDATE for a guild that doesn't exist");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.guilds[targetGuildIndex] = payload;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "GUILD_DELETE": {
|
|
|
|
const targetGuildIndex = this.guilds.findIndex(e => e.id === payload.id);
|
|
|
|
if (targetGuildIndex < 0) {
|
|
|
|
// tried to delete a guild that doesn't exist???
|
|
|
|
this.emit("warn", "got GUILD_DELETE for a guild that doesn't exist");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.guilds.splice(targetGuildIndex, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "CHANNEL_CREATE": {
|
|
|
|
const parentGuildIndex = this.guilds.findIndex(e => e.id === payload.guild_id);
|
|
|
|
if (parentGuildIndex < 0) {
|
|
|
|
this.emit("warn", "got CHANNEL_CREATE for a guild that doesn't exist");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.guilds[parentGuildIndex].channels.push(payload);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "CHANNEL_UPDATE": {
|
|
|
|
const parentGuildIndex = this.guilds.findIndex(e => e.id === payload.guild_id);
|
|
|
|
if (parentGuildIndex < 0) {
|
|
|
|
this.emit("warn", "got CHANNEL_CREATE for a guild that doesn't exist");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const relevantChannelIndex = this.guilds[parentGuildIndex].channels.findIndex(e => e.id === payload.id);
|
|
|
|
this.guilds[parentGuildIndex].channels[relevantChannelIndex] = payload;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "CHANNEL_DELETE": {
|
|
|
|
const parentGuildIndex = this.guilds.findIndex(e => e.id === payload.guild_id);
|
|
|
|
if (parentGuildIndex < 0) {
|
|
|
|
this.emit("warn", "got CHANNEL_CREATE for a guild that doesn't exist");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const relevantChannelIndex = this.guilds[parentGuildIndex].channels.findIndex(e => e.id === payload.id);
|
|
|
|
this.guilds[parentGuildIndex].channels.splice(relevantChannelIndex, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.emit(message.t, payload);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case opcodes.HEARTBEAT_ACK: {
|
|
|
|
this.gotServerHeartbeatACK = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-03 20:41:23 +02:00
|
|
|
case opcodes.INVALID_SESSION: {
|
2022-02-15 14:24:46 +02:00
|
|
|
logError("INVALID_SESSION - please check your authentication token");
|
|
|
|
logError("INVALID_SESSION: will not reconnect");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case opcodes.RECONNECT: {
|
|
|
|
log("gateway is requesting reconnect (payload RECONNECT)");
|
|
|
|
this.connect();
|
2022-02-03 20:41:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
default: {
|
2022-02-15 14:24:46 +02:00
|
|
|
logWarn(`got unhandled opcode "${message.op}"`);
|
2022-02-02 11:46:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
connect() {
|
2022-02-15 14:24:46 +02:00
|
|
|
log("connecting...");
|
2022-02-03 20:41:23 +02:00
|
|
|
if (this.ws) {
|
2022-02-15 14:24:46 +02:00
|
|
|
log("a websocket connection already exists, killing...");
|
2022-02-03 20:41:23 +02:00
|
|
|
this.ws.removeAllListeners();
|
|
|
|
this.ws.close();
|
|
|
|
this.ws = null;
|
|
|
|
}
|
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
const ws = new WebSocket(this.gatewayUrl);
|
|
|
|
|
|
|
|
this.ws = ws;
|
2022-02-02 20:24:14 +02:00
|
|
|
this.inflate = zlib.createInflate({
|
|
|
|
chunkSize: 128 * 1024
|
|
|
|
});
|
2022-02-02 11:46:42 +02:00
|
|
|
|
|
|
|
// we decompressed the data, send it to the handler now
|
2022-02-14 22:18:33 +02:00
|
|
|
this.inflate.on("data", (message) =>
|
|
|
|
this._handleGatewayMessage(ws, message)
|
|
|
|
);
|
2022-02-02 11:46:42 +02:00
|
|
|
ws.on("message", (data, isBinary) => {
|
|
|
|
// pass the data to the decompressor
|
|
|
|
this.inflate.write(data);
|
|
|
|
});
|
|
|
|
|
2022-02-14 22:18:33 +02:00
|
|
|
|
|
|
|
ws.on("open", () => {
|
2022-02-15 14:24:46 +02:00
|
|
|
log("WebSocket 'open'");
|
2022-02-14 22:18:33 +02:00
|
|
|
});
|
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
ws.on("close", (code, reason) => {
|
|
|
|
reason = reason.toString();
|
2022-02-15 14:24:46 +02:00
|
|
|
logError(`on 'close': disconnected from gateway: code '${code}', reason '${reason}'`);
|
2022-02-03 20:41:23 +02:00
|
|
|
|
|
|
|
this.emit("close", code, reason);
|
|
|
|
this._setHeartbeat(-1);
|
2022-02-14 22:18:33 +02:00
|
|
|
if (skipReconnectFor.includes(code)) {
|
2022-02-15 14:24:46 +02:00
|
|
|
logError("on 'close': the exit code above is in skipReconnectFor, and thus the server will not reconnect.");
|
2022-02-14 22:18:33 +02:00
|
|
|
} else {
|
2022-02-15 14:24:46 +02:00
|
|
|
log("on 'close': the client will now attempt to reconnect...");
|
2022-02-03 20:41:23 +02:00
|
|
|
this.connect();
|
|
|
|
}
|
2022-02-02 23:48:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
ws.on("error", (e) => {
|
2022-02-15 14:24:46 +02:00
|
|
|
logError("on 'error': websocket error:", e);
|
|
|
|
log("on 'error': reconnecting due to previous websocket error...");
|
2022-02-03 20:41:23 +02:00
|
|
|
this._setHeartbeat(-1);
|
|
|
|
this.connect();
|
2022-02-02 23:48:18 +02:00
|
|
|
});
|
2022-02-02 11:46:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async api([method, path], body=undefined, throwOnError=true) {
|
|
|
|
const options = {
|
|
|
|
method,
|
|
|
|
headers: {
|
|
|
|
"authorization": `Bot ${this.token}`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (method !== "GET" && method !== "HEAD" && typeof body === "object") {
|
|
|
|
options.headers["content-type"] = "application/json";
|
|
|
|
options.body = JSON.stringify(body);
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await fetch(`${this.apiBase}${path}`, options);
|
|
|
|
const json = await response.json();
|
|
|
|
|
|
|
|
if (!response.ok && throwOnError) {
|
|
|
|
throw new Error(`API request returned non-success status ${response.status}, with JSON body ${JSON.stringify(json)}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:18:33 +02:00
|
|
|
export default DiscordClient;
|