Skip to content

fix: regression in allowedMentions when replying #10866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/discord.js/.lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://json.schemastore.org/lintstagedrc.schema.json",
"*": "prettier --ignore-unknown --write",
"{src/**,test/**,typings/**,scripts/**}.{mjs,js,ts}": "cross-env ESLINT_USE_FLAT_CONFIG=false eslint --ext mjs,js,ts --fix"
"{src/**,typings/**,scripts/**}.{mjs,js,ts}": "cross-env ESLINT_USE_FLAT_CONFIG=false eslint --ext mjs,js,ts --fix"
}
4 changes: 3 additions & 1 deletion packages/discord.js/src/structures/MessagePayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ class MessagePayload {
username,
avatar_url: avatarURL,
allowed_mentions:
this.isMessage && this.target.author.id !== this.target.client.user.id ? undefined : allowedMentions,
this.isMessage && message_reference === undefined && this.target.author.id !== this.target.client.user.id
? undefined
: allowedMentions,
flags,
message_reference,
attachments: this.options.attachments,
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/util/APITypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@

/**
* @external APIMediaGalleryItem
* @se {@link https://discord-api-types.dev/api/discord-api-types-v10/interface/APIMediaGalleryItem}
* @see {@link https://discord-api-types.dev/api/discord-api-types-v10/interface/APIMediaGalleryItem}
*/

/**
Expand Down
148 changes: 148 additions & 0 deletions packages/discord.js/test/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
'use strict';

const { readFile } = require('node:fs/promises');
const { createReadStream } = require('node:fs');
const path = require('node:path');
const { setTimeout: sleep } = require('node:timers/promises');
const util = require('node:util');
const { fetch } = require('undici');
const { Client, GatewayIntentBits, AttachmentBuilder, EmbedBuilder, MessageFlags, ComponentType } = require('../src');

const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
});

const buffer = l =>
fetch(l)
.then(res => res.arrayBuffer())
.then(Buffer.from);

const linkA = 'https://discord.js.org/static/logo.svg';
const fileA = path.join(__dirname, 'blobReach.png');

const embed = () => new EmbedBuilder();
const attach = (attachment, name) => new AttachmentBuilder(attachment, { name });

const tests = [
m => m.channel.send('x'),

m => m.channel.send({ content: 'x', embeds: [{ description: 'a' }] }),
m => m.channel.send({ embeds: [{ description: 'a' }] }),
m => m.channel.send({ files: [{ attachment: fileA }] }),
m =>
m.channel.send({
embeds: [{ description: 'a' }],
files: [{ attachment: fileA, name: 'xyz.png' }],
}),

m => m.channel.send({ content: 'x', embeds: [embed().setDescription('a')] }),
m => m.channel.send({ embeds: [embed().setDescription('a')] }),
m => m.channel.send({ embeds: [embed().setDescription('a'), embed().setDescription('b')] }),

m => m.channel.send({ content: 'x', files: [attach(fileA)] }),
m => m.channel.send({ files: [fileA] }),
m => m.channel.send({ files: [attach(fileA)] }),
async m => m.channel.send({ files: [await buffer(linkA)] }),
async m => m.channel.send({ files: [{ attachment: await buffer(linkA) }] }),
m => m.channel.send({ files: [attach(fileA), attach(fileA)] }),

m => m.channel.send({ embeds: [{ description: 'a' }] }).then(m2 => m2.edit('x')),
m => m.channel.send({ embeds: [embed().setDescription('a')] }).then(m2 => m2.edit('x')),

m => m.channel.send('x').then(m2 => m2.edit({ embeds: [{ description: 'a' }] })),
m => m.channel.send('x').then(m2 => m2.edit({ embeds: [embed().setDescription('a')] })),

m => m.channel.send({ embeds: [{ description: 'a' }] }).then(m2 => m2.edit({ content: 'x', embeds: [] })),
m => m.channel.send({ embeds: [embed().setDescription('a')] }).then(m2 => m2.edit({ content: 'x', embeds: [] })),

m => m.channel.send({ content: 'x', embeds: [embed().setDescription('a')], files: [attach(fileA)] }),
m => m.channel.send({ content: 'x', files: [attach(fileA), attach(fileA)] }),

m => m.channel.send({ embeds: [embed().setDescription('a')], files: [attach(fileA)] }),
m =>
m.channel.send({
embeds: [embed().setImage('attachment://two.png')],
files: [attach(fileA, 'two.png')],
}),
m => m.channel.send({ content: 'x', files: [attach(fileA)] }),
m => m.channel.send({ files: [fileA] }),
m => m.channel.send({ files: [attach(fileA)] }),
async m => m.channel.send({ files: [await readFile(fileA)] }),

m => m.channel.send({ content: 'x', files: [attach(createReadStream(fileA))] }),
m => m.channel.send({ files: [createReadStream(fileA)] }),
m => m.channel.send({ files: [{ attachment: createReadStream(fileA) }] }),

m => m.reply({ content: 'x', allowedMentions: { repliedUser: false } }),
m => m.reply({ content: 'x', allowedMentions: { repliedUser: true } }),
m => m.reply({ content: 'x' }),
m => m.reply({ content: `${m.author}`, allowedMentions: { repliedUser: false } }),
m => m.reply({ content: `${m.author}`, allowedMentions: { parse: ['users'], repliedUser: false } }),
m => m.reply({ content: `${m.author}`, allowedMentions: { parse: ['users'], repliedUser: true } }),
m => m.reply({ content: `${m.author}` }),

m => m.edit({ flags: MessageFlags.SuppressEmbeds }),
m => m.edit({ flags: MessageFlags.SuppressEmbeds, allowedMentions: { repliedUser: false } }),

m =>
m
.reply({ content: 'x', allowedMentions: { repliedUser: false } })
.then(msg => msg.edit({ content: 'a', allowedMentions: { repliedUser: true } })),

m =>
m.channel.send({
components: [{ type: ComponentType.TextDisplay, content: `${m.author}` }],
flags: MessageFlags.IsComponentsV2,
}),
m =>
m.channel.send({
components: [{ type: ComponentType.TextDisplay, content: `${m.author}` }],
flags: MessageFlags.IsComponentsV2,
allowedMentions: { parse: ['users'] },
}),
m =>
m.channel.send({
components: [{ type: ComponentType.TextDisplay, content: `${m.author}` }],
flags: MessageFlags.IsComponentsV2,
allowedMentions: { parse: [] },
}),
m =>
m.reply({
components: [{ type: ComponentType.TextDisplay, content: `${m.author}` }],
flags: MessageFlags.IsComponentsV2,
allowedMentions: { parse: [], repliedUser: true },
}),
m =>
m.reply({
components: [{ type: ComponentType.TextDisplay, content: `${m.author}` }],
flags: MessageFlags.IsComponentsV2,
allowedMentions: { parse: [], repliedUser: false },
}),

m => m.channel.send('Done!'),
];

client.on('messageCreate', async message => {
if (message.author.id !== process.env.OWNER) return;
const match = message.content.match(/^do (.+)$/);
if (match?.[1] === 'it') {
/* eslint-disable no-await-in-loop */
for (const [i, test] of tests.entries()) {
await message.channel.send(`**#${i}**\n\`\`\`js\n${test.toString()}\`\`\``);
await test(message).catch(e => message.channel.send(`Error!\n\`\`\`\n${e}\`\`\``));
await sleep(1_000);
}
/* eslint-enable no-await-in-loop */
} else if (match) {
const n = parseInt(match[1]) || 0;
const test = tests.slice(n)[0];
const i = tests.indexOf(test);
await message.channel.send(`**#${i}**\n\`\`\`js\n${test.toString()}\`\`\``);
await test(message).catch(e => message.channel.send(`Error!\n\`\`\`\n${e}\`\`\``));
}
});

client.login();

// eslint-disable-next-line no-console
process.on('unhandledRejection', console.error);
2 changes: 1 addition & 1 deletion packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3219,7 +3219,7 @@ export type SelectMenuType = APISelectMenuComponent['type'];

export interface SeparatorComponentData extends BaseComponentData {
spacing?: SeparatorSpacingSize;
dividier?: boolean;
divider?: boolean;
}
export class SeparatorComponent extends Component<APISeparatorComponent> {
private constructor(data: APISeparatorComponent);
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ client.on('messageCreate', async message => {
const rawSeparator: SeparatorComponentData = {
type: ComponentType.Separator,
spacing: 1,
dividier: false,
divider: false,
};

const rawFile: FileComponentData = {
Expand Down
2 changes: 1 addition & 1 deletion packages/ws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"typescript": "~5.5.4",
"undici": "6.21.1",
"vitest": "^2.0.5",
"zlib-sync": "^0.1.9"
"zlib-sync": "^0.1.10"
},
"engines": {
"node": ">=20"
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.