From 29b0818806daac5150db980e49ccf687f8bbe473 Mon Sep 17 00:00:00 2001
From: Jiralite <33201955+Jiralite@users.noreply.github.com>
Date: Thu, 17 Oct 2024 20:49:41 +0100
Subject: [PATCH] refactor: remove ephemeral
---
.../src/structures/MessagePayload.js | 21 +----------------
.../interfaces/InteractionResponses.js | 23 +++++++++----------
.../src/util/MessageFlagsBitField.js | 9 ++++++++
packages/discord.js/typings/index.d.ts | 11 +++++----
4 files changed, 28 insertions(+), 36 deletions(-)
diff --git a/packages/discord.js/src/structures/MessagePayload.js b/packages/discord.js/src/structures/MessagePayload.js
index dad8ffedb026..7dc567924ba3 100644
--- a/packages/discord.js/src/structures/MessagePayload.js
+++ b/packages/discord.js/src/structures/MessagePayload.js
@@ -1,17 +1,14 @@
'use strict';
const { Buffer } = require('node:buffer');
-const { lazy, isJSONEncodable } = require('@discordjs/util');
+const { isJSONEncodable } = require('@discordjs/util');
const { DiscordSnowflake } = require('@sapphire/snowflake');
-const { MessageFlags } = require('discord-api-types/v10');
const ActionRowBuilder = require('./ActionRowBuilder');
const { DiscordjsError, DiscordjsRangeError, ErrorCodes } = require('../errors');
const { resolveFile } = require('../util/DataResolver');
const MessageFlagsBitField = require('../util/MessageFlagsBitField');
const { basename, verifyString, resolvePartialEmoji } = require('../util/Util');
-const getBaseInteraction = lazy(() => require('./BaseInteraction'));
-
/**
* Represents a message to be sent to the API.
*/
@@ -88,17 +85,6 @@ class MessagePayload {
return this.target instanceof MessageManager;
}
- /**
- * Whether or not the target is an {@link BaseInteraction} or an {@link InteractionWebhook}
- * @type {boolean}
- * @readonly
- */
- get isInteraction() {
- const BaseInteraction = getBaseInteraction();
- const InteractionWebhook = require('./InteractionWebhook');
- return this.target instanceof BaseInteraction || this.target instanceof InteractionWebhook;
- }
-
/**
* Makes the content of this message.
* @returns {?string}
@@ -120,7 +106,6 @@ class MessagePayload {
*/
resolveBody() {
if (this.body) return this;
- const isInteraction = this.isInteraction;
const isWebhook = this.isWebhook;
const content = this.makeContent();
@@ -175,10 +160,6 @@ class MessagePayload {
: this.target.flags?.bitfield;
}
- if (isInteraction && this.options.ephemeral) {
- flags |= MessageFlags.Ephemeral;
- }
-
let allowedMentions =
this.options.allowedMentions === undefined
? this.target.client.options.allowedMentions
diff --git a/packages/discord.js/src/structures/interfaces/InteractionResponses.js b/packages/discord.js/src/structures/interfaces/InteractionResponses.js
index 5c8900530f61..7b1cfb1429f0 100644
--- a/packages/discord.js/src/structures/interfaces/InteractionResponses.js
+++ b/packages/discord.js/src/structures/interfaces/InteractionResponses.js
@@ -3,7 +3,6 @@
const { isJSONEncodable } = require('@discordjs/util');
const { InteractionResponseType, MessageFlags, Routes, InteractionType } = require('discord-api-types/v10');
const { DiscordjsError, ErrorCodes } = require('../../errors');
-const MessageFlagsBitField = require('../../util/MessageFlagsBitField');
const InteractionCollector = require('../InteractionCollector');
const InteractionResponse = require('../InteractionResponse');
const MessagePayload = require('../MessagePayload');
@@ -23,7 +22,8 @@ class InteractionResponses {
/**
* Options for deferring the reply to an {@link BaseInteraction}.
* @typedef {Object} InteractionDeferReplyOptions
- * @property {boolean} [ephemeral] Whether the reply should be ephemeral
+ * @property {MessageFlagsResolvable} [flags] Flags for the reply.
+ * Only `MessageFlags.Ephemeral` can be set.
* @property {boolean} [fetchReply] Whether to fetch the reply
*/
@@ -37,9 +37,8 @@ class InteractionResponses {
* Options for a reply to a {@link BaseInteraction}.
* @typedef {BaseMessageOptionsWithPoll} InteractionReplyOptions
* @property {boolean} [tts=false] Whether the message should be spoken aloud
- * @property {boolean} [ephemeral] Whether the reply should be ephemeral
* @property {boolean} [fetchReply] Whether to fetch the reply
- * @property {MessageFlags} [flags] Which flags to set for the message.
+ * @property {MessageFlagsResolvable} [flags] Which flags to set for the message.
* Only `MessageFlags.Ephemeral`, `MessageFlags.SuppressEmbeds`, and `MessageFlags.SuppressNotifications`
* can be set.
*/
@@ -61,24 +60,25 @@ class InteractionResponses {
* .catch(console.error)
* @example
* // Defer to send an ephemeral reply later
- * interaction.deferReply({ ephemeral: true })
+ * interaction.deferReply({ flags: MessageFlags.Ephemeral })
* .then(console.log)
* .catch(console.error);
*/
async deferReply(options = {}) {
if (this.deferred || this.replied) throw new DiscordjsError(ErrorCodes.InteractionAlreadyReplied);
- this.ephemeral = options.ephemeral ?? false;
+
await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
body: {
type: InteractionResponseType.DeferredChannelMessageWithSource,
data: {
- flags: options.ephemeral ? MessageFlags.Ephemeral : undefined,
+ flags: options.flags,
},
},
auth: false,
});
- this.deferred = true;
+ this.deferred = true;
+ this.ephemeral = Boolean(options.flags & MessageFlags.Ephemeral);
return options.fetchReply ? this.fetchReply() : new InteractionResponse(this);
}
@@ -96,7 +96,7 @@ class InteractionResponses {
* // Create an ephemeral reply with an embed
* const embed = new EmbedBuilder().setDescription('Pong!');
*
- * interaction.reply({ embeds: [embed], ephemeral: true })
+ * interaction.reply({ embeds: [embed], flags: MessageFlags.Ephemeral })
* .then(() => console.log('Reply sent.'))
* .catch(console.error);
*/
@@ -109,8 +109,6 @@ class InteractionResponses {
const { body: data, files } = await messagePayload.resolveBody().resolveFiles();
- this.ephemeral = new MessageFlagsBitField(data.flags).has(MessageFlags.Ephemeral);
-
await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
body: {
type: InteractionResponseType.ChannelMessageWithSource,
@@ -119,8 +117,9 @@ class InteractionResponses {
files,
auth: false,
});
- this.replied = true;
+ this.ephemeral = Boolean(options.flags & MessageFlags.Ephemeral);
+ this.replied = true;
return options.fetchReply ? this.fetchReply() : new InteractionResponse(this);
}
diff --git a/packages/discord.js/src/util/MessageFlagsBitField.js b/packages/discord.js/src/util/MessageFlagsBitField.js
index 71f1fd6c5c08..e6764b2fd791 100644
--- a/packages/discord.js/src/util/MessageFlagsBitField.js
+++ b/packages/discord.js/src/util/MessageFlagsBitField.js
@@ -23,6 +23,15 @@ class MessageFlagsBitField extends BitField {
* @param {BitFieldResolvable} [bits=0] Bit(s) to read from
*/
+/**
+ * Data that can be resolved to give a message flags bit field. This can be:
+ * * A string (see {@link MessageFlagsBitField.Flags})
+ * * A message flag
+ * * An instance of {@link MessageFlagsBitField}
+ * * An array of `MessageFlagsResolvable`
+ * @typedef {string|number|MessageFlagsBitField|MessageFlagsResolvable[]} MessageFlagsResolvable
+ */
+
/**
* Bitfield of the packed bits
* @type {number}
diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts
index 2ac75da47a11..5d429506c935 100644
--- a/packages/discord.js/typings/index.d.ts
+++ b/packages/discord.js/typings/index.d.ts
@@ -2398,7 +2398,6 @@ export class MessagePayload {
public get isWebhook(): boolean;
public get isMessage(): boolean;
public get isMessageManager(): boolean;
- public get isInteraction(): boolean;
public files: RawFile[] | null;
public options: MessagePayloadOption;
public target: MessageTarget;
@@ -6066,15 +6065,19 @@ export interface InteractionCollectorOptions<
}
export interface InteractionDeferReplyOptions {
- ephemeral?: boolean;
+ flags?: BitFieldResolvable<
+ Extract,
+ MessageFlags.Ephemeral | MessageFlags.SuppressEmbeds | MessageFlags.SuppressNotifications
+ >;
fetchReply?: boolean;
}
-export interface InteractionDeferUpdateOptions extends Omit {}
+export interface InteractionDeferUpdateOptions {
+ fetchReply?: boolean;
+}
export interface InteractionReplyOptions extends BaseMessageOptionsWithPoll {
tts?: boolean;
- ephemeral?: boolean;
fetchReply?: boolean;
flags?: BitFieldResolvable<
Extract,