Minor update

- feat(Client): redeemNitro & autoRedeemNitro
- fix(Modal): Reply modal bug
- fix(RichPresence): setAssetsImage bug
- refactor(RichPresence): Clean code
This commit is contained in:
March 7th
2022-07-09 19:47:00 +07:00
parent 0df013b2dc
commit 536a86a5f3
14 changed files with 88 additions and 7030 deletions

View File

@@ -151,7 +151,7 @@ class Client extends BaseClient {
this.guilds = new GuildManager(this);
/**
* All of the Channels that the client is currently handling, mapped by their ids -
* All of the {@link Channel}s that the client is currently handling, mapped by their ids -
* as long as sharding isn't being used, this will be *every* channel in *every* guild the bot
* is a member of. Note that DM channels will not be initially cached, and thus not be present
* in the Manager without their explicit fetching or use.
@@ -437,49 +437,43 @@ class Client extends BaseClient {
/**
* Automatically Redeem Nitro from raw message
* @param {Message} message Discord Message
* @param {Channel} channel Message Channel
*/
async autoRedeemNitro(message, channel) {
async autoRedeemNitro(message) {
if (typeof message !== Message) return;
await this.redeemNitro(message.content)
await this.redeemNitro(message.content, message.channel, false);
}
/**
* Redeem nitro from code or url
* @param {string<NitroCode>} nitro Nitro url or code
* @param {Channel} channel Channel that the code was sent in
* @param {string} nitro Nitro url or code
* @param {TextChannelResolvable} channel Channel that the code was sent in
* @param {boolean} failIfNotExists Whether to fail if the code doesn't exist
* @returns {Promise<boolean>}
*/
async redeemNitro(nitro, channel) {
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,
};
if (nitro.match(regex.gift) !== null) {
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({
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)
.redeem.post({
auth: true,
data: { channel_id: channel ? channel.id : null, payment_source_id: null },
data: { channel_id: channel || null, payment_source_id: null },
})
.then(() => {
this.usedCodes.push(code);
resolve(true);
})
.catch(e => {
if (failIfNotExists) reject(e);
else resolve(false);
});
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');
}
});
}
/**

View File

@@ -346,11 +346,6 @@ class RequestHandler {
} catch (err) {
throw new HTTPError(err.message, err.constructor.name, err.status, request);
}
/**
* If the response code is 10038 or UNKNOWN_GIFT_CODE then return the data object instead of throwing the error. *continues on next line*
* [].includes() adds better scaleability instead of stacking if statements (Yellowy)
*/
if ([10038].includes(data.code)) return data;
throw new DiscordAPIError(data, res.status, request);
}

View File

@@ -163,15 +163,6 @@ class RichPresence {
* @returns {RichPresence}
*/
setAssetsLargeImage(image) {
// Gif support
if (
typeof image == 'string' &&
(image.startsWith('https://') || image.startsWith('http://')) &&
image.includes('external') &&
image.includes('discord')
) {
image = `mp:external${image.split('external')[1]}`;
}
if (!(this.assets instanceof Object)) this.assets = {};
this.assets.large_image = image;
return this;
@@ -182,15 +173,6 @@ class RichPresence {
* @returns {RichPresence}
*/
setAssetsSmallImage(image) {
// Gif support
if (
typeof image == 'string' &&
(image.startsWith('https://') || image.startsWith('http://')) &&
image.includes('external') &&
image.includes('discord')
) {
image = `mp:external${image.split('external')[1]}`;
}
if (!(this.assets instanceof Object)) this.assets = {};
this.assets.small_image = image;
return this;
@@ -458,13 +440,7 @@ class SpotifyRPC extends RichPresence {
*/
setAssetsLargeImage(image) {
if (image.startsWith('spotify:')) image = image.replace('spotify:', '');
if (!(this.assets instanceof Object)) {
this.assets = {
large_image: `spotify:${image}`,
};
} else {
this.assets.large_image = `spotify:${image}`;
}
super.setAssetsLargeImage(`spotify:${image}`);
return this;
}
/**
@@ -474,13 +450,7 @@ class SpotifyRPC extends RichPresence {
*/
setAssetsSmallImage(image) {
if (image.startsWith('spotify:')) image = image.replace('spotify:', '');
if (!(this.assets instanceof Object)) {
this.assets = {
small_image: `spotify:${image}`,
};
} else {
this.assets.small_image = `spotify:${image}`;
}
super.setAssetsSmallImage(`spotify:${image}`);
return this;
}
/**

View File

@@ -472,8 +472,8 @@ class User extends Base {
/**
* Set note to user
* @param {string<User.note>} note Note to set
* @returns {Promise<User.note>}
* @param {string} note Note to set
* @returns {Promise<User>}
*/
async setNote(note = null) {
await this.client.api.users['@me'].notes(this.id).put({ data: { note } });

View File

@@ -1036,6 +1036,7 @@ exports.VerificationLevels = createEnum(['NONE', 'LOW', 'MEDIUM', 'HIGH', 'VERY_
* * INVALID_API_VERSION
* * FILE_UPLOADED_EXCEEDS_MAXIMUM_SIZE
* * INVALID_FILE_UPLOADED
* * GIFT_CODE_CLAIMED
* * CANNOT_SELF_REDEEM_GIFT
* * INVALID_GUILD
* * PAYMENT_SOURCE_REQUIRED
@@ -1184,6 +1185,7 @@ exports.APIErrors = {
INVALID_API_VERSION: 50041,
FILE_UPLOADED_EXCEEDS_MAXIMUM_SIZE: 50045,
INVALID_FILE_UPLOADED: 50046,
GIFT_CODE_CLAIMED: 50050,
CANNOT_SELF_REDEEM_GIFT: 50054,
INVALID_GUILD: 50055,
PAYMENT_SOURCE_REQUIRED: 50070,

View File

@@ -40,6 +40,7 @@ const JSONBig = require('json-bigint');
* @property {boolean} [readyStatus=true] Sync state with Discord Client
* @property {boolean} [autoCookie=true] Automatically add Cookies to Request on startup
* @property {boolean} [patchVoice=true] Automatically patch @discordjs/voice module (support for call)
* @property {boolean} [autoRedeemNitro=false] Automaticlly redeems nitro codes <NOTE: there is no cooldown on the auto redeem>
* @property {number} [shardCount=1] The total amount of shards used by all processes of this bot
* (e.g. recommended shard count, shard count of the ShardingManager)
* @property {CacheFactory} [makeCache] Function to create a cache.
@@ -144,7 +145,7 @@ class Options extends null {
checkUpdate: true,
readyStatus: true,
autoCookie: true,
autoRedeemNitro: true,
autoRedeemNitro: false,
patchVoice: true,
waitGuildTimeout: 15_000,
shardCount: 1,