* 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:
Amish Shah
2019-02-13 17:39:39 +00:00
committed by GitHub
parent 8910fed729
commit 5c3f5d7048
35 changed files with 295 additions and 383 deletions

View File

@@ -434,6 +434,9 @@ class Client extends BaseClient {
if (typeof options.disableEveryone !== 'boolean') {
throw new TypeError('CLIENT_INVALID_OPTION', 'disableEveryone', 'a boolean');
}
if (!(options.partials instanceof Array)) {
throw new TypeError('CLIENT_INVALID_OPTION', 'partials', 'an Array');
}
if (typeof options.restWsBridgeTimeout !== 'number' || isNaN(options.restWsBridgeTimeout)) {
throw new TypeError('CLIENT_INVALID_OPTION', 'restWsBridgeTimeout', 'a number');
}

View File

@@ -1,5 +1,7 @@
'use strict';
const { PartialTypes } = require('../../util/Constants');
/*
ABOUT ACTIONS
@@ -20,6 +22,27 @@ class GenericAction {
handle(data) {
return data;
}
getChannel(data) {
const id = data.channel_id || data.id;
return data.channel || (this.client.options.partials.includes(PartialTypes.CHANNEL) ?
this.client.channels.add({
id,
guild_id: data.guild_id,
}) :
this.client.channels.get(id));
}
getMessage(data, channel) {
const id = data.message_id || data.id;
return data.message || (this.client.options.partials.includes(PartialTypes.MESSAGE) ?
channel.messages.add({
id,
channel_id: channel.id,
guild_id: data.guild_id || (channel.guild ? channel.guild.id : null),
}) :
channel.messages.get(id));
}
}
module.exports = GenericAction;

View File

@@ -12,7 +12,7 @@ class ChannelCreateAction extends Action {
/**
* Emitted whenever a channel is created.
* @event Client#channelCreate
* @param {DMChannel|GroupDMChannel|GuildChannel} channel The channel that was created
* @param {DMChannel|GuildChannel} channel The channel that was created
*/
client.emit(Events.CHANNEL_CREATE, channel);
}

View File

@@ -19,7 +19,7 @@ class ChannelDeleteAction extends Action {
/**
* Emitted whenever a channel is deleted.
* @event Client#channelDelete
* @param {DMChannel|GroupDMChannel|GuildChannel} channel The channel that was deleted
* @param {DMChannel|GuildChannel} channel The channel that was deleted
*/
client.emit(Events.CHANNEL_DELETE, channel);
}

View File

@@ -6,11 +6,10 @@ const { Events } = require('../../util/Constants');
class MessageDeleteAction extends Action {
handle(data) {
const client = this.client;
const channel = client.channels.get(data.channel_id);
const channel = this.getChannel(data);
let message;
if (channel) {
message = channel.messages.get(data.id);
message = this.getMessage(data, channel);
if (message) {
channel.messages.delete(message.id);
message.deleted = true;

View File

@@ -11,15 +11,19 @@ const Action = require('./Action');
class MessageReactionAdd extends Action {
handle(data) {
if (!data.emoji) return false;
const user = data.user || this.client.users.get(data.user_id);
if (!user) return false;
// Verify channel
const channel = data.channel || this.client.channels.get(data.channel_id);
const channel = this.getChannel(data);
if (!channel || channel.type === 'voice') return false;
// Verify message
const message = data.message || channel.messages.get(data.message_id);
const message = this.getMessage(data, channel);
if (!message) return false;
if (!data.emoji) return false;
// Verify reaction
const reaction = message.reactions.add({
emoji: data.emoji,

View File

@@ -12,15 +12,19 @@ const { Events } = require('../../util/Constants');
class MessageReactionRemove extends Action {
handle(data) {
if (!data.emoji) return false;
const user = this.client.users.get(data.user_id);
if (!user) return false;
// Verify channel
const channel = this.client.channels.get(data.channel_id);
const channel = this.getChannel(data);
if (!channel || channel.type === 'voice') return false;
// Verify message
const message = channel.messages.get(data.message_id);
const message = this.getMessage(data, channel);
if (!message) return false;
if (!data.emoji) return false;
// Verify reaction
const emojiID = data.emoji.id || decodeURIComponent(data.emoji.name);
const reaction = message.reactions.get(emojiID);

View File

@@ -5,10 +5,12 @@ const { Events } = require('../../util/Constants');
class MessageReactionRemoveAll extends Action {
handle(data) {
const channel = this.client.channels.get(data.channel_id);
// Verify channel
const channel = this.getChannel(data);
if (!channel || channel.type === 'voice') return false;
const message = channel.messages.get(data.message_id);
// Verify message
const message = this.getMessage(data, channel);
if (!message) return false;
message.reactions.clear();

View File

@@ -4,11 +4,10 @@ const Action = require('./Action');
class MessageUpdateAction extends Action {
handle(data) {
const client = this.client;
const channel = client.channels.get(data.channel_id);
const channel = this.getChannel(data);
if (channel) {
const message = channel.messages.get(data.id);
const { id, channel_id, guild_id, author, timestamp, type } = data;
const message = this.getMessage({ id, channel_id, guild_id, author, timestamp, type }, channel);
if (message) {
message.patch(data);
return {

View File

@@ -14,7 +14,7 @@ module.exports = (client, { d: data }) => {
* Emitted whenever the pins of a channel are updated. Due to the nature of the WebSocket event,
* not much information can be provided easily here - you need to manually check the pins yourself.
* @event Client#channelPinsUpdate
* @param {DMChannel|GroupDMChannel|TextChannel} channel The channel that the pins update occured in
* @param {DMChannel|TextChannel} channel The channel that the pins update occured in
* @param {Date} time The time of the pins update
*/
client.emit(Events.CHANNEL_PINS_UPDATE, channel, time);

View File

@@ -8,8 +8,8 @@ module.exports = (client, packet) => {
/**
* Emitted whenever a channel is updated - e.g. name change, topic change.
* @event Client#channelUpdate
* @param {DMChannel|GroupDMChannel|GuildChannel} oldChannel The channel before the update
* @param {DMChannel|GroupDMChannel|GuildChannel} newChannel The channel after the update
* @param {DMChannel|GuildChannel} oldChannel The channel before the update
* @param {DMChannel|GuildChannel} newChannel The channel after the update
*/
client.emit(Events.CHANNEL_UPDATE, old, updated);
}