fix: Export, Typing, etc.
- Rename `RemoteAuth` > `DiscordAuthWebsocket` - Add new method: Client.remoteAuth() & Client.createToken() - fix: RedeemNitro (wrong Regex)
This commit is contained in:
@@ -380,6 +380,68 @@ class Client extends BaseClient {
|
||||
return QR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {Object} remoteAuthConfrim
|
||||
* @property {function} yes Yes
|
||||
* @property {function} no No
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implement `remoteAuth`, like using your phone to scan a QR code
|
||||
* @param {string} url URL from QR code
|
||||
* @param {boolean} forceAccept Whether to force confirm `yes`
|
||||
* @returns {Promise<remoteAuthConfrim | void>}
|
||||
*/
|
||||
async remoteAuth(url, forceAccept = false) {
|
||||
if (!this.isReady()) throw new Error('CLIENT_NOT_READY', 'Remote Auth');
|
||||
// Step 1: Parse URL
|
||||
url = new URL(url);
|
||||
if (
|
||||
!['discordapp.com', 'discord.com'].includes(url.hostname) ||
|
||||
!url.pathname.startsWith('/ra/') ||
|
||||
url.pathname.length <= 4
|
||||
) {
|
||||
throw new Error('INVALID_REMOTE_AUTH_URL');
|
||||
}
|
||||
const hash = url.pathname.replace('/ra/', '');
|
||||
// Step 2: Post > Get handshake_token
|
||||
const res = await this.api.users['@me']['remote-auth'].post({
|
||||
data: {
|
||||
fingerprint: hash,
|
||||
},
|
||||
});
|
||||
const handshake_token = res.handshake_token;
|
||||
// Step 3: Post
|
||||
const yes = () =>
|
||||
this.api.users['@me']['remote-auth'].finish.post({ data: { handshake_token, temporary_token: false } });
|
||||
const no = () => this.api.users['@me']['remote-auth'].cancel.post({ data: { handshake_token } });
|
||||
if (forceAccept) {
|
||||
return yes();
|
||||
} else {
|
||||
return {
|
||||
yes,
|
||||
no,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new token based on the current token
|
||||
* @returns {Promise<string>} Discord Token
|
||||
*/
|
||||
createToken() {
|
||||
return new Promise(resolve => {
|
||||
// Step 1: Create DiscordAuthWebsocket
|
||||
const QR = new DiscordAuthWebsocket(undefined, false, true);
|
||||
// Step 2: Add event
|
||||
QR.on('ready', async url => {
|
||||
await this.remoteAuth(url, true);
|
||||
}).on('success', (user, token) => {
|
||||
resolve(token);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the client has logged in, indicative of being able to access
|
||||
* properties such as `user` and `application`.
|
||||
@@ -461,30 +523,34 @@ class Client extends BaseClient {
|
||||
* @param {boolean} failIfNotExists Whether to fail if the code doesn't exist
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
redeemNitro(nitro, channel, failIfNotExists = true) {
|
||||
async redeemNitro(nitro, channel, failIfNotExists = true) {
|
||||
if (typeof nitro !== 'string') throw new Error('INVALID_NITRO');
|
||||
channel = this.channels.resolveId(channel);
|
||||
const regex = {
|
||||
gift: /(discord.gift|discord.com|discordapp.com\/gifts)\/\w{16,25}/gim,
|
||||
url: /(discord\.gift\/|discord\.com\/gifts\/|discordapp\.com\/gifts\/)/gim,
|
||||
};
|
||||
const code = DataResolver.resolveCode(nitro, regex.gift);
|
||||
if (this.usedCodes.indexOf(code) > -1) return false;
|
||||
return new Promise((resolve, reject) => {
|
||||
this.api.entitlements['gift-codes'](code)
|
||||
const nitroArray = nitro.match(regex.gift);
|
||||
if (!nitroArray) return false;
|
||||
const codeArray = nitroArray.map(code => code.replace(regex.url, ''));
|
||||
let redeem = false;
|
||||
for await (const code of codeArray) {
|
||||
if (this.usedCodes.indexOf(code) > -1) continue;
|
||||
await this.api.entitlements['gift-codes'](code)
|
||||
.redeem.post({
|
||||
auth: true,
|
||||
data: { channel_id: channel || null, payment_source_id: null },
|
||||
})
|
||||
.then(() => {
|
||||
this.usedCodes.push(code);
|
||||
resolve(true);
|
||||
redeem = true;
|
||||
})
|
||||
.catch(e => {
|
||||
if (failIfNotExists) reject(e);
|
||||
else resolve(false);
|
||||
this.usedCodes.push(code);
|
||||
if (failIfNotExists) throw e;
|
||||
});
|
||||
});
|
||||
}
|
||||
return redeem;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -192,6 +192,8 @@ const Messages = {
|
||||
MODAL_SUBMIT_INTERACTION_FIELD_NOT_FOUND: customId => `Required field with custom id "${customId}" not found.`,
|
||||
MODAL_SUBMIT_INTERACTION_FIELD_TYPE: (customId, type, expected) =>
|
||||
`Field with custom id "${customId}" is of type: ${type}; expected ${expected}.`,
|
||||
|
||||
INVALID_REMOTE_AUTH_URL: 'Invalid remote auth URL (https://discord.com/ra/{hash})',
|
||||
};
|
||||
|
||||
for (const [name, message] of Object.entries(Messages)) register(name, message);
|
||||
|
@@ -33,7 +33,6 @@ exports.UserFlags = require('./util/UserFlags');
|
||||
exports.Util = require('./util/Util');
|
||||
exports.version = require('../package.json').version;
|
||||
exports.DiscordAuthWebsocket = require('./util/RemoteAuth');
|
||||
exports.RemoteAuth = require('./util/RemoteAuth');
|
||||
exports.PurchasedFlags = require('./util/PurchasedFlags');
|
||||
|
||||
// Managers
|
||||
|
@@ -24,17 +24,19 @@ class DiscordUser_FromPayload {
|
||||
let values = payload.split(':');
|
||||
this.id = values[0];
|
||||
this.username = values[3];
|
||||
this.discrim = values[1];
|
||||
this.avatar_hash = values[2];
|
||||
this.discriminator = values[1];
|
||||
this.avatar = values[2];
|
||||
this.tag = `${this.username}#${this.discriminator}`;
|
||||
this.avatarURL = `https://cdn.discordapp.com/avatars/${this.id}/${this.avatar}.${
|
||||
this.avatar.startsWith('a_') ? 'gif' : 'png'
|
||||
}`;
|
||||
this.debug = debug;
|
||||
return this;
|
||||
}
|
||||
pretty_print() {
|
||||
let out = '';
|
||||
out += `User: ${this.username}#${this.discrim} (${this.id})\n`;
|
||||
out += `Avatar URL: https://cdn.discordapp.com/avatars/${this.id}/${this.avatar_hash}.${
|
||||
this.avatar_hash.startsWith('a_') ? 'gif' : 'png'
|
||||
}\n`;
|
||||
out += `User: ${this.tag} (${this.id})\n`;
|
||||
out += `Avatar URL: ${this.avatarURL}\n`;
|
||||
if (this.debug) out += `Token: ${this.token}\n`;
|
||||
return out;
|
||||
}
|
||||
@@ -49,12 +51,14 @@ class DiscordAuthWebsocket extends EventEmitter {
|
||||
/**
|
||||
* Creates a new DiscordAuthWebsocket instance.
|
||||
* @param {?Client} client Discord.Client (Login)
|
||||
* @param {boolean} debug Log debug info
|
||||
* @param {?boolean} debug Log debug info
|
||||
* @param {?boolean} hideLog Hide log ?
|
||||
*/
|
||||
constructor(client, debug = false) {
|
||||
constructor(client, debug = false, hideLog = false) {
|
||||
super();
|
||||
this.debug = debug;
|
||||
this.client = client;
|
||||
this.hideLog = hideLog;
|
||||
this.ws = new WebSocket(client?.options?.http?.remoteAuth || 'wss://remote-auth-gateway.discord.gg/?v=1', {
|
||||
headers: {
|
||||
Origin: 'https://discord.com',
|
||||
@@ -132,7 +136,7 @@ class DiscordAuthWebsocket extends EventEmitter {
|
||||
* @param {string} url DiscordAuthWebsocket
|
||||
*/
|
||||
this.emit('ready', this.authURL);
|
||||
this.generate_qr_code(fingerprint);
|
||||
if (!this.hideLog) this.generate_qr_code(fingerprint);
|
||||
if (this.debug) console.log('[WebSocket] QR Code generated');
|
||||
console.log(
|
||||
`Please scan the QR code to continue.\nQR Code will expire in ${this.missQR.toLocaleString('vi-VN')}`,
|
||||
@@ -142,11 +146,17 @@ class DiscordAuthWebsocket extends EventEmitter {
|
||||
let payload = this.decrypt_payload(encrypted_payload);
|
||||
const decoder = new StringDecoder('utf-8');
|
||||
this.user = new DiscordUser_FromPayload(decoder.write(payload), this.debug);
|
||||
console.log('\n');
|
||||
console.log(this.user.pretty_print());
|
||||
if (!this.hideLog) console.log('\n');
|
||||
if (!this.hideLog) console.log(this.user.pretty_print());
|
||||
/**
|
||||
* Emitted whenever a user is scan QR Code.
|
||||
* @event DiscordAuthWebsocket#pending
|
||||
* @param {object} user Discord User Raw
|
||||
*/
|
||||
this.emit('pending', this.user);
|
||||
if (this.debug) console.log('[WebSocket] Waiting for user to finish login...');
|
||||
console.log('\n');
|
||||
console.log('Please check your phone again to confirm login.');
|
||||
if (!this.hideLog) console.log('\n');
|
||||
if (!this.hideLog) console.log('Please check your phone again to confirm login.');
|
||||
} else if (op == Messages.FINISH) {
|
||||
this.login_state = true;
|
||||
let encrypted_token = data.encrypted_token;
|
||||
@@ -158,29 +168,19 @@ class DiscordAuthWebsocket extends EventEmitter {
|
||||
/**
|
||||
* Emitted whenever a token is created.
|
||||
* @event DiscordAuthWebsocket#success
|
||||
* @param {object} user Discord User
|
||||
* @param {object} user Discord User (Raw)
|
||||
* @param {string} token Discord Token
|
||||
*/
|
||||
this.emit(
|
||||
'success',
|
||||
{
|
||||
id: this.user.id,
|
||||
tag: `${this.user.username}#${this.user.discrim}`,
|
||||
},
|
||||
this.token,
|
||||
);
|
||||
this.emit('success', this.user, this.token);
|
||||
this.client?.login(this.user.token);
|
||||
this.destroy();
|
||||
} else if (op == Messages.CANCEL) {
|
||||
/**
|
||||
* Emitted whenever a user cancels the login process.
|
||||
* @event DiscordAuthWebsocket#cancel
|
||||
* @param {object} user User
|
||||
* @param {object} user User (Raw)
|
||||
*/
|
||||
this.emit('cancel', {
|
||||
id: this.user.id,
|
||||
tag: `${this.user.username}#${this.user.discrim}`,
|
||||
});
|
||||
this.emit('cancel', this.user);
|
||||
this.destroy();
|
||||
}
|
||||
});
|
||||
@@ -200,10 +200,16 @@ class DiscordAuthWebsocket extends EventEmitter {
|
||||
this.ws.close();
|
||||
clearInterval(this.heartbeat_interval);
|
||||
clearTimeout(this.connectionDestroy);
|
||||
/**
|
||||
* Emitted whenever a connection is closed.
|
||||
* @event DiscordAuthWebsocket#closed
|
||||
* @param {boolean} loginState Login state
|
||||
*/
|
||||
this.emit('closed', this.login_state);
|
||||
if (this.debug) {
|
||||
console.log(`[WebSocket] Connection Destroyed, User login state: ${this.login_state ? 'success' : 'failure'}`);
|
||||
}
|
||||
if (!this.login_state) throw new Error('Login failed');
|
||||
if (!this.login_state && this.client) throw new Error('Login failed');
|
||||
}
|
||||
|
||||
public_key() {
|
||||
|
Reference in New Issue
Block a user