mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
refactor(MessagePayload): rename APIMessage (#5921)
This commit is contained in:
@@ -28,8 +28,8 @@ class InteractionWebhook {
|
||||
/* eslint-disable no-empty-function, valid-jsdoc */
|
||||
/**
|
||||
* Sends a message with this webhook.
|
||||
* @param {string|APIMessage|InteractionReplyOptions} options The content for the reply
|
||||
* @returns {Promise<Message|APIMessageRaw>}
|
||||
* @param {string|MessagePayload|InteractionReplyOptions} options The content for the reply
|
||||
* @returns {Promise<Message|APIMessage>}
|
||||
*/
|
||||
send() {}
|
||||
fetchMessage() {}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
const APIMessage = require('./APIMessage');
|
||||
const Base = require('./Base');
|
||||
const BaseMessageComponent = require('./BaseMessageComponent');
|
||||
const ClientApplication = require('./ClientApplication');
|
||||
@@ -8,6 +7,7 @@ const MessageAttachment = require('./MessageAttachment');
|
||||
const MessageComponentInteractionCollector = require('./MessageComponentInteractionCollector');
|
||||
const Embed = require('./MessageEmbed');
|
||||
const Mentions = require('./MessageMentions');
|
||||
const MessagePayload = require('./MessagePayload');
|
||||
const ReactionCollector = require('./ReactionCollector');
|
||||
const Sticker = require('./Sticker');
|
||||
const { Error } = require('../errors');
|
||||
@@ -26,7 +26,7 @@ const Util = require('../util/Util');
|
||||
class Message extends Base {
|
||||
/**
|
||||
* @param {Client} client The instantiating client
|
||||
* @param {APIMessageRaw} data The data for the message
|
||||
* @param {APIMessage} data The data for the message
|
||||
* @param {TextChannel|DMChannel|NewsChannel} channel The channel the message was sent in
|
||||
*/
|
||||
constructor(client, data, channel) {
|
||||
@@ -300,7 +300,7 @@ class Message extends Base {
|
||||
|
||||
/**
|
||||
* Updates the message and returns the old message.
|
||||
* @param {APIMessageRaw} data Raw Discord message update data
|
||||
* @param {APIMessage} data Raw Discord message update data
|
||||
* @returns {Message}
|
||||
* @private
|
||||
*/
|
||||
@@ -565,7 +565,7 @@ class Message extends Base {
|
||||
|
||||
/**
|
||||
* Edits the content of the message.
|
||||
* @param {string|APIMessage|MessageEditOptions} options The options to provide
|
||||
* @param {string|MessagePayload|MessageEditOptions} options The options to provide
|
||||
* @returns {Promise<Message>}
|
||||
* @example
|
||||
* // Update the content of a message
|
||||
@@ -667,7 +667,7 @@ class Message extends Base {
|
||||
|
||||
/**
|
||||
* Send an inline reply to this message.
|
||||
* @param {string|APIMessage|ReplyMessageOptions} options The options to provide
|
||||
* @param {string|MessagePayload|ReplyMessageOptions} options The options to provide
|
||||
* @returns {Promise<Message|Message[]>}
|
||||
* @example
|
||||
* // Reply to a message
|
||||
@@ -678,10 +678,10 @@ class Message extends Base {
|
||||
reply(options) {
|
||||
let data;
|
||||
|
||||
if (options instanceof APIMessage) {
|
||||
if (options instanceof MessagePayload) {
|
||||
data = options;
|
||||
} else {
|
||||
data = APIMessage.create(this, options, {
|
||||
data = MessagePayload.create(this, options, {
|
||||
reply: {
|
||||
messageReference: this,
|
||||
failIfNotExists: options?.failIfNotExists ?? true,
|
||||
@@ -754,7 +754,7 @@ class Message extends Base {
|
||||
* without checking all the properties, use `message.id === message2.id`, which is much more efficient. This
|
||||
* method allows you to see if there are differences in content, embeds, attachments, nonce and tts properties.
|
||||
* @param {Message} message The message to compare it to
|
||||
* @param {APIMessageRaw} rawData Raw data passed through the WebSocket about this message
|
||||
* @param {APIMessage} rawData Raw data passed through the WebSocket about this message
|
||||
* @returns {boolean}
|
||||
*/
|
||||
equals(message, rawData) {
|
||||
|
||||
@@ -16,7 +16,7 @@ class MessageComponentInteraction extends Interaction {
|
||||
|
||||
/**
|
||||
* The message to which the component was attached
|
||||
* @type {?(Message|APIMessageRaw)}
|
||||
* @type {?(Message|APIMessage)}
|
||||
*/
|
||||
this.message = data.message ? this.channel?.messages.add(data.message) ?? data.message : null;
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ const Util = require('../util/Util');
|
||||
/**
|
||||
* Represents a message to be sent to the API.
|
||||
*/
|
||||
class APIMessage {
|
||||
class MessagePayload {
|
||||
/**
|
||||
* @param {MessageTarget} target - The target for this message to be sent to
|
||||
* @param {MessageOptions|WebhookMessageOptions} options - Options passed in from send
|
||||
@@ -31,7 +31,7 @@ class APIMessage {
|
||||
|
||||
/**
|
||||
* Data sendable to the API
|
||||
* @type {?APIMessageRaw}
|
||||
* @type {?APIMessage}
|
||||
*/
|
||||
this.data = null;
|
||||
|
||||
@@ -119,7 +119,7 @@ class APIMessage {
|
||||
|
||||
/**
|
||||
* Resolves data.
|
||||
* @returns {APIMessage}
|
||||
* @returns {MessagePayload}
|
||||
*/
|
||||
resolveData() {
|
||||
if (this.data) return this;
|
||||
@@ -202,7 +202,7 @@ class APIMessage {
|
||||
|
||||
/**
|
||||
* Resolves files.
|
||||
* @returns {Promise<APIMessage>}
|
||||
* @returns {Promise<MessagePayload>}
|
||||
*/
|
||||
async resolveFiles() {
|
||||
if (this.files) return this;
|
||||
@@ -247,7 +247,7 @@ class APIMessage {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an `APIMessage` from user-level arguments.
|
||||
* Creates a `MessagePayload` from user-level arguments.
|
||||
* @param {MessageTarget} target Target to send to
|
||||
* @param {string|MessageOptions|WebhookMessageOptions} options Options or content to use
|
||||
* @param {MessageOptions|WebhookMessageOptions} [extra={}] - Extra options to add onto specified options
|
||||
@@ -261,7 +261,7 @@ class APIMessage {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = APIMessage;
|
||||
module.exports = MessagePayload;
|
||||
|
||||
/**
|
||||
* A target for a message.
|
||||
@@ -270,6 +270,6 @@ module.exports = APIMessage;
|
||||
*/
|
||||
|
||||
/**
|
||||
* @external APIMessageRaw
|
||||
* @external APIMessage
|
||||
* @see {@link https://discord.com/developers/docs/resources/channel#message-object}
|
||||
*/
|
||||
@@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const APIMessage = require('./APIMessage');
|
||||
const Channel = require('./Channel');
|
||||
const MessagePayload = require('./MessagePayload');
|
||||
const { Error } = require('../errors');
|
||||
const { WebhookTypes } = require('../util/Constants');
|
||||
const DataResolver = require('../util/DataResolver');
|
||||
@@ -109,8 +109,8 @@ class Webhook {
|
||||
|
||||
/**
|
||||
* Sends a message with this webhook.
|
||||
* @param {string|APIMessage|WebhookMessageOptions} options The options to provide
|
||||
* @returns {Promise<Message|APIMessageRaw>}
|
||||
* @param {string|MessagePayload|WebhookMessageOptions} options The options to provide
|
||||
* @returns {Promise<Message|APIMessage>}
|
||||
* @example
|
||||
* // Send a basic message
|
||||
* webhook.send('hello!')
|
||||
@@ -158,21 +158,21 @@ class Webhook {
|
||||
async send(options) {
|
||||
if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE');
|
||||
|
||||
let apiMessage;
|
||||
let messagePayload;
|
||||
|
||||
if (options instanceof APIMessage) {
|
||||
apiMessage = options.resolveData();
|
||||
if (options instanceof MessagePayload) {
|
||||
messagePayload = options.resolveData();
|
||||
} else {
|
||||
apiMessage = APIMessage.create(this, options).resolveData();
|
||||
messagePayload = MessagePayload.create(this, options).resolveData();
|
||||
}
|
||||
|
||||
const { data, files } = await apiMessage.resolveFiles();
|
||||
const { data, files } = await messagePayload.resolveFiles();
|
||||
return this.client.api
|
||||
.webhooks(this.id, this.token)
|
||||
.post({
|
||||
data,
|
||||
files,
|
||||
query: { thread_id: apiMessage.options.threadID, wait: true },
|
||||
query: { thread_id: messagePayload.options.threadID, wait: true },
|
||||
auth: false,
|
||||
})
|
||||
.then(d => {
|
||||
@@ -247,7 +247,7 @@ class Webhook {
|
||||
* Gets a message that was sent by this webhook.
|
||||
* @param {Snowflake|'@original'} message The ID of the message to fetch
|
||||
* @param {boolean} [cache=true] Whether to cache the message
|
||||
* @returns {Promise<Message|APIMessageRaw>} Returns the raw message data if the webhook was instantiated as a
|
||||
* @returns {Promise<Message|APIMessage>} Returns the raw message data if the webhook was instantiated as a
|
||||
* {@link WebhookClient} or if the channel is uncached, otherwise a {@link Message} will be returned
|
||||
*/
|
||||
async fetchMessage(message, cache = true) {
|
||||
@@ -260,19 +260,19 @@ class Webhook {
|
||||
/**
|
||||
* Edits a message that was sent by this webhook.
|
||||
* @param {MessageResolvable|'@original'} message The message to edit
|
||||
* @param {string|APIMessage|WebhookEditMessageOptions} options The options to provide
|
||||
* @returns {Promise<Message|APIMessageRaw>} Returns the raw message data if the webhook was instantiated as a
|
||||
* @param {string|MessagePayload|WebhookEditMessageOptions} options The options to provide
|
||||
* @returns {Promise<Message|APIMessage>} Returns the raw message data if the webhook was instantiated as a
|
||||
* {@link WebhookClient} or if the channel is uncached, otherwise a {@link Message} will be returned
|
||||
*/
|
||||
async editMessage(message, options) {
|
||||
if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE');
|
||||
|
||||
let apiMessage;
|
||||
let messagePayload;
|
||||
|
||||
if (options instanceof APIMessage) apiMessage = options;
|
||||
else apiMessage = APIMessage.create(this, options);
|
||||
if (options instanceof MessagePayload) messagePayload = options;
|
||||
else messagePayload = MessagePayload.create(this, options);
|
||||
|
||||
const { data, files } = await apiMessage.resolveData().resolveFiles();
|
||||
const { data, files } = await messagePayload.resolveData().resolveFiles();
|
||||
|
||||
const d = await this.client.api
|
||||
.webhooks(this.id, this.token)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
const { Error } = require('../../errors');
|
||||
const { InteractionResponseTypes } = require('../../util/Constants');
|
||||
const MessageFlags = require('../../util/MessageFlags');
|
||||
const APIMessage = require('../APIMessage');
|
||||
const MessagePayload = require('../MessagePayload');
|
||||
|
||||
/**
|
||||
* Interface for classes that support shared interaction response types.
|
||||
@@ -53,7 +53,7 @@ class InteractionResponses {
|
||||
|
||||
/**
|
||||
* Creates a reply to this interaction.
|
||||
* @param {string|APIMessage|InteractionReplyOptions} options The options for the reply
|
||||
* @param {string|MessagePayload|InteractionReplyOptions} options The options for the reply
|
||||
* @returns {Promise<void>}
|
||||
* @example
|
||||
* // Reply to the interaction with an embed
|
||||
@@ -72,11 +72,11 @@ class InteractionResponses {
|
||||
if (this.deferred || this.replied) throw new Error('INTERACTION_ALREADY_REPLIED');
|
||||
this.ephemeral = options.ephemeral ?? false;
|
||||
|
||||
let apiMessage;
|
||||
if (options instanceof APIMessage) apiMessage = options;
|
||||
else apiMessage = APIMessage.create(this, options);
|
||||
let messagePayload;
|
||||
if (options instanceof MessagePayload) messagePayload = options;
|
||||
else messagePayload = MessagePayload.create(this, options);
|
||||
|
||||
const { data, files } = await apiMessage.resolveData().resolveFiles();
|
||||
const { data, files } = await messagePayload.resolveData().resolveFiles();
|
||||
|
||||
await this.client.api.interactions(this.id, this.token).callback.post({
|
||||
data: {
|
||||
@@ -91,7 +91,7 @@ class InteractionResponses {
|
||||
/**
|
||||
* Fetches the initial reply to this interaction.
|
||||
* @see Webhook#fetchMessage
|
||||
* @returns {Promise<Message|APIMessageRaw>}
|
||||
* @returns {Promise<Message|APIMessage>}
|
||||
* @example
|
||||
* // Fetch the reply to this interaction
|
||||
* interaction.fetchReply()
|
||||
@@ -106,8 +106,8 @@ class InteractionResponses {
|
||||
/**
|
||||
* Edits the initial reply to this interaction.
|
||||
* @see Webhook#editMessage
|
||||
* @param {string|APIMessage|WebhookEditMessageOptions} options The new options for the message
|
||||
* @returns {Promise<Message|APIMessageRaw>}
|
||||
* @param {string|MessagePayload|WebhookEditMessageOptions} options The new options for the message
|
||||
* @returns {Promise<Message|APIMessage>}
|
||||
* @example
|
||||
* // Edit the reply to this interaction
|
||||
* interaction.editReply('New content')
|
||||
@@ -138,8 +138,8 @@ class InteractionResponses {
|
||||
|
||||
/**
|
||||
* Send a follow-up message to this interaction.
|
||||
* @param {string|APIMessage|InteractionReplyOptions} options The options for the reply
|
||||
* @returns {Promise<Message|APIMessageRaw>}
|
||||
* @param {string|MessagePayload|InteractionReplyOptions} options The options for the reply
|
||||
* @returns {Promise<Message|APIMessage>}
|
||||
*/
|
||||
followUp(options) {
|
||||
return this.webhook.send(options);
|
||||
@@ -166,7 +166,7 @@ class InteractionResponses {
|
||||
|
||||
/**
|
||||
* Updates the original message whose button was pressed
|
||||
* @param {string|APIMessage|WebhookEditMessageOptions} options The options for the reply
|
||||
* @param {string|MessagePayload|WebhookEditMessageOptions} options The options for the reply
|
||||
* @returns {Promise<void>}
|
||||
* @example
|
||||
* // Remove the components from the message
|
||||
@@ -180,11 +180,11 @@ class InteractionResponses {
|
||||
async update(options) {
|
||||
if (this.deferred || this.replied) throw new Error('INTERACTION_ALREADY_REPLIED');
|
||||
|
||||
let apiMessage;
|
||||
if (options instanceof APIMessage) apiMessage = options;
|
||||
else apiMessage = APIMessage.create(this, options);
|
||||
let messagePayload;
|
||||
if (options instanceof MessagePayload) messagePayload = options;
|
||||
else messagePayload = MessagePayload.create(this, options);
|
||||
|
||||
const { data, files } = await apiMessage.resolveData().resolveFiles();
|
||||
const { data, files } = await messagePayload.resolveData().resolveFiles();
|
||||
|
||||
await this.client.api.interactions(this.id, this.token).callback.post({
|
||||
data: {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
/* eslint-disable import/order */
|
||||
const MessageCollector = require('../MessageCollector');
|
||||
const APIMessage = require('../APIMessage');
|
||||
const MessagePayload = require('../MessagePayload');
|
||||
const SnowflakeUtil = require('../../util/SnowflakeUtil');
|
||||
const Collection = require('../../util/Collection');
|
||||
const { RangeError, TypeError, Error } = require('../../errors');
|
||||
@@ -105,7 +105,7 @@ class TextBasedChannel {
|
||||
|
||||
/**
|
||||
* Sends a message to this channel.
|
||||
* @param {string|APIMessage|MessageOptions} options The options to provide
|
||||
* @param {string|MessagePayload|MessageOptions} options The options to provide
|
||||
* @returns {Promise<Message|Message[]>}
|
||||
* @example
|
||||
* // Send a basic message
|
||||
@@ -156,15 +156,15 @@ class TextBasedChannel {
|
||||
return this.createDM().then(dm => dm.send(options));
|
||||
}
|
||||
|
||||
let apiMessage;
|
||||
let messagePayload;
|
||||
|
||||
if (options instanceof APIMessage) {
|
||||
apiMessage = options.resolveData();
|
||||
if (options instanceof MessagePayload) {
|
||||
messagePayload = options.resolveData();
|
||||
} else {
|
||||
apiMessage = APIMessage.create(this, options).resolveData();
|
||||
messagePayload = MessagePayload.create(this, options).resolveData();
|
||||
}
|
||||
|
||||
const { data, files } = await apiMessage.resolveFiles();
|
||||
const { data, files } = await messagePayload.resolveFiles();
|
||||
return this.client.api.channels[this.id].messages
|
||||
.post({ data, files })
|
||||
.then(d => this.client.actions.MessageCreate.handle(d).message);
|
||||
|
||||
Reference in New Issue
Block a user