Add message forward (#3764)

This commit is contained in:
Fleny
2024-07-21 18:49:07 +02:00
committed by GitHub
parent b61cabed05
commit 402f1b3049
5 changed files with 86 additions and 3 deletions

View File

@@ -36,6 +36,7 @@ import type {
DiscordMessage, DiscordMessage,
DiscordMessageCall, DiscordMessageCall,
DiscordMessageInteractionMetadata, DiscordMessageInteractionMetadata,
DiscordMessageSnapshot,
DiscordPoll, DiscordPoll,
DiscordPollMedia, DiscordPollMedia,
DiscordPresenceUpdate, DiscordPresenceUpdate,
@@ -90,6 +91,7 @@ import {
type Message, type Message,
type MessageCall, type MessageCall,
type MessageInteractionMetadata, type MessageInteractionMetadata,
type MessageSnapshot,
type Poll, type Poll,
type PollMedia, type PollMedia,
type PresenceUpdate, type PresenceUpdate,
@@ -147,6 +149,7 @@ import {
transformMessage, transformMessage,
transformMessageCall, transformMessageCall,
transformMessageInteractionMetadata, transformMessageInteractionMetadata,
transformMessageSnapshot,
transformPoll, transformPoll,
transformPollMedia, transformPollMedia,
transformPresence, transformPresence,
@@ -184,6 +187,7 @@ export interface Transformers {
forumTag: (bot: Bot, payload: DiscordForumTag, forumTag: ForumTag) => any forumTag: (bot: Bot, payload: DiscordForumTag, forumTag: ForumTag) => any
interaction: (bot: Bot, payload: { interaction: DiscordInteraction; shardId: number }, interaction: Interaction) => any interaction: (bot: Bot, payload: { interaction: DiscordInteraction; shardId: number }, interaction: Interaction) => any
message: (bot: Bot, payload: DiscordMessage, message: Message) => any message: (bot: Bot, payload: DiscordMessage, message: Message) => any
messageSnapshot: (bot: Bot, payload: DiscordMessageSnapshot, messageSnapshot: MessageSnapshot) => any
messageInteractionMetadata: (bot: Bot, payload: DiscordMessageInteractionMetadata, metadata: MessageInteractionMetadata) => any messageInteractionMetadata: (bot: Bot, payload: DiscordMessageInteractionMetadata, metadata: MessageInteractionMetadata) => any
messageCall: (bot: Bot, payload: DiscordMessageCall, call: MessageCall) => any messageCall: (bot: Bot, payload: DiscordMessageCall, call: MessageCall) => any
user: (bot: Bot, payload: DiscordUser, user: User) => any user: (bot: Bot, payload: DiscordUser, user: User) => any
@@ -267,6 +271,7 @@ export interface Transformers {
user: (bot: Bot, payload: DiscordUser) => User user: (bot: Bot, payload: DiscordUser) => User
member: (bot: Bot, payload: DiscordMember, guildId: BigString, userId: BigString) => Member member: (bot: Bot, payload: DiscordMember, guildId: BigString, userId: BigString) => Member
message: (bot: Bot, payload: DiscordMessage) => Message message: (bot: Bot, payload: DiscordMessage) => Message
messageSnapshot: (bot: Bot, payload: DiscordMessageSnapshot) => MessageSnapshot
messageInteractionMetadata: (bot: Bot, payload: DiscordMessageInteractionMetadata) => MessageInteractionMetadata messageInteractionMetadata: (bot: Bot, payload: DiscordMessageInteractionMetadata) => MessageInteractionMetadata
messageCall: (bot: Bot, payload: DiscordMessageCall) => MessageCall messageCall: (bot: Bot, payload: DiscordMessageCall) => MessageCall
role: (bot: Bot, payload: { role: DiscordRole } & { guildId: BigString }) => Role role: (bot: Bot, payload: { role: DiscordRole } & { guildId: BigString }) => Role
@@ -518,6 +523,8 @@ export interface TransformersDesiredProprieties {
mentionedRoleIds: boolean mentionedRoleIds: boolean
mentions: boolean mentions: boolean
messageReference: boolean messageReference: boolean
referencedMessage: boolean
messageSnapshots: boolean
nonce: boolean nonce: boolean
reactions: boolean reactions: boolean
stickerItems: boolean stickerItems: boolean
@@ -527,6 +534,9 @@ export interface TransformersDesiredProprieties {
poll: boolean poll: boolean
call: boolean call: boolean
} }
messageSnapshot: {
message: boolean
}
messageInteractionMetadata: { messageInteractionMetadata: {
id: boolean id: boolean
type: boolean type: boolean
@@ -756,6 +766,9 @@ export function createTransformers(options: Partial<Transformers>, opts?: Create
message(_bot, _payload, message) { message(_bot, _payload, message) {
return message return message
}, },
messageSnapshot(_bot, _payload, messageSnapshot) {
return messageSnapshot
},
messageInteractionMetadata(_bot, _payload, metadata) { messageInteractionMetadata(_bot, _payload, metadata) {
return metadata return metadata
}, },
@@ -929,6 +942,7 @@ export function createTransformers(options: Partial<Transformers>, opts?: Create
invite: options.invite ?? transformInvite, invite: options.invite ?? transformInvite,
member: options.member ?? transformMember, member: options.member ?? transformMember,
message: options.message ?? transformMessage, message: options.message ?? transformMessage,
messageSnapshot: options.messageSnapshot ?? transformMessageSnapshot,
messageInteractionMetadata: options.messageInteractionMetadata ?? transformMessageInteractionMetadata, messageInteractionMetadata: options.messageInteractionMetadata ?? transformMessageInteractionMetadata,
messageCall: options.messageCall ?? transformMessageCall, messageCall: options.messageCall ?? transformMessageCall,
presence: options.presence ?? transformPresence, presence: options.presence ?? transformPresence,
@@ -1185,6 +1199,8 @@ export function createDesiredProprietiesObject(
mentionedRoleIds: defaultValue, mentionedRoleIds: defaultValue,
mentions: defaultValue, mentions: defaultValue,
messageReference: defaultValue, messageReference: defaultValue,
messageSnapshots: defaultValue,
referencedMessage: defaultValue,
nonce: defaultValue, nonce: defaultValue,
reactions: defaultValue, reactions: defaultValue,
stickerItems: defaultValue, stickerItems: defaultValue,
@@ -1195,6 +1211,10 @@ export function createDesiredProprietiesObject(
call: defaultValue, call: defaultValue,
...desiredProperties.message, ...desiredProperties.message,
}, },
messageSnapshot: {
message: defaultValue,
...desiredProperties.messageSnapshot,
},
messageInteractionMetadata: { messageInteractionMetadata: {
id: defaultValue, id: defaultValue,
type: defaultValue, type: defaultValue,

View File

@@ -3,10 +3,11 @@ import {
type DiscordMessage, type DiscordMessage,
type DiscordMessageCall, type DiscordMessageCall,
type DiscordMessageInteractionMetadata, type DiscordMessageInteractionMetadata,
type DiscordMessageSnapshot,
MessageFlags, MessageFlags,
} from '@discordeno/types' } from '@discordeno/types'
import { CHANNEL_MENTION_REGEX } from '../constants.js' import { CHANNEL_MENTION_REGEX } from '../constants.js'
import { type Bot, type Message, type MessageCall, type MessageInteractionMetadata, snowflakeToTimestamp } from '../index.js' import { type Bot, type Message, type MessageCall, type MessageInteractionMetadata, type MessageSnapshot, snowflakeToTimestamp } from '../index.js'
import { ToggleBitfield } from './toggles/ToggleBitfield.js' import { ToggleBitfield } from './toggles/ToggleBitfield.js'
const EMPTY_STRING = '' const EMPTY_STRING = ''
@@ -202,6 +203,9 @@ export function transformMessage(bot: Bot, payload: DiscordMessage): Message {
message.messageReference = reference message.messageReference = reference
} }
if (props.referencedMessage && payload.referenced_message) message.referencedMessage = bot.transformers.message(bot, payload.referenced_message)
if (props.messageSnapshots && payload.message_snapshots)
message.messageSnapshots = payload.message_snapshots.map((snap) => bot.transformers.messageSnapshot(bot, snap))
if (props.nonce && payload.nonce) message.nonce = payload.nonce if (props.nonce && payload.nonce) message.nonce = payload.nonce
if (payload.pinned) message.pinned = true if (payload.pinned) message.pinned = true
if (props.reactions && payload.reactions?.length) { if (props.reactions && payload.reactions?.length) {
@@ -233,6 +237,15 @@ export function transformMessage(bot: Bot, payload: DiscordMessage): Message {
return bot.transformers.customizers.message(bot, payload, message) return bot.transformers.customizers.message(bot, payload, message)
} }
export function transformMessageSnapshot(bot: Bot, payload: DiscordMessageSnapshot): MessageSnapshot {
const props = bot.transformers.desiredProperties.messageSnapshot
const messageSnapshot = {} as MessageSnapshot
if (props.message && payload.message) messageSnapshot.message = bot.transformers.message(bot, payload.message as DiscordMessage)
return bot.transformers.customizers.messageSnapshot(bot, payload, messageSnapshot)
}
export function transformMessageInteractionMetadata(bot: Bot, payload: DiscordMessageInteractionMetadata): MessageInteractionMetadata { export function transformMessageInteractionMetadata(bot: Bot, payload: DiscordMessageInteractionMetadata): MessageInteractionMetadata {
const props = bot.transformers.desiredProperties.messageInteractionMetadata const props = bot.transformers.desiredProperties.messageInteractionMetadata
const metadata = {} as MessageInteractionMetadata const metadata = {} as MessageInteractionMetadata

View File

@@ -1068,6 +1068,13 @@ export interface Message {
mentionedRoleIds?: bigint[] mentionedRoleIds?: bigint[]
/** Data showing the source of a crossposted channel follow add, pin or reply message */ /** Data showing the source of a crossposted channel follow add, pin or reply message */
messageReference?: MessageReference messageReference?: MessageReference
/**
* The message associated with the `message_reference`
* Note: This field is only returned for messages with a `type` of `19` (REPLY). If the message is a reply but the `referenced_message` field is not present, the backend did not attempt to fetch the message that was being replied to, so its state is unknown. If the field exists but is null, the referenced message was deleted.
*/
referencedMessage?: Message
/** The message associated with the `message_reference`. This is a minimal subset of fields in a message (e.g. `author` is excluded.) */
messageSnapshots?: MessageSnapshot[]
nonce?: string | number nonce?: string | number
/** Reactions on this message. */ /** Reactions on this message. */
reactions?: Reaction[] reactions?: Reaction[]
@@ -1148,6 +1155,11 @@ export interface MessageReference {
messageId?: bigint messageId?: bigint
} }
export interface MessageSnapshot {
/** Minimal subset of fields in the forwarded message */
message: Pick<Message, 'type' | 'content' | 'embeds' | 'attachments' | 'timestamp' | 'editedTimestamp' | 'flags' | 'mentions' | 'mentionedRoleIds'>
}
export interface MessageInteractionMetadata { export interface MessageInteractionMetadata {
/** Id of the interaction */ /** Id of the interaction */
id: bigint id: bigint

View File

@@ -1330,7 +1330,7 @@ export interface DiscordMessage {
application?: Partial<DiscordApplication> application?: Partial<DiscordApplication>
/** if the message is an Interaction or application-owned webhook, this is the id of the application */ /** if the message is an Interaction or application-owned webhook, this is the id of the application */
application_id?: string application_id?: string
/** Data showing the source of a crossposted channel follow add, pin or reply message */ /** Data showing the source of a crosspost, channel follow add, pin, or reply message */
message_reference?: Omit<DiscordMessageReference, 'failIfNotExists'> message_reference?: Omit<DiscordMessageReference, 'failIfNotExists'>
/** Message flags combined as a bitfield */ /** Message flags combined as a bitfield */
flags?: MessageFlags flags?: MessageFlags
@@ -1344,6 +1344,8 @@ export interface DiscordMessage {
* Note: This field is only returned for messages with a `type` of `19` (REPLY). If the message is a reply but the `referenced_message` field is not present, the backend did not attempt to fetch the message that was being replied to, so its state is unknown. If the field exists but is null, the referenced message was deleted. * Note: This field is only returned for messages with a `type` of `19` (REPLY). If the message is a reply but the `referenced_message` field is not present, the backend did not attempt to fetch the message that was being replied to, so its state is unknown. If the field exists but is null, the referenced message was deleted.
*/ */
referenced_message?: DiscordMessage referenced_message?: DiscordMessage
/** The message associated with the `message_reference`. This is a minimal subset of fields in a message (e.g. `author` is excluded.) */
message_snapshots?: DiscordMessageSnapshot[]
/** sent if the message is sent as a result of an interaction */ /** sent if the message is sent as a result of an interaction */
interaction_metadata?: DiscordMessageInteractionMetadata interaction_metadata?: DiscordMessageInteractionMetadata
/** /**
@@ -1426,6 +1428,8 @@ export interface DiscordMessageActivity {
/** https://discord.com/developers/docs/resources/channel#message-object-message-reference-structure */ /** https://discord.com/developers/docs/resources/channel#message-object-message-reference-structure */
export interface DiscordMessageReference { export interface DiscordMessageReference {
/** Type of reference */
type?: DiscordMessageReferenceType
/** id of the originating message */ /** id of the originating message */
message_id?: string message_id?: string
/** /**
@@ -1439,6 +1443,37 @@ export interface DiscordMessageReference {
fail_if_not_exists: boolean fail_if_not_exists: boolean
} }
/** https://discord.com/developers/docs/resources/channel#message-reference-object-message-reference-types */
export enum DiscordMessageReferenceType {
/**
* A standard reference used by replies.
*
* @remarks
* When the type is set to this value, the field {@link DiscordMessage.referenced_message} will be present
*/
Default,
/**
* Reference used to point to a message at a point in time.
*
* @remarks
* When the type is set to this value, the field {@link DiscordMessage.message_snapshot} will be present in the
*
* This value can only be used for basic messages;
* i.e. messages which do not have strong bindings to a non global entity.
* Thus we support only messages with `DEFAULT` or `REPLY` types, but disallowed if there are any polls, calls, or components.
*/
Forward,
}
/** https://discord.com/developers/docs/resources/channel#message-snapshot-object-message-snapshot-structure */
export interface DiscordMessageSnapshot {
/** Minimal subset of fields in the forwarded message */
message: Pick<
DiscordMessage,
'type' | 'content' | 'embeds' | 'attachments' | 'timestamp' | 'edited_timestamp' | 'flags' | 'mentions' | 'mention_roles'
>
}
/** https://discord.com/developers/docs/resources/poll#poll-object */ /** https://discord.com/developers/docs/resources/poll#poll-object */
export interface DiscordPoll { export interface DiscordPoll {
/** The question of the poll. Only `text` is supported. */ /** The question of the poll. Only `text` is supported. */

View File

@@ -15,6 +15,7 @@ import type {
DiscordGuildOnboardingPrompt, DiscordGuildOnboardingPrompt,
DiscordInstallParams, DiscordInstallParams,
DiscordInteractionContextType, DiscordInteractionContextType,
DiscordMessageReferenceType,
DiscordPollAnswer, DiscordPollAnswer,
DiscordPollLayoutType, DiscordPollLayoutType,
DiscordPollMedia, DiscordPollMedia,
@@ -63,8 +64,10 @@ export interface CreateMessageOptions {
embeds?: Camelize<DiscordEmbed>[] embeds?: Camelize<DiscordEmbed>[]
/** Allowed mentions for the message */ /** Allowed mentions for the message */
allowedMentions?: AllowedMentions allowedMentions?: AllowedMentions
/** Include to make your message a reply */ /** Include to make your message a reply or a forward */
messageReference?: { messageReference?: {
/** Type of reference */
type?: DiscordMessageReferenceType
/** id of the originating message */ /** id of the originating message */
messageId?: BigString messageId?: BigString
/** /**