Skip to content

Commit

Permalink
refactor: remove ephemeral
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiralite committed Oct 17, 2024
1 parent 62fb9de commit 29b0818
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
21 changes: 1 addition & 20 deletions packages/discord.js/src/structures/MessagePayload.js
Original file line number Diff line number Diff line change
@@ -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.
*/
Expand Down Expand Up @@ -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}
Expand All @@ -120,7 +106,6 @@ class MessagePayload {
*/
resolveBody() {
if (this.body) return this;
const isInteraction = this.isInteraction;
const isWebhook = this.isWebhook;

const content = this.makeContent();
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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.
* <info>Only `MessageFlags.Ephemeral` can be set.</info>
* @property {boolean} [fetchReply] Whether to fetch the reply
*/

Expand All @@ -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.
* <info>Only `MessageFlags.Ephemeral`, `MessageFlags.SuppressEmbeds`, and `MessageFlags.SuppressNotifications`
* can be set.</info>
*/
Expand All @@ -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);
}

Expand All @@ -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);
*/
Expand All @@ -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,
Expand All @@ -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);
}

Expand Down
9 changes: 9 additions & 0 deletions packages/discord.js/src/util/MessageFlagsBitField.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
11 changes: 7 additions & 4 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -6066,15 +6065,19 @@ export interface InteractionCollectorOptions<
}

export interface InteractionDeferReplyOptions {
ephemeral?: boolean;
flags?: BitFieldResolvable<
Extract<MessageFlagsString, 'Ephemeral' | 'SuppressEmbeds' | 'SuppressNotifications'>,
MessageFlags.Ephemeral | MessageFlags.SuppressEmbeds | MessageFlags.SuppressNotifications
>;
fetchReply?: boolean;
}

export interface InteractionDeferUpdateOptions extends Omit<InteractionDeferReplyOptions, 'ephemeral'> {}
export interface InteractionDeferUpdateOptions {
fetchReply?: boolean;
}

export interface InteractionReplyOptions extends BaseMessageOptionsWithPoll {
tts?: boolean;
ephemeral?: boolean;
fetchReply?: boolean;
flags?: BitFieldResolvable<
Extract<MessageFlagsString, 'Ephemeral' | 'SuppressEmbeds' | 'SuppressNotifications'>,
Expand Down

0 comments on commit 29b0818

Please sign in to comment.