Skip to content

Commit

Permalink
feat: message forwarding (#10464)
Browse files Browse the repository at this point in the history
* feat: message forwarding

* fix: redundant usage

* feat: add additional snapshot fields

* refactor: use collection to store snapshots

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and Jiralite committed Sep 30, 2024
1 parent 0873f9a commit c122178
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/discord.js/src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ class Message extends Base {
* @property {Snowflake} channelId The channel id that was referenced
* @property {Snowflake|undefined} guildId The guild id that was referenced
* @property {Snowflake|undefined} messageId The message id that was referenced
* @property {MessageReferenceType} type The type of message reference
*/

if ('message_reference' in data) {
Expand All @@ -375,6 +376,7 @@ class Message extends Base {
channelId: data.message_reference.channel_id,
guildId: data.message_reference.guild_id,
messageId: data.message_reference.message_id,
type: data.message_reference.type,
};
} else {
this.reference ??= null;
Expand Down Expand Up @@ -448,6 +450,29 @@ class Message extends Base {
this.poll ??= null;
}

if (data.message_snapshots) {
/**
* The message associated with the message reference
* @type {Collection<Snowflake, Message>}
*/
this.messageSnapshots = data.message_snapshots.reduce((coll, snapshot) => {
const channel = this.client.channels.resolve(this.reference.channelId);
const snapshotData = {
...snapshot.message,
id: this.reference.messageId,
channel_id: this.reference.channelId,
guild_id: this.reference.guildId,
};

return coll.set(
this.reference.messageId,
channel ? channel.messages._add(snapshotData) : new this.constructor(this.client, snapshotData),
);
}, new Collection());
} else {
this.messageSnapshots ??= new Collection();
}

/**
* A call associated with a message
* @typedef {Object} MessageCall
Expand Down
5 changes: 5 additions & 0 deletions packages/discord.js/src/util/APITypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,11 @@
* @see {@link https://discord-api-types.dev/api/discord-api-types-v10/enum/MessageActivityType}
*/

/**
* @external MessageReferenceType
* @see {@link https://discord-api-types.dev/api/discord-api-types-v10/enum/MessageReferenceType}
*/

/**
* @external MessageType
* @see {@link https://discord-api-types.dev/api/discord-api-types-v10/enum/MessageType}
Expand Down
23 changes: 23 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ import {
InviteType,
ReactionType,
APIAuthorizingIntegrationOwnersMap,
MessageReferenceType,
} from 'discord-api-types/v10';
import { ChildProcess } from 'node:child_process';
import { EventEmitter } from 'node:events';
Expand Down Expand Up @@ -2185,6 +2186,7 @@ export class Message<InGuild extends boolean = boolean> extends Base {
public webhookId: Snowflake | null;
public flags: Readonly<MessageFlagsBitField>;
public reference: MessageReference | null;
public messageSnapshots: Collection<Snowflake, MessageSnapshot>;
public awaitMessageComponent<ComponentType extends MessageComponentType>(
options?: AwaitMessageCollectorOptionsParams<ComponentType, InGuild>,
): Promise<MappedInteractionTypes<InGuild>[ComponentType]>;
Expand Down Expand Up @@ -6442,6 +6444,26 @@ export interface MessageMentionOptions {

export type MessageMentionTypes = 'roles' | 'users' | 'everyone';

export interface MessageSnapshot
extends Partialize<
Message,
null,
Exclude<
keyof Message,
| 'attachments'
| 'client'
| 'components'
| 'content'
| 'createdTimestamp'
| 'editedTimestamp'
| 'embeds'
| 'flags'
| 'mentions'
| 'stickers'
| 'type'
>
> {}

export interface BaseMessageOptions {
content?: string;
embeds?: readonly (JSONEncodable<APIEmbed> | APIEmbed)[];
Expand Down Expand Up @@ -6497,6 +6519,7 @@ export interface MessageReference {
channelId: Snowflake;
guildId: Snowflake | undefined;
messageId: Snowflake | undefined;
type: MessageReferenceType;
}

export type MessageResolvable = Message | Snowflake;
Expand Down

0 comments on commit c122178

Please sign in to comment.