feat(Message*): add missing fields, add support for flag editing (#3795)

This commit is contained in:
SpaceEEC
2020-02-22 12:38:43 +01:00
committed by GitHub
parent 330c410796
commit 7e9c995566
8 changed files with 175 additions and 13 deletions

View File

@@ -22,6 +22,8 @@ const Guild = require('../../structures/Guild');
const VoiceRegion = require('../../structures/VoiceRegion'); const VoiceRegion = require('../../structures/VoiceRegion');
const GuildAuditLogs = require('../../structures/GuildAuditLogs'); const GuildAuditLogs = require('../../structures/GuildAuditLogs');
const MessageFlags = require('../../util/MessageFlags');
class RESTMethods { class RESTMethods {
constructor(restManager) { constructor(restManager) {
this.rest = restManager; this.rest = restManager;
@@ -132,9 +134,11 @@ class RESTMethods {
}); });
} }
updateMessage(message, content, { embed, code, reply } = {}) { updateMessage(message, content, { flags, embed, code, reply } = {}) {
if (typeof content !== 'undefined') content = this.client.resolver.resolveString(content); if (typeof content !== 'undefined') content = this.client.resolver.resolveString(content);
if (typeof flags !== 'undefined') flags = MessageFlags.resolve(flags);
// Wrap everything in a code block // Wrap everything in a code block
if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) { if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) {
content = Util.escapeMarkdown(this.client.resolver.resolveString(content), true); content = Util.escapeMarkdown(this.client.resolver.resolveString(content), true);
@@ -151,7 +155,7 @@ class RESTMethods {
if (embed instanceof RichEmbed) embed = embed._apiTransform(); if (embed instanceof RichEmbed) embed = embed._apiTransform();
return this.rest.makeRequest('patch', Endpoints.Message(message), true, { return this.rest.makeRequest('patch', Endpoints.Message(message), true, {
content, embed, content, embed, flags,
}).then(data => this.client.actions.MessageUpdate.handle(data).updated); }).then(data => this.client.actions.MessageUpdate.handle(data).updated);
} }

View File

@@ -14,6 +14,7 @@ module.exports = {
Constants: require('./util/Constants'), Constants: require('./util/Constants'),
DiscordAPIError: require('./client/rest/DiscordAPIError'), DiscordAPIError: require('./client/rest/DiscordAPIError'),
EvaluatedPermissions: require('./util/Permissions'), EvaluatedPermissions: require('./util/Permissions'),
MessageFlags: require('./util/MessageFlags'),
Permissions: require('./util/Permissions'), Permissions: require('./util/Permissions'),
Snowflake: require('./util/Snowflake'), Snowflake: require('./util/Snowflake'),
SnowflakeUtil: require('./util/Snowflake'), SnowflakeUtil: require('./util/Snowflake'),

View File

@@ -8,6 +8,7 @@ const Util = require('../util/Util');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
const Constants = require('../util/Constants'); const Constants = require('../util/Constants');
const Permissions = require('../util/Permissions'); const Permissions = require('../util/Permissions');
const MessageFlags = require('../util/MessageFlags');
let GuildMember; let GuildMember;
/** /**
@@ -77,7 +78,9 @@ class Message {
/** /**
* A random number or string used for checking message delivery * A random number or string used for checking message delivery
* @type {string} * <warn>This is only received after the message was sent successfully, and
* lost if re-fetched</warn>
* @type {?string}
*/ */
this.nonce = data.nonce; this.nonce = data.nonce;
@@ -85,7 +88,7 @@ class Message {
* Whether or not this message was sent by Discord, not actually a user (e.g. pin notifications) * Whether or not this message was sent by Discord, not actually a user (e.g. pin notifications)
* @type {boolean} * @type {boolean}
*/ */
this.system = data.type === 6; this.system = data.type !== 0;
/** /**
* A list of embeds in the message - e.g. YouTube Player * A list of embeds in the message - e.g. YouTube Player
@@ -128,7 +131,7 @@ class Message {
* All valid mentions that the message contains * All valid mentions that the message contains
* @type {MessageMentions} * @type {MessageMentions}
*/ */
this.mentions = new Mentions(this, data.mentions, data.mention_roles, data.mention_everyone); this.mentions = new Mentions(this, data.mentions, data.mention_roles, data.mention_everyone, data.mention_channels);
/** /**
* ID of the webhook that sent the message, if applicable * ID of the webhook that sent the message, if applicable
@@ -142,6 +145,30 @@ class Message {
*/ */
this.hit = typeof data.hit === 'boolean' ? data.hit : null; this.hit = typeof data.hit === 'boolean' ? data.hit : null;
/**
* Flags that are applied to the message
* @type {Readonly<MessageFlags>}
*/
this.flags = new MessageFlags(data.flags).freeze();
/**
* Reference data sent in a crossposted message.
* @typedef {Object} MessageReference
* @property {string} channelID ID of the channel the message was crossposted from
* @property {?string} guildID ID of the guild the message was crossposted from
* @property {?string} messageID ID of the message that was crossposted
*/
/**
* Message reference data
* @type {?MessageReference}
*/
this.reference = data.message_reference ? {
channelID: data.message_reference.channel_id,
guildID: data.message_reference.guild_id,
messageID: data.message_reference.message_id,
} : null;
/** /**
* The previous versions of the message, sorted with the most recent first * The previous versions of the message, sorted with the most recent first
* @type {Message[]} * @type {Message[]}
@@ -188,8 +215,11 @@ class Message {
this, this,
'mentions' in data ? data.mentions : this.mentions.users, 'mentions' in data ? data.mentions : this.mentions.users,
'mentions_roles' in data ? data.mentions_roles : this.mentions.roles, 'mentions_roles' in data ? data.mentions_roles : this.mentions.roles,
'mention_everyone' in data ? data.mention_everyone : this.mentions.everyone 'mention_everyone' in data ? data.mention_everyone : this.mentions.everyone,
'mention_channels' in data ? data.mention_channels : this.mentions.crosspostedChannels
); );
this.flags = new MessageFlags('flags' in data ? data.flags : 0).freeze();
} }
/** /**
@@ -384,6 +414,7 @@ class Message {
* @typedef {Object} MessageEditOptions * @typedef {Object} MessageEditOptions
* @property {Object} [embed] An embed to be added/edited * @property {Object} [embed] An embed to be added/edited
* @property {string|boolean} [code] Language for optional codeblock formatting to apply * @property {string|boolean} [code] Language for optional codeblock formatting to apply
* @property {MessageFlagsResolvable} [flags] Message flags to apply
*/ */
/** /**
@@ -528,6 +559,23 @@ class Message {
return this.client.fetchWebhook(this.webhookID); return this.client.fetchWebhook(this.webhookID);
} }
/**
* Suppresses or unsuppresses embeds on a message
* @param {boolean} [suppress=true] If the embeds should be suppressed or not
* @returns {Promise<Message>}
*/
suppressEmbeds(suppress = true) {
const flags = new MessageFlags(this.flags.bitfield);
if (suppress) {
flags.add(MessageFlags.FLAGS.SUPPRESS_EMBEDS);
} else {
flags.remove(MessageFlags.FLAGS.SUPPRESS_EMBEDS);
}
return this.edit(undefined, { flags });
}
/** /**
* Used mainly internally. Whether two messages are identical in properties. If you want to compare messages * Used mainly internally. Whether two messages are identical in properties. If you want to compare messages
* without checking all the properties, use `message.id === message2.id`, which is much more efficient. This * without checking all the properties, use `message.id === message2.id`, which is much more efficient. This
@@ -542,12 +590,12 @@ class Message {
if (embedUpdate) return this.id === message.id && this.embeds.length === message.embeds.length; if (embedUpdate) return this.id === message.id && this.embeds.length === message.embeds.length;
let equal = this.id === message.id && let equal = this.id === message.id &&
this.author.id === message.author.id && this.author.id === message.author.id &&
this.content === message.content && this.content === message.content &&
this.tts === message.tts && this.tts === message.tts &&
this.nonce === message.nonce && this.nonce === message.nonce &&
this.embeds.length === message.embeds.length && this.embeds.length === message.embeds.length &&
this.attachments.length === message.attachments.length; this.attachments.length === message.attachments.length;
if (equal && rawData) { if (equal && rawData) {
equal = this.mentions.everyone === message.mentions.everyone && equal = this.mentions.everyone === message.mentions.everyone &&

View File

@@ -1,10 +1,11 @@
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
const { ChannelTypes } = require('../util/Constants');
/** /**
* Keeps track of mentions in a {@link Message}. * Keeps track of mentions in a {@link Message}.
*/ */
class MessageMentions { class MessageMentions {
constructor(message, users, roles, everyone) { constructor(message, users, roles, everyone, crosspostedChannels) {
/** /**
* Whether `@everyone` or `@here` were mentioned * Whether `@everyone` or `@here` were mentioned
* @type {boolean} * @type {boolean}
@@ -87,6 +88,39 @@ class MessageMentions {
* @private * @private
*/ */
this._channels = null; this._channels = null;
/**
* Crossposted channel data.
* @typedef {Object} CrosspostedChannel
* @property {Snowflake} channelID ID of the mentioned channel
* @property {Snowflake} guildID ID of the guild that has the channel
* @property {string} type Type of the channel
* @property {string} name Name of the channel
*/
if (crosspostedChannels) {
if (crosspostedChannels instanceof Collection) {
/**
* A collection of crossposted channels
* @type {Collection<Snowflake, CrosspostedChannel>}
*/
this.crosspostedChannels = new Collection(crosspostedChannels);
} else {
this.crosspostedChannels = new Collection();
const channelTypes = Object.keys(ChannelTypes);
for (const d of crosspostedChannels) {
const type = channelTypes[d.type];
this.crosspostedChannels.set(d.id, {
channelID: d.id,
guildID: d.guild_id,
type: type ? type.toLowerCase() : 'unknown',
name: d.name,
});
}
}
} else {
this.crosspostedChannels = new Collection();
}
} }
/** /**

View File

@@ -52,6 +52,13 @@ class User {
*/ */
this.bot = Boolean(data.bot); this.bot = Boolean(data.bot);
/**
* Whether this is an Official Discord System user (part of the urgent message system)
* @type {?boolean}
* @name User#system
*/
if (typeof data.system !== 'undefined') this.system = Boolean(data.system);
/** /**
* The ID of the last message sent by the user, if one was sent * The ID of the last message sent by the user, if one was sent
* @type {?Snowflake} * @type {?Snowflake}

View File

@@ -507,6 +507,7 @@ exports.WSEvents = {
* * USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_1 * * USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_1
* * USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_2 * * USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_2
* * USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3 * * USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3
* * CHANNEL_FOLLOW_ADD
* @typedef {string} MessageType * @typedef {string} MessageType
*/ */
exports.MessageTypes = [ exports.MessageTypes = [
@@ -522,6 +523,7 @@ exports.MessageTypes = [
'USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_1', 'USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_1',
'USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_2', 'USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_2',
'USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3', 'USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3',
'CHANNEL_FOLLOW_ADD',
]; ];
/** /**

36
src/util/MessageFlags.js Normal file
View File

@@ -0,0 +1,36 @@
const BitField = require('./BitField');
/**
* Data structure that makes it easy to interact with an {@link Message#flags} bitfield.
* @extends {BitField}
*/
class MessageFlags extends BitField {}
/**
* Data that can be resolved to give a permission number. This can be:
* * A string (see {@link MessageFlags.FLAGS})
* * A message flag
* * An instance of MessageFlags
* * An array of MessageFlagsResolvable
* @typedef {string|number|MessageFlags|MessageFlagsResolvable[]} MessageFlagsResolvable
*/
/**
* Numeric message flags. All available properties:
* * `CROSSPOSTED`
* * `IS_CROSSPOST`
* * `SUPPRESS_EMBEDS`
* * `SOURCE_MESSAGE_DELETED`
* * `URGENT`
* @type {Object}
* @see {@link https://discordapp.com/developers/docs/resources/channel#message-object-message-flags}
*/
MessageFlags.FLAGS = {
CROSSPOSTED: 1 << 0,
IS_CROSSPOST: 1 << 1,
SUPPRESS_EMBEDS: 1 << 2,
SOURCE_MESSAGE_DELETED: 1 << 3,
URGENT: 1 << 4,
};
module.exports = MessageFlags;

30
typings/index.d.ts vendored
View File

@@ -804,6 +804,7 @@ declare module 'discord.js' {
public editedTimestamp: number; public editedTimestamp: number;
public readonly edits: Message[]; public readonly edits: Message[];
public embeds: MessageEmbed[]; public embeds: MessageEmbed[];
public flags: Readonly<MessageFlags>;
public readonly guild: Guild; public readonly guild: Guild;
public hit: boolean; public hit: boolean;
public id: Snowflake; public id: Snowflake;
@@ -813,6 +814,7 @@ declare module 'discord.js' {
public readonly pinnable: boolean; public readonly pinnable: boolean;
public pinned: boolean; public pinned: boolean;
public reactions: Collection<Snowflake, MessageReaction>; public reactions: Collection<Snowflake, MessageReaction>;
public reference: MessageReference | null;
public system: boolean; public system: boolean;
public tts: boolean; public tts: boolean;
public type: string; public type: string;
@@ -833,6 +835,7 @@ declare module 'discord.js' {
public react(emoji: string | Emoji | ReactionEmoji): Promise<MessageReaction>; public react(emoji: string | Emoji | ReactionEmoji): Promise<MessageReaction>;
public reply(content?: StringResolvable, options?: MessageOptions): Promise<Message | Message[]>; public reply(content?: StringResolvable, options?: MessageOptions): Promise<Message | Message[]>;
public reply(options?: MessageOptions): Promise<Message | Message[]>; public reply(options?: MessageOptions): Promise<Message | Message[]>;
public suppressEmbeds(suppress?: boolean): Promise<Message>;
public toString(): string; public toString(): string;
public unpin(): Promise<Message>; public unpin(): Promise<Message>;
} }
@@ -940,6 +943,11 @@ declare module 'discord.js' {
public width: number; public width: number;
} }
export class MessageFlags extends BitField<MessageFlagsString> {
public static FLAGS: Record<MessageFlagsString, number>;
public static resolve(bit?: BitFieldResolvable<MessageFlagsString>): number;
}
export class MessageMentions { export class MessageMentions {
private _channels: Collection<Snowflake, GuildChannel>; private _channels: Collection<Snowflake, GuildChannel>;
private _client: Client; private _client: Client;
@@ -948,6 +956,7 @@ declare module 'discord.js' {
private _members: Collection<Snowflake, GuildMember>; private _members: Collection<Snowflake, GuildMember>;
public readonly channels: Collection<Snowflake, TextChannel>; public readonly channels: Collection<Snowflake, TextChannel>;
public crosspostedChannels: Collection<Snowflake, CrosspostedChannel>;
public everyone: boolean; public everyone: boolean;
public readonly members: Collection<Snowflake, GuildMember>; public readonly members: Collection<Snowflake, GuildMember>;
public roles: Collection<Snowflake, Role>; public roles: Collection<Snowflake, Role>;
@@ -1361,6 +1370,7 @@ declare module 'discord.js' {
public lastMessageID: string; public lastMessageID: string;
public readonly note: string; public readonly note: string;
public readonly presence: Presence; public readonly presence: Presence;
public system?: boolean;
public readonly tag: string; public readonly tag: string;
public username: string; public username: string;
public addFriend(): Promise<User>; public addFriend(): Promise<User>;
@@ -1849,6 +1859,13 @@ declare module 'discord.js' {
| number | number
| string; | string;
type CrosspostedChannel = {
channelID: Snowflake;
guildID: Snowflake;
type: Channel["type"] | 'unknown';
name: string;
};
type DeconstructedSnowflake = { type DeconstructedSnowflake = {
timestamp: number; timestamp: number;
date: Date; date: Date;
@@ -2040,8 +2057,15 @@ declare module 'discord.js' {
type MessageEditOptions = { type MessageEditOptions = {
embed?: RichEmbedOptions; embed?: RichEmbedOptions;
code?: string | boolean; code?: string | boolean;
flags?: BitFieldResolvable<MessageFlagsString>;
}; };
type MessageFlagsString = 'CROSSPOSTED'
| 'IS_CROSSPOST'
| 'SUPRRESS_EMBEDS'
| 'SOURCE_MESSAGE_DELETED'
| 'URGENT';
type MessageNotifications = 'EVERYTHING' type MessageNotifications = 'EVERYTHING'
| 'MENTIONS' | 'MENTIONS'
| 'NOTHING'; | 'NOTHING';
@@ -2100,6 +2124,12 @@ declare module 'discord.js' {
nsfw?: boolean; nsfw?: boolean;
}; };
type MessageReference = {
channelID: Snowflake;
guildID: Snowflake | null;
messageID: Snowflake | null;
};
type MessageSearchResult = { type MessageSearchResult = {
totalResults: number; totalResults: number;
messages: Message[][]; messages: Message[][];