mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
rewrite ratelimiting and api route builder (#1667)
* rewrite ratelimiting and api route builder * more stuff * let people pass their own handlers * Update burst.js * Update RequestHandler.js * Update burst.js * Update sequential.js * Update RequestHandler.js
This commit is contained in:
@@ -62,7 +62,7 @@ class Channel {
|
||||
* .catch(console.error); // Log error
|
||||
*/
|
||||
delete() {
|
||||
return this.client.api.channels[this.id].delete().then(() => this);
|
||||
return this.client.api.channels(this.id).delete().then(() => this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -91,7 +91,7 @@ class ClientUser extends User {
|
||||
if (data.new_password) _data.new_password = data.newPassword;
|
||||
}
|
||||
|
||||
return this.client.api.users['@me'].patch({ data })
|
||||
return this.client.api.users('@me').patch({ data })
|
||||
.then(newData => this.client.actions.UserUpdate.handle(newData).updated);
|
||||
}
|
||||
|
||||
@@ -284,7 +284,7 @@ class ClientUser extends User {
|
||||
if (options.guild instanceof Guild) options.guild = options.guild.id;
|
||||
Util.mergeDefault({ limit: 25, roles: true, everyone: true, guild: null }, options);
|
||||
|
||||
return this.client.api.users['@me'].mentions.get({ query: options })
|
||||
return this.client.api.users('@me').mentions.get({ query: options })
|
||||
.then(data => data.map(m => new Message(this.client.channels.get(m.channel_id), m, this.client)));
|
||||
}
|
||||
|
||||
@@ -351,7 +351,7 @@ class ClientUser extends User {
|
||||
return o;
|
||||
}, {}),
|
||||
} : { recipients: recipients.map(u => this.client.resolver.resolveUserID(u)) };
|
||||
return this.client.api.users['@me'].channels.post({ data })
|
||||
return this.client.api.users('@me').channels.post({ data })
|
||||
.then(res => new GroupDMChannel(this.client, res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ class Emoji {
|
||||
* .catch(console.error);
|
||||
*/
|
||||
edit(data, reason) {
|
||||
return this.client.api.guilds[this.guild.id].emojis[this.id]
|
||||
return this.client.api.guilds(this.guild.id).emojis(this.id)
|
||||
.patch({ data: {
|
||||
name: data.name,
|
||||
roles: data.roles ? data.roles.map(r => r.id ? r.id : r) : [],
|
||||
|
||||
@@ -378,7 +378,7 @@ class Guild {
|
||||
* @returns {Promise<Collection<Snowflake, Object>>}
|
||||
*/
|
||||
fetchBans() {
|
||||
return this.client.api.guilds[this.id].bans.get().then(bans =>
|
||||
return this.client.api.guilds(this.id).bans.get().then(bans =>
|
||||
bans.reduce((collection, ban) => {
|
||||
collection.set(ban.user.id, {
|
||||
reason: ban.reason,
|
||||
@@ -394,7 +394,7 @@ class Guild {
|
||||
* @returns {Promise<Collection<string, Invite>>}
|
||||
*/
|
||||
fetchInvites() {
|
||||
return this.client.api.guilds[this.id].invites.get()
|
||||
return this.client.api.guilds(this.id).invites.get()
|
||||
.then(inviteItems => {
|
||||
const invites = new Collection();
|
||||
for (const inviteItem of inviteItems) {
|
||||
@@ -410,7 +410,7 @@ class Guild {
|
||||
* @returns {Promise<Collection<Snowflake, Webhook>>}
|
||||
*/
|
||||
fetchWebhooks() {
|
||||
return this.client.api.guilds[this.id].webhooks.get().then(data => {
|
||||
return this.client.api.guilds(this.id).webhooks.get().then(data => {
|
||||
const hooks = new Collection();
|
||||
for (const hook of data) hooks.set(hook.id, new Webhook(this.client, hook));
|
||||
return hooks;
|
||||
@@ -422,7 +422,7 @@ class Guild {
|
||||
* @returns {Promise<Collection<string, VoiceRegion>>}
|
||||
*/
|
||||
fetchVoiceRegions() {
|
||||
return this.client.api.guilds[this.id].regions.get().then(res => {
|
||||
return this.client.api.guilds(this.id).regions.get().then(res => {
|
||||
const regions = new Collection();
|
||||
for (const region of res) regions.set(region.id, new VoiceRegion(region));
|
||||
return regions;
|
||||
@@ -444,7 +444,7 @@ class Guild {
|
||||
if (options.after && options.after instanceof GuildAuditLogs.Entry) options.after = options.after.id;
|
||||
if (typeof options.type === 'string') options.type = GuildAuditLogs.Actions[options.type];
|
||||
|
||||
return this.client.api.guilds[this.id]['audit-logs'].get({ query: {
|
||||
return this.client.api.guilds(this.id)['audit-logs'].get({ query: {
|
||||
before: options.before,
|
||||
after: options.after,
|
||||
limit: options.limit,
|
||||
@@ -476,7 +476,7 @@ class Guild {
|
||||
options.roles = roles.map(role => role.id);
|
||||
}
|
||||
}
|
||||
return this.client.api.guilds[this.id].members[user.id].put({ data: options })
|
||||
return this.client.api.guilds(this.id).members(user.id).put({ data: options })
|
||||
.then(data => this.client.actions.GuildMemberGet.handle(this, data).member);
|
||||
}
|
||||
|
||||
@@ -490,7 +490,7 @@ class Guild {
|
||||
user = this.client.resolver.resolveUser(user);
|
||||
if (!user) return Promise.reject(new Error('USER_NOT_CACHED'));
|
||||
if (this.members.has(user.id)) return Promise.resolve(this.members.get(user.id));
|
||||
return this.client.api.guilds[this.id].members[user.id].get()
|
||||
return this.client.api.guilds(this.id).members(user.id).get()
|
||||
.then(data => {
|
||||
if (cache) return this.client.actions.GuildMemberGet.handle(this, data).member;
|
||||
else return new GuildMember(this, data);
|
||||
@@ -597,7 +597,7 @@ class Guild {
|
||||
if (typeof data.explicitContentFilter !== 'undefined') {
|
||||
_data.explicit_content_filter = Number(data.explicitContentFilter);
|
||||
}
|
||||
return this.client.api.guilds[this.id].patch({ data: _data, reason })
|
||||
return this.client.api.guilds(this.id).patch({ data: _data, reason })
|
||||
.then(newData => this.client.actions.GuildUpdate.handle(newData).updated);
|
||||
}
|
||||
|
||||
@@ -742,7 +742,7 @@ class Guild {
|
||||
* @returns {Promise<Guild>}
|
||||
*/
|
||||
acknowledge() {
|
||||
return this.client.api.guilds[this.id].ack
|
||||
return this.client.api.guilds(this.id).ack
|
||||
.post({ data: { token: this.client.rest._ackToken } })
|
||||
.then(res => {
|
||||
if (res.token) this.client.rest._ackToken = res.token;
|
||||
@@ -781,7 +781,7 @@ class Guild {
|
||||
if (options.days) options['delete-message-days'] = options.days;
|
||||
const id = this.client.resolver.resolveUserID(user);
|
||||
if (!id) return Promise.reject(new Error('BAN_RESOLVE_ID', true));
|
||||
return this.client.api.guilds[this.id].bans[id].put({ query: options })
|
||||
return this.client.api.guilds(this.id).bans[id].put({ query: options })
|
||||
.then(() => {
|
||||
if (user instanceof GuildMember) return user;
|
||||
const _user = this.client.resolver.resolveUser(id);
|
||||
@@ -830,7 +830,7 @@ class Guild {
|
||||
*/
|
||||
pruneMembers({ days = 7, dry = false, reason } = {}) {
|
||||
if (typeof days !== 'number') throw new TypeError('PRUNE_DAYS_TYPE');
|
||||
return this.client.api.guilds[this.id].prune[dry ? 'get' : 'post']({ query: { days }, reason })
|
||||
return this.client.api.guilds(this.id).prune[dry ? 'get' : 'post']({ query: { days }, reason })
|
||||
.then(data => data.pruned);
|
||||
}
|
||||
|
||||
@@ -865,7 +865,7 @@ class Guild {
|
||||
id: overwrite.id,
|
||||
}));
|
||||
}
|
||||
return this.client.api.guilds[this.id].channels.post({
|
||||
return this.client.api.guilds(this.id).channels.post({
|
||||
data: {
|
||||
name, type, permission_overwrites: overwrites,
|
||||
},
|
||||
@@ -898,7 +898,7 @@ class Guild {
|
||||
};
|
||||
}
|
||||
|
||||
return this.client.api.guilds[this.id].channels.patch({ data: {
|
||||
return this.client.api.guilds(this.id).channels.patch({ data: {
|
||||
guild_id: this.id,
|
||||
channels: channelPositions,
|
||||
} }).then(() =>
|
||||
@@ -936,7 +936,7 @@ class Guild {
|
||||
if (data.color) data.color = Util.resolveColor(data.color);
|
||||
if (data.permissions) data.permissions = Permissions.resolve(data.permissions);
|
||||
|
||||
return this.client.api.guilds[this.id].roles.post({ data, reason }).then(role =>
|
||||
return this.client.api.guilds(this.id).roles.post({ data, reason }).then(role =>
|
||||
this.client.actions.GuildRoleCreate.handle({
|
||||
guild_id: this.id,
|
||||
role,
|
||||
@@ -965,7 +965,7 @@ class Guild {
|
||||
if (typeof attachment === 'string' && attachment.startsWith('data:')) {
|
||||
const data = { image: attachment, name };
|
||||
if (roles) data.roles = roles.map(r => r.id ? r.id : r);
|
||||
return this.client.api.guilds[this.id].emojis.post({ data })
|
||||
return this.client.api.guilds(this.id).emojis.post({ data })
|
||||
.then(emoji => this.client.actions.GuildEmojiCreate.handle(this, emoji).emoji);
|
||||
} else {
|
||||
return this.client.resolver.resolveBuffer(attachment)
|
||||
@@ -983,7 +983,7 @@ class Guild {
|
||||
*/
|
||||
deleteEmoji(emoji) {
|
||||
if (!(emoji instanceof Emoji)) emoji = this.emojis.get(emoji);
|
||||
return this.client.api.guilds(this.id).emojis[emoji.id].delete()
|
||||
return this.client.api.guilds(this.id).emojis(emoji.id).delete()
|
||||
.then(() => this.client.actions.GuildEmojiDelete.handle(emoji).data);
|
||||
}
|
||||
|
||||
@@ -998,7 +998,7 @@ class Guild {
|
||||
*/
|
||||
leave() {
|
||||
if (this.ownerID === this.client.user.id) return Promise.reject(new Error('GUILD_OWNED'));
|
||||
return this.client.api.users['@me'].guilds[this.id].delete()
|
||||
return this.client.api.users('@me').guilds(this.id).delete()
|
||||
.then(() => this.client.actions.GuildDelete.handle({ id: this.id }).guild);
|
||||
}
|
||||
|
||||
@@ -1012,7 +1012,7 @@ class Guild {
|
||||
* .catch(console.error);
|
||||
*/
|
||||
delete() {
|
||||
return this.client.api.guilds[this.id].delete()
|
||||
return this.client.api.guilds(this.id).delete()
|
||||
.then(() => this.client.actions.GuildDelete.handle({ id: this.id }).guild);
|
||||
}
|
||||
|
||||
@@ -1170,7 +1170,7 @@ class Guild {
|
||||
Util.moveElementInArray(updatedRoles, role, position, relative);
|
||||
|
||||
updatedRoles = updatedRoles.map((r, i) => ({ id: r.id, position: i }));
|
||||
return this.client.api.guilds[this.id].roles.patch({ data: updatedRoles })
|
||||
return this.client.api.guilds(this.id).roles.patch({ data: updatedRoles })
|
||||
.then(() =>
|
||||
this.client.actions.GuildRolesPositionUpdate.handle({
|
||||
guild_id: this.id,
|
||||
@@ -1202,7 +1202,7 @@ class Guild {
|
||||
Util.moveElementInArray(updatedChannels, channel, position, relative);
|
||||
|
||||
updatedChannels = updatedChannels.map((r, i) => ({ id: r.id, position: i }));
|
||||
return this.client.api.guilds[this.id].channels.patch({ data: updatedChannels })
|
||||
return this.client.api.guilds(this.id).channels.patch({ data: updatedChannels })
|
||||
.then(() =>
|
||||
this.client.actions.GuildChannelsPositionUpdate.handle({
|
||||
guild_id: this.id,
|
||||
|
||||
@@ -189,7 +189,7 @@ class GuildChannel extends Channel {
|
||||
}
|
||||
}
|
||||
|
||||
return this.client.api.channels[this.id].permissions[payload.id]
|
||||
return this.client.api.channels(this.id).permissions[payload.id]
|
||||
.put({ data: payload, reason })
|
||||
.then(() => this);
|
||||
}
|
||||
@@ -216,7 +216,7 @@ class GuildChannel extends Channel {
|
||||
* .catch(console.error);
|
||||
*/
|
||||
edit(data, reason) {
|
||||
return this.client.api.channels[this.id].patch({
|
||||
return this.client.api.channels(this.id).patch({
|
||||
data: {
|
||||
name: (data.name || this.name).trim(),
|
||||
topic: data.topic || this.topic,
|
||||
@@ -289,7 +289,7 @@ class GuildChannel extends Channel {
|
||||
* @returns {Promise<Invite>}
|
||||
*/
|
||||
createInvite({ temporary = false, maxAge = 86400, maxUses = 0, unique, reason } = {}) {
|
||||
return this.client.api.channels[this.id].invites.post({ data: {
|
||||
return this.client.api.channels(this.id).invites.post({ data: {
|
||||
temporary, max_age: maxAge, max_uses: maxUses, unique,
|
||||
}, reason })
|
||||
.then(invite => new Invite(this.client, invite));
|
||||
@@ -353,7 +353,7 @@ class GuildChannel extends Channel {
|
||||
* .catch(console.error); // Log error
|
||||
*/
|
||||
delete(reason) {
|
||||
return this.client.api.channels[this.id].delete({ reason }).then(() => this);
|
||||
return this.client.api.channels(this.id).delete({ reason }).then(() => this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -342,13 +342,13 @@ class GuildMember {
|
||||
data.channel = null;
|
||||
}
|
||||
if (data.roles) data.roles = data.roles.map(role => role instanceof Role ? role.id : role);
|
||||
let endpoint = this.client.api.guilds[this.guild.id];
|
||||
let endpoint = this.client.api.guilds(this.guild.id);
|
||||
if (this.user.id === this.client.user.id) {
|
||||
const keys = Object.keys(data);
|
||||
if (keys.length === 1 && keys[0] === 'nick') endpoint = endpoint.members['@me'].nick;
|
||||
else endpoint = endpoint.members[this.id];
|
||||
if (keys.length === 1 && keys[0] === 'nick') endpoint = endpoint.members('@me').nick;
|
||||
else endpoint = endpoint.members(this.id);
|
||||
} else {
|
||||
endpoint = endpoint.members[this.id];
|
||||
endpoint = endpoint.members(this.id);
|
||||
}
|
||||
return endpoint.patch({ data, reason }).then(newData => this.guild._updateMember(this, newData).mem);
|
||||
}
|
||||
@@ -398,7 +398,7 @@ class GuildMember {
|
||||
if (!(role instanceof Role)) role = this.guild.roles.get(role);
|
||||
if (!role) return Promise.reject(new TypeError('INVALID_TYPE', 'role', 'Role nor a Snowflake'));
|
||||
if (this._roles.includes(role.id)) return Promise.resolve(this);
|
||||
return this.client.api.guilds[this.guild.id].members[this.user.id].roles[role.id]
|
||||
return this.client.api.guilds(this.guild.id).members(this.user.id).roles(role.id)
|
||||
.put()
|
||||
.then(() => this);
|
||||
}
|
||||
@@ -427,7 +427,7 @@ class GuildMember {
|
||||
removeRole(role) {
|
||||
if (!(role instanceof Role)) role = this.guild.roles.get(role);
|
||||
if (!role) return Promise.reject(new TypeError('INVALID_TYPE', 'role', 'Role nor a Snowflake'));
|
||||
return this.client.api.guilds[this.guild.id].members[this.user.id].roles[role.id]
|
||||
return this.client.api.guilds(this.guild.id).members(this.user.id).roles(role.id)
|
||||
.delete()
|
||||
.then(() => this);
|
||||
}
|
||||
@@ -484,7 +484,7 @@ class GuildMember {
|
||||
* @returns {Promise<GuildMember>}
|
||||
*/
|
||||
kick(reason) {
|
||||
return this.client.api.guilds[this.guild.id].members[this.user.id].delete({ reason })
|
||||
return this.client.api.guilds(this.guild.id).members(this.user.id).delete({ reason })
|
||||
.then(() =>
|
||||
this.client.actions.GuildMemberRemove.handle({
|
||||
guild_id: this.guild.id,
|
||||
|
||||
@@ -413,7 +413,7 @@ class Message {
|
||||
* @returns {Promise<Message>}
|
||||
*/
|
||||
pin() {
|
||||
return this.client.api.channels[this.channel.id].pins[this.id].put()
|
||||
return this.client.api.channels(this.channel.id).pins(this.id).put()
|
||||
.then(() => this);
|
||||
}
|
||||
|
||||
@@ -422,7 +422,7 @@ class Message {
|
||||
* @returns {Promise<Message>}
|
||||
*/
|
||||
unpin() {
|
||||
return this.client.api.channels[this.channel.id].pins[this.id].delete()
|
||||
return this.client.api.channels(this.channel.id).pins(this.id).delete()
|
||||
.then(() => this);
|
||||
}
|
||||
|
||||
@@ -435,7 +435,7 @@ class Message {
|
||||
emoji = this.client.resolver.resolveEmojiIdentifier(emoji);
|
||||
if (!emoji) throw new TypeError('EMOJI_TYPE');
|
||||
|
||||
return this.client.api.channels[this.channel.id].messages[this.id].reactions[emoji]['@me']
|
||||
return this.client.api.channels(this.channel.id).messages(this.id).reactions(emoji, '@me')
|
||||
.put()
|
||||
.then(() => this._addReaction(Util.parseEmoji(emoji), this.client.user));
|
||||
}
|
||||
@@ -445,7 +445,7 @@ class Message {
|
||||
* @returns {Promise<Message>}
|
||||
*/
|
||||
clearReactions() {
|
||||
return this.client.api.channels[this.channel.id].messages[this.id].reactions.delete()
|
||||
return this.client.api.channels(this.channel.id).messages(this.id).reactions.delete()
|
||||
.then(() => this);
|
||||
}
|
||||
|
||||
@@ -463,7 +463,7 @@ class Message {
|
||||
*/
|
||||
delete({ timeout = 0, reason } = {}) {
|
||||
if (timeout <= 0) {
|
||||
return this.client.api.channels[this.channel.id].messages[this.id]
|
||||
return this.client.api.channels(this.channel.id).messages(this.id)
|
||||
.delete({ reason })
|
||||
.then(() =>
|
||||
this.client.actions.MessageDelete.handle({
|
||||
@@ -506,7 +506,7 @@ class Message {
|
||||
* @returns {Promise<Message>}
|
||||
*/
|
||||
acknowledge() {
|
||||
return this.client.api.channels[this.channel.id].messages[this.id].ack
|
||||
return this.client.api.channels(this.channel.id).messages(this.id).ack
|
||||
.post({ data: { token: this.client.rest._ackToken } })
|
||||
.then(res => {
|
||||
if (res.token) this.client.rest._ackToken = res.token;
|
||||
|
||||
@@ -202,7 +202,7 @@ class User {
|
||||
*/
|
||||
createDM() {
|
||||
if (this.dmChannel) return Promise.resolve(this.dmChannel);
|
||||
return this.client.api.users[this.client.user.id].channels.post({ data: {
|
||||
return this.client.api.users(this.client.user.id).channels.post({ data: {
|
||||
recipient_id: this.id,
|
||||
} })
|
||||
.then(data => this.client.actions.ChannelCreate.handle(data).channel);
|
||||
@@ -214,7 +214,7 @@ class User {
|
||||
*/
|
||||
deleteDM() {
|
||||
if (!this.dmChannel) return Promise.reject(new Error('USER_NO_DMCHANNEL'));
|
||||
return this.client.api.channels[this.dmChannel.id].delete()
|
||||
return this.client.api.channels(this.dmChannel.id).delete()
|
||||
.then(data => this.client.actions.ChannelDelete.handle(data).channel);
|
||||
}
|
||||
|
||||
@@ -224,7 +224,7 @@ class User {
|
||||
* @returns {Promise<UserProfile>}
|
||||
*/
|
||||
fetchProfile() {
|
||||
return this.client.api.users[this.id].profile.get().then(data => new UserProfile(this, data));
|
||||
return this.client.api.users(this.id).profile.get().then(data => new UserProfile(this, data));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -234,7 +234,7 @@ class User {
|
||||
* @returns {Promise<User>}
|
||||
*/
|
||||
setNote(note) {
|
||||
return this.client.api.users['@me'].notes[this.id].put({ data: { note } })
|
||||
return this.client.api.users('@me').notes(this.id).put({ data: { note } })
|
||||
.then(() => this);
|
||||
}
|
||||
|
||||
|
||||
@@ -153,7 +153,7 @@ class Webhook {
|
||||
file.file = buffer;
|
||||
return file;
|
||||
})
|
||||
)).then(files => this.client.api.webhooks.opts(this.id, this.token).post({
|
||||
)).then(files => this.client.api.webhooks(this.id, this.token).post({
|
||||
data: options,
|
||||
query: { wait: true },
|
||||
files,
|
||||
@@ -161,7 +161,7 @@ class Webhook {
|
||||
}));
|
||||
}
|
||||
|
||||
return this.client.api.webhooks.opts(this.id, this.token).post({
|
||||
return this.client.api.webhooks(this.id, this.token).post({
|
||||
data: options,
|
||||
query: { wait: true },
|
||||
auth: false,
|
||||
@@ -190,7 +190,7 @@ class Webhook {
|
||||
* }).catch(console.error);
|
||||
*/
|
||||
sendSlackMessage(body) {
|
||||
return this.client.api.webhooks.opts(this.id, this.token).slack.post({
|
||||
return this.client.api.webhooks(this.id, this.token).slack.post({
|
||||
query: { wait: true },
|
||||
auth: false,
|
||||
data: body,
|
||||
@@ -216,7 +216,7 @@ class Webhook {
|
||||
return this.edit({ name, avatar: dataURI }, reason);
|
||||
});
|
||||
}
|
||||
return this.client.api.webhooks.opts(this.id, this.token).patch({
|
||||
return this.client.api.webhooks(this.id, this.token).patch({
|
||||
data: { name, avatar },
|
||||
reason,
|
||||
}).then(data => {
|
||||
@@ -232,7 +232,7 @@ class Webhook {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
delete(reason) {
|
||||
return this.client.api.webhooks.opts(this.id, this.token).delete({ reason });
|
||||
return this.client.api.webhooks(this.id, this.token).delete({ reason });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user