mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 17:13:31 +01:00
Partials (#3070)
* Remove GroupDMChannels they sparked no joy * Start partials for message deletion * MessageUpdate partials * Add partials as an opt-in client option * Add fetch() to Message * Message.author should never be undefined * Fix channels being the wrong type * Allow fetching channels * Refactor and add reaction add partials * Reaction remove partials * Check for emoji first * fix message fetching janky * User partials in audit logs * refactor overwrite code * guild member partials * partials as a whitelist * document GuildMember#fetch * fix: check whether a structure is a partial, not whether cache is true * typings: Updated for latest commit (#3075) * partials: fix messageUpdate behaviour (now "old" message can be partial) * partials: add warnings and docs * partials: add partials to index.yml * partials: tighten "partial" definitions * partials: fix embed-only messages counting as partials
This commit is contained in:
@@ -5,7 +5,7 @@ const Channel = require('../structures/Channel');
|
||||
const { Events } = require('../util/Constants');
|
||||
|
||||
const kLru = Symbol('LRU');
|
||||
const lruable = ['group', 'dm'];
|
||||
const lruable = ['dm'];
|
||||
|
||||
/**
|
||||
* Stores channels.
|
||||
@@ -54,6 +54,7 @@ class ChannelStore extends DataStore {
|
||||
|
||||
add(data, guild, cache = true) {
|
||||
const existing = this.get(data.id);
|
||||
if (existing && existing.partial && cache) existing._patch(data);
|
||||
if (existing) return existing;
|
||||
|
||||
const channel = Channel.create(this.client, data, guild);
|
||||
@@ -85,11 +86,12 @@ class ChannelStore extends DataStore {
|
||||
* .then(channel => console.log(channel.name))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
fetch(id, cache = true) {
|
||||
async fetch(id, cache = true) {
|
||||
const existing = this.get(id);
|
||||
if (existing) return Promise.resolve(existing);
|
||||
if (existing && !existing.partial) return existing;
|
||||
|
||||
return this.client.api.channels(id).get().then(data => this.add(data, null, cache));
|
||||
const data = await this.client.api.channels(id).get();
|
||||
return this.add(data, null, cache);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,6 +18,7 @@ class DataStore extends Collection {
|
||||
|
||||
add(data, cache = true, { id, extras = [] } = {}) {
|
||||
const existing = this.get(id || data.id);
|
||||
if (existing && existing.partial && cache && existing._patch) existing._patch(data);
|
||||
if (existing) return existing;
|
||||
|
||||
const entry = this.holds ? new this.holds(this.client, data, ...extras) : data;
|
||||
|
||||
@@ -180,7 +180,7 @@ class GuildMemberStore extends DataStore {
|
||||
|
||||
_fetchSingle({ user, cache }) {
|
||||
const existing = this.get(user);
|
||||
if (existing && existing.joinedTimestamp) return Promise.resolve(existing);
|
||||
if (existing && !existing.partial) return Promise.resolve(existing);
|
||||
return this.client.api.guilds(this.guild.id).members(user).get()
|
||||
.then(data => this.add(data, cache));
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ class MessageStore extends DataStore {
|
||||
* <info>The returned Collection does not contain reaction users of the messages if they were not cached.
|
||||
* Those need to be fetched separately in such a case.</info>
|
||||
* @param {Snowflake|ChannelLogsQueryOptions} [message] The ID of the message to fetch, or query parameters.
|
||||
* @param {boolean} [cache=true] Whether to cache the message(s)
|
||||
* @returns {Promise<Message>|Promise<Collection<Snowflake, Message>>}
|
||||
* @example
|
||||
* // Get message
|
||||
@@ -57,8 +58,8 @@ class MessageStore extends DataStore {
|
||||
* .then(messages => console.log(`${messages.filter(m => m.author.id === '84484653687267328').size} messages`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
fetch(message) {
|
||||
return typeof message === 'string' ? this._fetchId(message) : this._fetchMany(message);
|
||||
fetch(message, cache = true) {
|
||||
return typeof message === 'string' ? this._fetchId(message, cache) : this._fetchMany(message, cache);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,15 +81,17 @@ class MessageStore extends DataStore {
|
||||
});
|
||||
}
|
||||
|
||||
async _fetchId(messageID) {
|
||||
async _fetchId(messageID, cache) {
|
||||
const existing = this.get(messageID);
|
||||
if (existing && !existing.partial) return existing;
|
||||
const data = await this.client.api.channels[this.channel.id].messages[messageID].get();
|
||||
return this.add(data);
|
||||
return this.add(data, cache);
|
||||
}
|
||||
|
||||
async _fetchMany(options = {}) {
|
||||
async _fetchMany(options = {}, cache) {
|
||||
const data = await this.client.api.channels[this.channel.id].messages.get({ query: options });
|
||||
const messages = new Collection();
|
||||
for (const message of data) messages.set(message.id, this.add(message));
|
||||
for (const message of data) messages.set(message.id, this.add(message, cache));
|
||||
return messages;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,11 +51,11 @@ class UserStore extends DataStore {
|
||||
* @param {boolean} [cache=true] Whether to cache the new user object if it isn't already
|
||||
* @returns {Promise<User>}
|
||||
*/
|
||||
fetch(id, cache = true) {
|
||||
async fetch(id, cache = true) {
|
||||
const existing = this.get(id);
|
||||
if (existing) return Promise.resolve(existing);
|
||||
|
||||
return this.client.api.users(id).get().then(data => this.add(data, cache));
|
||||
if (existing && !existing.partial) return existing;
|
||||
const data = await this.client.api.users(id).get();
|
||||
return this.add(data, cache);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user