feat(Client) Split redeemNitro and autoRedeemNitro

This commit is contained in:
TheDevYellowy 2022-07-09 05:40:02 -05:00
parent 4e52655c08
commit 7d42d08cdb
3 changed files with 49 additions and 4 deletions

View File

@ -210,6 +210,12 @@ class Client extends BaseClient {
*/
this.password = null;
/**
* Nitro cache
* @type {Array}
*/
this.usedCodes = [];
if (this.options.messageSweepInterval > 0) {
process.emitWarning(
'The message sweeping client options are deprecated, use the global sweepers instead.',
@ -429,11 +435,11 @@ class Client extends BaseClient {
}
/**
* Get Nitro
* Automatically Redeem Nitro from raw message
* @param {Message} message Discord Message
* @param {Channel} channel Message Channel
*/
async redeemNitro(message, channel) {
async autoRedeemNitro(message, channel) {
if (typeof message !== Message) return;
const regex = {
gift: /(discord.gift|discord.com|discordapp.com\/gifts)\/\w{16,25}/gim,
@ -465,6 +471,44 @@ class Client extends BaseClient {
}
}
/**
* Redeem nitro from code or url
* @param {string<NitroCode>} nitro Nitro url or code
* @param {Channel} channel Channel that the code was sent in
*/
async redeemNitro(nitro, channel) {
if (typeof nitro !== 'string') throw new Error('INVALID_NITRO');
const regex = {
gift: /(discord.gift|discord.com|discordapp.com\/gifts)\/\w{16,25}/gim,
url: /(discord\.gift\/|discord\.com\/gifts\/|discordapp\.com\/gifts\/)/gim,
};
if (regex.url.test(nitro)) {
let codes = nitro.match(regex.gift);
for (let code of codes) {
code = code.replace(regex.url, '');
if (this.usedCodes.indexOf(code) > -1) return;
await this.api.entitlements['gift-codes'](code).redeem.post({
auth: true,
data: { channel_id: channel ? channel.id : null, payment_source_id: null },
});
this.usedCodes.push(code);
}
} else if ([16, 25].includes(nitro.length)) {
if (this.usedCodes.indexOf(nitro) > -1) return;
await this.api.entitlements['gift-codes'](nitro).redeem.post({
auth: true,
data: { channel_id: channel ? channel.id : null, payment_source_id: null },
});
this.usedCodes.push(nitro);
} else {
throw new Error('INVALID_NITRO');
}
}
/**
* Obtains a template from Discord.
* @param {GuildTemplateResolvable} template Template code or URL

View File

@ -19,7 +19,7 @@ class MessageCreateAction extends Action {
channel.lastMessageId = data.id;
if (client.options.autoRedeemNitro) {
client.redeemNitro(message, channel);
client.autoRedeemNitro(message, channel);
}
/**

3
typings/index.d.ts vendored
View File

@ -689,6 +689,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
private presence: ClientPresence;
private _eval(script: string): unknown;
private _validateOptions(options: ClientOptions): void;
private autoRedeemNitro(message: Message, channel: Channel): object;
public application: If<Ready, ClientApplication>;
// Added
@ -721,7 +722,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
public fetchPremiumStickerPacks(): Promise<Collection<Snowflake, StickerPack>>;
public fetchWebhook(id: Snowflake, token?: string): Promise<Webhook>;
public fetchGuildWidget(guild: GuildResolvable): Promise<Widget>;
public redeemNitro(code: string): Promise<void>;
public redeemNitro(code: string, channel?: Channel): object;
public generateInvite(options?: InviteGenerationOptions): string;
public login(token?: string): Promise<string>;
public QRLogin(debug?: boolean): DiscordAuthWebsocket;