revert: "feat(Partials): add DMChannel/MessageReaction#fetch()… (#3468)

This reverts commit b0047c424b.
This commit is contained in:
Crawl
2019-09-10 19:49:56 +02:00
committed by GitHub
parent dad0cd8e81
commit 321beb73bd
9 changed files with 20 additions and 80 deletions

View File

@@ -9,8 +9,8 @@ discard the event. With partials, you're able to receive the event, with a Messa
Partials are opt-in, and you can enable them in the Client options by specifying [PartialTypes](../typedef/PartialType):
```js
// Accept partial messages, DM channels, and reactions when emitting events
new Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
// Accept partial messages and DM channels when emitting events
new Client({ partials: ['MESSAGE', 'CHANNEL'] });
```
## Usage & warnings
@@ -45,10 +45,6 @@ client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
// Now the message has been cached and is fully available:
console.log(`${reaction.message.author}'s message "${reaction.message.content}" gained a reaction!`);
// Fetches and caches the reaction itself, updating resources that were possibly defunct.
if (reaction.partial) await reaction.fetch();
// Now the reaction is fully available and the properties will be reflected accurately:
console.log(`${reaction.count} user(s) have given the same reaction this message!`);
});
```
@@ -62,4 +58,4 @@ bot or any bot that relies on still receiving updates to resources you don't hav
good example.
Currently, the only type of channel that can be uncached is a DM channel, there is no reason why guild channels should
not be cached.
not be cached.

View File

@@ -36,7 +36,6 @@ class GenericAction {
return data.channel || this.getPayload({
id,
guild_id: data.guild_id,
recipients: [data.author || { id: data.user_id }],
}, this.client.channels, id, PartialTypes.CHANNEL);
}
@@ -53,9 +52,9 @@ class GenericAction {
const id = data.emoji.id || decodeURIComponent(data.emoji.name);
return this.getPayload({
emoji: data.emoji,
count: message.partial ? null : 0,
count: 0,
me: user.id === this.client.user.id,
}, message.reactions, id, PartialTypes.REACTION);
}, message.reactions, id, PartialTypes.MESSAGE);
}
getMember(data, guild) {

View File

@@ -26,8 +26,11 @@ class MessageReactionAdd extends Action {
if (!message) return false;
// Verify reaction
const reaction = this.getReaction(data, message, user);
if (!reaction) return false;
const reaction = message.reactions.add({
emoji: data.emoji,
count: 0,
me: user.id === this.client.user.id,
});
reaction._add(user);
/**
* Emitted whenever a reaction is added to a cached message.

View File

@@ -50,28 +50,6 @@ class ReactionStore extends DataStore {
return this.client.api.channels(this.message.channel.id).messages(this.message.id).reactions.delete()
.then(() => this.message);
}
_partial(emoji) {
const id = emoji.id || emoji.name;
const existing = this.get(id);
return !existing || existing.partial;
}
async _fetchReaction(reactionEmoji, cache) {
const id = reactionEmoji.id || reactionEmoji.name;
const existing = this.get(id);
if (!this._partial(reactionEmoji)) return existing;
const data = await this.client.api.channels(this.message.channel.id).messages(this.message.id).get();
if (!data.reactions || !data.reactions.some(r => (r.emoji.id || r.emoji.name) === id)) {
reactionEmoji.reaction._patch({ count: 0 });
this.message.reactions.remove(id);
return existing;
}
for (const reaction of data.reactions) {
if (this._partial(reaction.emoji)) this.add(reaction, cache);
}
return existing;
}
}
module.exports = ReactionStore;

View File

@@ -56,15 +56,7 @@ class DMChannel extends Channel {
* @readonly
*/
get partial() {
return this.lastMessageID === undefined;
}
/**
* Fetch this DMChannel.
* @returns {Promise<DMChannel>}
*/
fetch() {
return this.recipient.createDM();
return !this.recipient;
}
/**

View File

@@ -27,6 +27,12 @@ class MessageReaction {
*/
this.me = data.me;
/**
* The number of people that have given the same reaction
* @type {number}
*/
this.count = data.count || 0;
/**
* The users that have given this reaction, mapped by their ID
* @type {ReactionUserStore<Snowflake, User>}
@@ -34,16 +40,6 @@ class MessageReaction {
this.users = new ReactionUserStore(client, undefined, this);
this._emoji = new ReactionEmoji(this, data.emoji);
this._patch(data);
}
_patch(data) {
/**
* The number of people that have given the same reaction
* @type {?number}
*/
this.count = typeof data.count === 'number' ? data.count : null;
}
/**
@@ -67,36 +63,18 @@ class MessageReaction {
return this._emoji;
}
/**
* Whether or not this reaction is a partial
* @type {boolean}
* @readonly
*/
get partial() {
return this.count === null;
}
/**
* Fetch this reaction.
* @returns {Promise<MessageReaction>}
*/
fetch() {
return this.message.reactions._fetchReaction(this.emoji, true);
}
toJSON() {
return Util.flatten(this, { emoji: 'emojiID', message: 'messageID' });
}
_add(user) {
if (this.partial) return;
this.users.set(user.id, user);
if (!this.me || user.id !== this.message.client.user.id || this.count === 0) this.count++;
if (!this.me) this.me = user.id === this.message.client.user.id;
}
_remove(user) {
if (this.partial) return;
this.users.delete(user.id);
if (!this.me || user.id !== this.message.client.user.id) this.count--;
if (user.id === this.message.client.user.id) this.me = false;

View File

@@ -211,7 +211,7 @@ class User extends Base {
*/
async createDM() {
const { dmChannel } = this;
if (dmChannel && !dmChannel.partial) return dmChannel;
if (dmChannel) return dmChannel;
const data = await this.client.api.users(this.client.user.id).channels.post({ data: {
recipient_id: this.id,
} });

View File

@@ -287,7 +287,6 @@ exports.ShardEvents = {
* * CHANNEL (only affects DMChannels)
* * GUILD_MEMBER
* * MESSAGE
* * REACTION
* <warn>Partials require you to put checks in place when handling data, read the Partials topic listed in the
* sidebar for more information.</warn>
* @typedef {string} PartialType
@@ -297,7 +296,6 @@ exports.PartialTypes = keyMirror([
'CHANNEL',
'GUILD_MEMBER',
'MESSAGE',
'REACTION',
]);
/**

8
typings/index.d.ts vendored
View File

@@ -622,7 +622,6 @@ declare module 'discord.js' {
public messages: MessageStore;
public recipient: User;
public readonly partial: boolean;
public fetch(): Promise<DMChannel>;
}
export class Emoji extends Base {
@@ -1061,13 +1060,11 @@ declare module 'discord.js' {
constructor(client: Client, data: object, message: Message);
private _emoji: GuildEmoji | ReactionEmoji;
public count: number | null;
public count: number;
public readonly emoji: GuildEmoji | ReactionEmoji;
public me: boolean;
public message: Message;
public readonly partial: boolean;
public users: ReactionUserStore;
public fetch(): Promise<MessageReaction>;
public toJSON(): object;
}
@@ -2424,8 +2421,7 @@ declare module 'discord.js' {
type PartialTypes = 'USER'
| 'CHANNEL'
| 'GUILD_MEMBER'
| 'MESSAGE'
| 'REACTION';
| 'MESSAGE';
type PresenceStatus = ClientPresenceStatus | 'offline';