discord.js-selfbot-v13/src/managers/SessionManager.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

'use strict';
const CachedManager = require('./CachedManager');
const { Error } = require('../errors/DJSError');
const Session = require('../structures/Session');
/**
* Manages API methods for users and stores their cache.
* @extends {CachedManager}
*/
class SessionManager extends CachedManager {
constructor(client, iterable) {
super(client, Session, iterable);
}
2023-04-03 10:52:24 +00:00
/**
* The cache of Sessions
* @type {Collection<string, Session>}
* @name SessionManager#cache
*/
/**
* Fetch all sessions of the client.
* @returns {Promise<SessionManager>}
*/
fetch() {
return new Promise((resolve, reject) => {
this.client.api.auth.sessions
.get()
.then(data => {
const allData = data.user_sessions;
2022-10-21 11:53:09 +00:00
this.cache.clear();
for (const session of allData) {
2022-10-03 11:09:10 +00:00
this._add(new Session(this.client, session), true, { id: session.id_hash });
}
resolve(this);
})
.catch(reject);
});
}
/**
* Logout the client (remote).
* @param {string | null} mfaCode MFA code (if 2FA is enabled)
* @returns {Promise<undefined>}
*/
2022-11-08 16:56:17 +00:00
logoutAllDevices(mfaCode) {
if (typeof this.client.password !== 'string') throw new Error('REQUIRE_PASSWORD');
return this.client.api.auth.sessions.logout({
data: {
session_id_hashes: this.cache.map(session => session.id),
2022-11-08 16:56:17 +00:00
password: this.client.password,
code: typeof mfaCode === 'string' ? mfaCode : undefined,
},
});
}
}
module.exports = SessionManager;