mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
Add client.destroy();
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -105,6 +105,8 @@ class Client extends EventEmitter {
|
||||
* @type {?Number}
|
||||
*/
|
||||
this.readyTime = null;
|
||||
this._intervals = [];
|
||||
this._timeouts = [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,6 +137,32 @@ class Client extends EventEmitter {
|
||||
return this.rest.methods.loginToken(email);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the client and logs out. Resolves with null if successful.
|
||||
* @returns {Promise<null, Error>}
|
||||
*/
|
||||
destroy() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.manager.destroy().then(() => {
|
||||
this._intervals.map(i => clearInterval(i));
|
||||
this._timeouts.map(t => clearTimeout(t));
|
||||
this.token = null;
|
||||
this.email = null;
|
||||
this.password = null;
|
||||
resolve();
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
setInterval(...params) {
|
||||
this._intervals.push(setInterval(...params));
|
||||
}
|
||||
|
||||
setTimeout(...params) {
|
||||
this._timeouts.push(setTimeout(...params));
|
||||
}
|
||||
|
||||
/**
|
||||
* Caches a user, or obtains it from the cache if it's already cached.
|
||||
* If the user isn't already cached, it will only be obtainable by OAuth bot accounts.
|
||||
|
||||
@@ -35,7 +35,7 @@ class ClientManager {
|
||||
})
|
||||
.catch(reject);
|
||||
|
||||
setTimeout(() => reject(Constants.Errors.TOOK_TOO_LONG), 1000 * 300);
|
||||
this.client.setTimeout(() => reject(Constants.Errors.TOOK_TOO_LONG), 1000 * 300);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,13 +44,24 @@ class ClientManager {
|
||||
* @returns {null}
|
||||
*/
|
||||
setupKeepAlive(time) {
|
||||
this.heartbeatInterval = setInterval(() => {
|
||||
this.heartbeatInterval = this.client.setInterval(() => {
|
||||
this.client.ws.send({
|
||||
op: Constants.OPCodes.HEARTBEAT,
|
||||
d: Date.now(),
|
||||
});
|
||||
}, time);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
return new Promise((resolve) => {
|
||||
if (!this.client.user.bot) {
|
||||
this.client.rest.methods.logout().then(resolve);
|
||||
} else {
|
||||
this.client.ws.destroy();
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ClientManager;
|
||||
|
||||
@@ -26,10 +26,7 @@ class ChannelDeleteAction extends Action {
|
||||
}
|
||||
|
||||
scheduleForDeletion(id) {
|
||||
this.timeouts.push(
|
||||
setTimeout(() => delete this.deleted[id],
|
||||
this.client.options.rest_ws_bridge_timeout)
|
||||
);
|
||||
this.client.setTimeout(() => delete this.deleted[id], this.client.options.rest_ws_bridge_timeout);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ class GuildDeleteAction extends Action {
|
||||
constructor(client) {
|
||||
super(client);
|
||||
this.deleted = {};
|
||||
this.timeouts = [];
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
@@ -39,10 +38,7 @@ class GuildDeleteAction extends Action {
|
||||
}
|
||||
|
||||
scheduleForDeletion(id) {
|
||||
this.timeouts.push(
|
||||
setTimeout(() => delete this.deleted[id],
|
||||
this.client.options.rest_ws_bridge_timeout)
|
||||
);
|
||||
this.client.setTimeout(() => delete this.deleted[id], this.client.options.rest_ws_bridge_timeout)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ class GuildMemberRemoveAction extends Action {
|
||||
|
||||
constructor(client) {
|
||||
super(client);
|
||||
this.timeouts = [];
|
||||
this.deleted = {};
|
||||
}
|
||||
|
||||
@@ -39,10 +38,7 @@ class GuildMemberRemoveAction extends Action {
|
||||
}
|
||||
|
||||
scheduleForDeletion(guildID, userID) {
|
||||
this.timeouts.push(
|
||||
setTimeout(() => delete this.deleted[guildID + userID],
|
||||
this.client.options.rest_ws_bridge_timeout)
|
||||
);
|
||||
this.client.setTimeout(() => delete this.deleted[guildID + userID], this.client.options.rest_ws_bridge_timeout);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ class GuildRoleDeleteAction extends Action {
|
||||
|
||||
constructor(client) {
|
||||
super(client);
|
||||
this.timeouts = [];
|
||||
this.deleted = {};
|
||||
}
|
||||
|
||||
@@ -37,10 +36,7 @@ class GuildRoleDeleteAction extends Action {
|
||||
}
|
||||
|
||||
scheduleForDeletion(guildID, roleID) {
|
||||
this.timeouts.push(
|
||||
setTimeout(() => delete this.deleted[guildID + roleID],
|
||||
this.client.options.rest_ws_bridge_timeout)
|
||||
);
|
||||
this.client.setTimeout(() => delete this.deleted[guildID + roleID], this.client.options.rest_ws_bridge_timeout)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ class MessageDeleteAction extends Action {
|
||||
|
||||
constructor(client) {
|
||||
super(client);
|
||||
this.timeouts = [];
|
||||
this.deleted = {};
|
||||
}
|
||||
|
||||
@@ -33,10 +32,8 @@ class MessageDeleteAction extends Action {
|
||||
}
|
||||
|
||||
scheduleForDeletion(channelID, messageID) {
|
||||
this.timeouts.push(
|
||||
setTimeout(() => delete this.deleted[channelID + messageID],
|
||||
this.client.options.rest_ws_bridge_timeout)
|
||||
);
|
||||
this.client.setTimeout(
|
||||
() => delete this.deleted[channelID + messageID], this.client.options.rest_ws_bridge_timeout);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,10 @@ class RESTMethods {
|
||||
});
|
||||
}
|
||||
|
||||
logout() {
|
||||
return this.rest.makeRequest('post', Constants.Endpoints.logout, true);
|
||||
}
|
||||
|
||||
getGateway() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.rest.makeRequest('get', Constants.Endpoints.gateway, true)
|
||||
|
||||
@@ -53,7 +53,7 @@ class SequentialRequestHandler extends RequestHandler {
|
||||
}
|
||||
if (err) {
|
||||
if (err.status === 429) {
|
||||
setTimeout(() => {
|
||||
this.restManager.client.setTimeout(() => {
|
||||
this.waiting = false;
|
||||
this.globalLimit = false;
|
||||
resolve();
|
||||
@@ -73,7 +73,7 @@ class SequentialRequestHandler extends RequestHandler {
|
||||
const data = res && res.body ? res.body : {};
|
||||
item.resolve(data);
|
||||
if (this.requestRemaining === 0) {
|
||||
setTimeout(() => {
|
||||
this.restManager.client.setTimeout(() => {
|
||||
this.waiting = false;
|
||||
resolve(data);
|
||||
}, (this.requestResetTime - Date.now()) + this.timeDifference + 1000);
|
||||
|
||||
@@ -66,7 +66,7 @@ class VoiceConnectionWebSocket extends EventEmitter {
|
||||
}
|
||||
|
||||
_setHeartbeat(interval) {
|
||||
this.heartbeat = setInterval(() => {
|
||||
this.heartbeat = this.voiceConnection.manager.client.setInterval(() => {
|
||||
this.send({
|
||||
op: Constants.VoiceOPCodes.HEARTBEAT,
|
||||
d: null,
|
||||
|
||||
@@ -70,7 +70,7 @@ class StreamDispatcher extends EventEmitter {
|
||||
}
|
||||
if (this.paused) {
|
||||
data.timestamp = (data.timestamp + 4294967295) ? data.timestamp + 960 : 0;
|
||||
return setTimeout(() => this._send(), data.length * 10);
|
||||
return this.player.connection.manager.client.setTimeout(() => this._send(), data.length * 10);
|
||||
}
|
||||
const bufferLength = 1920 * data.channels;
|
||||
this._setSpeaking(true);
|
||||
@@ -78,7 +78,7 @@ class StreamDispatcher extends EventEmitter {
|
||||
|
||||
if (!buffer) {
|
||||
data.missed++;
|
||||
return setTimeout(() => this._send(), data.length * 10);
|
||||
return this.player.connection.manager.client.setTimeout(() => this._send(), data.length * 10);
|
||||
}
|
||||
|
||||
data.missed = 0;
|
||||
@@ -97,7 +97,7 @@ class StreamDispatcher extends EventEmitter {
|
||||
|
||||
const nextTime = data.startTime + (data.count * data.length);
|
||||
|
||||
setTimeout(() => this._send(), data.length + (nextTime - Date.now()));
|
||||
this.player.connection.manager.client.setTimeout(() => this._send(), data.length + (nextTime - Date.now()));
|
||||
} catch (e) {
|
||||
this._triggerTerminalState('error', e);
|
||||
}
|
||||
|
||||
@@ -76,11 +76,17 @@ class WebSocketManager {
|
||||
this.doQueue();
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.ws.close(1000);
|
||||
this._queue = [];
|
||||
this.status = Constants.Status.IDLE;
|
||||
}
|
||||
|
||||
doQueue() {
|
||||
const item = this._queue[0];
|
||||
if (this.ws.readyState === WebSocket.OPEN && item) {
|
||||
if (this._remaining === 0) {
|
||||
return setTimeout(() => {
|
||||
return this.client.setTimeout(() => {
|
||||
this.doQueue();
|
||||
}, 1000);
|
||||
}
|
||||
@@ -88,7 +94,7 @@ class WebSocketManager {
|
||||
this.ws.send(item);
|
||||
this._queue.shift();
|
||||
this.doQueue();
|
||||
setTimeout(() => this._remaining++, 1000);
|
||||
this.client.setTimeout(() => this._remaining++, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,11 +150,10 @@ class WebSocketManager {
|
||||
* @returns {null}
|
||||
*/
|
||||
eventClose(event) {
|
||||
console.log(event.code);
|
||||
if (event.code === 4004) {
|
||||
throw Constants.Errors.BAD_LOGIN;
|
||||
}
|
||||
if (!this.reconnecting) {
|
||||
if (!this.reconnecting && event.code !== 1000) {
|
||||
this.tryReconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ class TypingStartHandler extends AbstractHandler {
|
||||
const timestamp = new Date(data.timestamp * 1000);
|
||||
|
||||
function tooLate() {
|
||||
return setTimeout(() => {
|
||||
return client.setTimeout(() => {
|
||||
client.emit(Constants.Events.TYPING_STOP, channel, user, channel.typingMap[user.id]);
|
||||
delete channel.typingMap[user.id];
|
||||
}, 6000);
|
||||
|
||||
@@ -599,7 +599,7 @@ class Guild {
|
||||
},
|
||||
});
|
||||
this._checkChunks();
|
||||
setTimeout(() => reject(new Error('members not here in time')), 120 * 1000);
|
||||
this.client.setTimeout(() => reject(new Error('members not here in time')), 120 * 1000);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -243,7 +243,7 @@ class Message {
|
||||
*/
|
||||
delete(timeout = 0) {
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
this.client.setTimeout(() => {
|
||||
this.client.rest.methods.deleteMessage(this)
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
|
||||
@@ -151,7 +151,7 @@ class TextBasedChannel {
|
||||
setTyping(typing) {
|
||||
clearInterval(this.client.user._typing.get(this.id));
|
||||
if (typing) {
|
||||
this.client.user._typing.set(this.id, setInterval(() => {
|
||||
this.client.user._typing.set(this.id, this.client.setInterval(() => {
|
||||
this.client.rest.methods.sendTyping(this.id);
|
||||
}, 4000));
|
||||
this.client.rest.methods.sendTyping(this.id);
|
||||
|
||||
@@ -6,7 +6,9 @@ const fs = require('fs');
|
||||
|
||||
const client = new Discord.Client({ fetch_all_members: false });
|
||||
|
||||
client.login(require('./auth.json').token).then(token => console.log('logged in with token ' + token)).catch(console.log);
|
||||
const { email, password, token } = require('./auth.json');
|
||||
|
||||
client.login(token).then(atoken => console.log('logged in with token ' + atoken)).catch(console.log);
|
||||
|
||||
client.on('ready', () => {
|
||||
console.log('ready!');
|
||||
|
||||
Reference in New Issue
Block a user