Internal API Request Rewrite (#1490)

* start rewrite

* converted guilds

* more changes

* convert GuildMember

* convert User and remove friend methods which kill people

* convert more stuff

* even more stuff

* make things nicer

* speed and fixes and stuff

* almost finished

* fix

* Update Client.js

* uwu

* Update RESTMethods.js

* message editing

* fix router

* fix issue with references

* message delete reason

* move message sending

* fix dm

* message splitting

* NO MORE REST METHODS

* Update Client.js

* Update WebhookClient.js

* remove all those endpoints from the constants

* Update ClientUser.js

* Update ClientUser.js

* fixes

* Update ClientUser.js

* complaiancy

* all sort of fixes

* merge master (#1)

* Fix Permissions now that member is deprecated (#1491)

* removing more deprecation leftovers (#1492)

* Fix MessageCollectors

* Fix awaitMessages (#1493)

* Fix MessageCollector#cleanup

* Fix MessageCollector#postCheck

* Add max option back for safety

* Update Invite.js (#1496)

* guild setPosition missing docs (#1498)

* missing docs

* update return docs

* indent

* switched .invites for the apirouter and invite.js

* make multiple options an object

* Update ClientUser.js

* fix nicks

* Update WebhookClient.js
This commit is contained in:
Gus Caplan
2017-05-21 00:04:19 -05:00
committed by Crawl
parent 02f03c439f
commit 0baa59b679
33 changed files with 849 additions and 1303 deletions

View File

@@ -1,7 +1,8 @@
const path = require('path');
const Message = require('../Message');
const MessageCollector = require('../MessageCollector');
const Shared = require('../shared');
const Collection = require('../../util/Collection');
const Snowflake = require('../../util/Snowflake');
/**
* Interface for classes that have text-channel-like features.
@@ -78,6 +79,8 @@ class TextBasedChannel {
options = {};
}
if (!options.content) options.content = content;
if (options.embed && options.embed.file) options.file = options.embed.file;
if (options.file) {
@@ -106,10 +109,13 @@ class TextBasedChannel {
file.file = buffer;
return file;
})
)).then(files => this.client.rest.methods.sendMessage(this, content, options, files));
)).then(files => {
options.files = files;
return Shared.sendMessage(this, options);
});
}
return this.client.rest.methods.sendMessage(this, content, options);
return Shared.sendMessage(this, options);
}
/**
@@ -125,14 +131,17 @@ class TextBasedChannel {
* .catch(console.error);
*/
fetchMessage(messageID) {
const Message = require('../Message');
if (!this.client.user.bot) {
return this.fetchMessages({ limit: 1, around: messageID }).then(messages => {
return this.fetchMessages({ limit: 1, around: messageID })
.then(messages => {
const msg = messages.get(messageID);
if (!msg) throw new Error('Message not found.');
return msg;
});
}
return this.client.rest.methods.getChannelMessage(this, messageID).then(data => {
return this.client.api.channels(this.id).messages(messageID).get()
.then(data => {
const msg = data instanceof Message ? data : new Message(this, data, this.client);
this._cacheMessage(msg);
return msg;
@@ -160,7 +169,9 @@ class TextBasedChannel {
* .catch(console.error);
*/
fetchMessages(options = {}) {
return this.client.rest.methods.getChannelMessages(this, options).then(data => {
const Message = require('../Message');
return this.client.api.channels(this.id).messages.get({ query: options })
.then(data => {
const messages = new Collection();
for (const message of data) {
const msg = new Message(this, message, this.client);
@@ -176,7 +187,8 @@ class TextBasedChannel {
* @returns {Promise<Collection<Snowflake, Message>>}
*/
fetchPinnedMessages() {
return this.client.rest.methods.getChannelPinnedMessages(this).then(data => {
const Message = require('../Message');
return this.client.api.channels(this.id).pins.get().then(data => {
const messages = new Collection();
for (const message of data) {
const msg = new Message(this, message, this.client);
@@ -231,7 +243,7 @@ class TextBasedChannel {
* }).catch(console.error);
*/
search(options = {}) {
return this.client.rest.methods.search(this, options);
return Shared.search(this, options);
}
/**
@@ -244,13 +256,14 @@ class TextBasedChannel {
startTyping(count) {
if (typeof count !== 'undefined' && count < 1) throw new RangeError('Count must be at least 1.');
if (!this.client.user._typing.has(this.id)) {
const endpoint = this.client.api.channels(this.id).typing;
this.client.user._typing.set(this.id, {
count: count || 1,
interval: this.client.setInterval(() => {
this.client.rest.methods.sendTyping(this.id);
endpoint.post();
}, 9000),
});
this.client.rest.methods.sendTyping(this.id);
endpoint.post();
} else {
const entry = this.client.user._typing.get(this.id);
entry.count = count || entry.count + 1;
@@ -360,8 +373,20 @@ class TextBasedChannel {
bulkDelete(messages, filterOld = false) {
if (!isNaN(messages)) return this.fetchMessages({ limit: messages }).then(msgs => this.bulkDelete(msgs, filterOld));
if (messages instanceof Array || messages instanceof Collection) {
const messageIDs = messages instanceof Collection ? messages.keyArray() : messages.map(m => m.id);
return this.client.rest.methods.bulkDeleteMessages(this, messageIDs, filterOld);
let messageIDs = messages instanceof Collection ? messages.keyArray() : messages.map(m => m.id);
if (filterOld) {
messageIDs = messageIDs.filter(id =>
Date.now() - Snowflake.deconstruct(id).date.getTime() < 1209600000
);
}
return this.rest.api.channels(this.id).messages['bulk-delete']
.post({ data: { messages: messageIDs } })
.then(() =>
this.client.actions.MessageDeleteBulk.handle({
channel_id: this.id,
ids: messageIDs,
}).messages
);
}
throw new TypeError('The messages must be an Array, Collection, or number.');
}
@@ -373,7 +398,12 @@ class TextBasedChannel {
*/
acknowledge() {
if (!this.lastMessageID) return Promise.resolve(this);
return this.client.rest.methods.ackTextChannel(this);
return this.client.api.channels(this.id).messages(this.lastMessageID)
.post({ data: { token: this.client.rest._ackToken } })
.then(res => {
if (res.token) this.client.rest._ackToken = res.token;
return this;
});
}
_cacheMessage(message) {