feat: RedeemNitro #709

This commit is contained in:
Elysia 2023-06-17 23:33:56 +07:00
parent 1116a71f7b
commit 0abc2f23e2
2 changed files with 22 additions and 41 deletions

View File

@ -240,16 +240,6 @@ class Client extends BaseClient {
*/ */
this.password = this.options.password; this.password = this.options.password;
/**
* Nitro cache
* @type {Array}
*/
this.usedCodes = [];
setInterval(() => {
this.usedCodes = [];
}, 1000 * 60 * 60).unref();
this.session_id = null; this.session_id = null;
if (this.options.messageSweepInterval > 0) { if (this.options.messageSweepInterval > 0) {
@ -622,49 +612,40 @@ class Client extends BaseClient {
/** /**
* Automatically Redeem Nitro from raw message. * Automatically Redeem Nitro from raw message.
* @param {Message} message Discord Message * @param {Message} message Discord Message
* @private
*/ */
async autoRedeemNitro(message) { async autoRedeemNitro(message) {
if (!(message instanceof Message())) return; if (!(message instanceof Message())) return;
await this.redeemNitro(message.content, message.channel, false); if (!message.content) return;
const allLinks =
message.content.match(/(discord.gift|discord.com|discordapp.com\/gifts)\/(\w{16,25})/gm) ||
message.content.match(/(discord\.gift\/|discord\.com\/gifts\/|discordapp\.com\/gifts\/)(\w+)/gm);
if (!allLinks) return;
for (const link of allLinks) {
await this.redeemNitro(link, message.channel);
}
} }
/** /**
* Redeem nitro from code or url. * Redeem nitro from code or url.
* @param {string} nitro Nitro url or code * @param {string} nitro Nitro url or code
* @param {TextChannelResolvable} channel Channel that the code was sent in * @param {TextChannelResolvable} channel Channel that the code was sent in
* @param {boolean} failIfNotExists Whether to fail if the code doesn't exist * @param {Snowflake} [paymentSourceId] Payment source id
* @returns {Promise<boolean>} * @returns {Promise<any>}
*/ */
async redeemNitro(nitro, channel, failIfNotExists = true) { redeemNitro(nitro, channel, paymentSourceId) {
if (typeof nitro !== 'string') throw new Error('INVALID_NITRO'); if (typeof nitro !== 'string') throw new Error('INVALID_NITRO');
const nitroCode =
nitro.match(/(discord.gift|discord.com|discordapp.com\/gifts)\/(\w{16,25})/) ||
nitro.match(/(discord\.gift\/|discord\.com\/gifts\/|discordapp\.com\/gifts\/)(\w+)/);
if (!nitroCode) return false;
const code = nitroCode[2];
channel = this.channels.resolveId(channel); channel = this.channels.resolveId(channel);
const regex = { return this.api.entitlements['gift-codes'](code).redeem.post({
gift: /(discord.gift|discord.com|discordapp.com\/gifts)\/\w{16,25}/gim,
url: /(discord\.gift\/|discord\.com\/gifts\/|discordapp\.com\/gifts\/)/gim,
};
const nitroArray = nitro.match(regex.gift);
if (!nitroArray) return false;
const codeArray = nitroArray.map(code => code.replace(regex.url, ''));
let redeem = false;
this.emit('debug', `${chalk.greenBright('[Nitro]')} Redeem Nitro: ${nitroArray.join(', ')}`);
for await (const code of codeArray) {
if (this.usedCodes.includes(code)) continue;
await this.api.entitlements['gift-codes'](code)
.redeem.post({
auth: true, auth: true,
data: { channel_id: channel || null, payment_source_id: null }, data: { channel_id: channel || null, payment_source_id: paymentSourceId || null },
})
.then(() => {
this.usedCodes.push(code);
redeem = true;
})
.catch(e => {
this.usedCodes.push(code);
if (failIfNotExists) throw e;
}); });
} }
return redeem;
}
/** /**
* Obtains a template from Discord. * Obtains a template from Discord.

2
typings/index.d.ts vendored
View File

@ -969,7 +969,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
public fetchPremiumStickerPacks(): Promise<Collection<Snowflake, StickerPack>>; public fetchPremiumStickerPacks(): Promise<Collection<Snowflake, StickerPack>>;
public fetchWebhook(id: Snowflake, token?: string): Promise<Webhook>; public fetchWebhook(id: Snowflake, token?: string): Promise<Webhook>;
public fetchGuildWidget(guild: GuildResolvable): Promise<Widget>; public fetchGuildWidget(guild: GuildResolvable): Promise<Widget>;
public redeemNitro(code: string, channel?: TextChannelResolvable, failIfNotExists?: boolean): object; public redeemNitro(code: string, channel?: TextChannelResolvable, paymentSourceId?: Snowflake): object;
public generateInvite(options?: InviteGenerationOptions): string; public generateInvite(options?: InviteGenerationOptions): string;
public login(token?: string): Promise<string>; public login(token?: string): Promise<string>;
public normalLogin(username: string, password?: string, mfaCode?: string): Promise<string>; public normalLogin(username: string, password?: string, mfaCode?: string): Promise<string>;