Skip to content

Commit

Permalink
Message Forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
DonovanDMC committed Jul 16, 2024
1 parent 8644f94 commit dc48df8
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 14 deletions.
56 changes: 42 additions & 14 deletions lib/structures/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ import type {
RoleSubscriptionData,
MessageInteractionMetadata,
GetPollAnswerUsersOptions,
Call
Call,
MessageSnapshot,
MessageMentions
} from "../types/channels";
import type { RawMember } from "../types/guilds";
import type { DeleteWebhookMessageOptions, EditWebhookMessageOptions } from "../types/webhooks";
Expand Down Expand Up @@ -95,20 +97,11 @@ export default class Message<T extends AnyTextableChannel | Uncached = AnyTextab
/** Channels mentioned in a `CROSSPOSTED` channel follower message. See [Discord's docs](https://discord.com/developers/docs/resources/channel#channel-mention-object) for more information. */
mentionChannels?: Array<ChannelMention>;
/** The mentions in this message. */
mentions: {
/** The ids of the channels mentioned in this message. */
channels: Array<string>;
/** If @everyone/@here is mentioned in this message. */
everyone: boolean;
/** The members mentioned in this message. */
members: Array<Member>;
/** The ids of the roles mentioned in this message. */
roles: Array<string>;
/** The users mentioned in this message. */
users: Array<User>;
};
mentions: MessageMentions;
/** If this message is a `REPLY` or `THREAD_STARTER_MESSAGE`, some info about the referenced message. */
messageReference?: MessageReference;
/** If this message is a forwarded message, the partial contents of that message. */
messageSnapshots?: Array<MessageSnapshot>;
/** A nonce for ensuring a message was sent. */
nonce?: number | string;
/** If this message is pinned. */
Expand Down Expand Up @@ -279,6 +272,25 @@ export default class Message<T extends AnyTextableChannel | Uncached = AnyTextab
};
}

if (data.message_snapshots) {
this.messageSnapshots = data.message_snapshots.map(s => ({
message: {
attachments: s.message.attachments.map(a => new Attachment(a, this.client)),
content: s.message.content,
editedTimestamp: s.message.edited_timestamp ? new Date(s.message.edited_timestamp) : null,
embeds: this.client.util.embedsToParsed(s.message.embeds),
flags: s.message.flags ?? 0,
mentions: {
channels: (s.message.content.match(/<#\d{17,21}>/g) ?? []).map(mention => mention.slice(2, -1)),
roles: s.message.mention_roles,
users: s.message.mentions.map(u => this.client.users.update(u))
},
timestamp: new Date(s.message.timestamp),
type: s.message.type
}
}));
}

if (data.nonce !== undefined) {
this.nonce = data.nonce;
}
Expand Down Expand Up @@ -533,7 +545,23 @@ export default class Message<T extends AnyTextableChannel | Uncached = AnyTextab
roles: this.mentions.roles,
users: this.mentions.users.map(user => user.toJSON())
},
messageReference: this.messageReference,
messageReference: this.messageReference,
messageSnapshots: this.messageSnapshots?.map(s => ({
message: {
attachments: s.message.attachments.map(a => a.toJSON()),
content: s.message.content,
editedTimestamp: s.message.editedTimestamp?.getTime() ?? null,
embeds: s.message.embeds,
flags: s.message.flags,
mentions: {
channels: s.message.mentions.channels,
roles: s.message.mentions.roles,
users: s.message.mentions.users.map(u => u.toJSON())
},
timestamp: s.message.timestamp.getTime(),
type: s.message.type
}
})),
nonce: this.nonce,
pinned: this.pinned,
position: this.position,
Expand Down
35 changes: 35 additions & 0 deletions lib/types/channels.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import type ForumChannel from "../structures/ForumChannel";
import type Message from "../structures/Message";
import type Guild from "../structures/Guild";
import type Invite from "../structures/Invite";
import type Attachment from "../structures/Attachment";

export interface RawChannel {
application_id?: string;
Expand Down Expand Up @@ -704,6 +705,7 @@ export interface RawMessage {
mention_roles: Array<string>;
mentions: Array<RawUserWithMember>;
message_reference?: RawMessageReference;
message_snapshots?: Array<RawMessageSnapshot>;
nonce?: number | string;
pinned: boolean;
poll?: RawPoll;
Expand Down Expand Up @@ -1271,3 +1273,36 @@ export interface EventReaction {
emoji: PartialEmoji;
type: ReactionType;
}

export interface RawMessageSnapshotMessage extends Pick<RawMessage, "type" | "content" | "embeds" | "attachments" | "timestamp" | "edited_timestamp" | "flags" | "mentions" | "mention_roles"> {}

export interface RawMessageSnapshot {
message: RawMessageSnapshotMessage;
}
export interface MessageSnapshotMessage {
attachments: Array<Attachment>;
content: string;
editedTimestamp: Date | null;
embeds: Array<Embed>;
flags: number;
mentions: Omit<MessageMentions, "everyone" | "members">;
timestamp: Date;
type: MessageTypes;
}

export interface MessageSnapshot {
message: MessageSnapshotMessage;
}

export interface MessageMentions {
/** The ids of the channels mentioned in this message. */
channels: Array<string>;
/** If @everyone/@here is mentioned in this message. */
everyone: boolean;
/** The members mentioned in this message. */
members: Array<Member>;
/** The ids of the roles mentioned in this message. */
roles: Array<string>;
/** The users mentioned in this message. */
users: Array<User>;
}
16 changes: 16 additions & 0 deletions lib/types/json.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,22 @@ export interface JSONMessage extends JSONBase {
users: Array<JSONUser>;
};
messageReference?: MessageReference;
messageSnapshots?: Array<{
message: {
attachments: Array<JSONAttachment>;
content: string;
editedTimestamp: number | null;
embeds: Array<Embed>;
flags: number;
mentions: {
channels: Array<string>;
roles: Array<string>;
users: Array<JSONUser>;
};
timestamp: number;
type: MessageTypes;
};
}>;
nonce?: number | string;
pinned: boolean;
position?: number;
Expand Down

0 comments on commit dc48df8

Please sign in to comment.