ContextMenuCommandBuilder Permissions

I'm trying to add a User Command, and the permisisons through PermissionFlagsBits but I get an error. Here is the code snippet:
data: new ContextMenuCommandBuilder()
.setName("View User Avatar")
.setType(2)
.setDMPermission(true)
.setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers),
data: new ContextMenuCommandBuilder()
.setName("View User Avatar")
.setType(2)
.setDMPermission(true)
.setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers),
And the error output:
Failed to update commands: Cannot read properties of undefined (reading 'ModerateMembers')
Failed to update commands: Cannot read properties of undefined (reading 'ModerateMembers')
discord.js v14.15.3 and node v20.11.1
5 Replies
d.js toolkit
d.js toolkit6mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
dyLn
dyLnOP6mo ago
From builders:
const { ContextMenuCommandBuilder, PermissionFlagsBits } = require("@discordjs/builders");
const { ContextMenuCommandBuilder, PermissionFlagsBits } = require("@discordjs/builders");
Here is the entire code:
const { ContextMenuCommandBuilder, PermissionFlagsBits } = require("@discordjs/builders");
const { MediaEmbed, ErrorEmbed } = require("../utils/embeds");
const { Error, Debug } = require("../utils/logging");

module.exports = {
data: new ContextMenuCommandBuilder()
.setName("View User Avatar")
.setType(2)
.setDMPermission(true)
.setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers),
async execute(interaction) {
try {
const targetUser = await interaction.client.users.fetch(
interaction.targetId,
{ force: true }
);
const avatarURL = targetUser.avatarURL({ size: 4096 });

if (!avatarURL) {
const errorEmbed = ErrorEmbed(
"Error",
`Could not retrieve ${targetUser}'s avatar`
);

if (interaction.deferred || interaction.replied) {
return await interaction.editReply({
embeds: [errorEmbed],
ephemeral: true,
});
} else {
return await interaction.reply({
embeds: [errorEmbed],
ephemeral: true,
});
}
}

const mediaEmbed = MediaEmbed(avatarURL);
await interaction.reply({ embeds: [mediaEmbed] });
} catch (error) {
const errorEmbed = ErrorEmbed(
"Error executing View User Avatar",
error.message
);
Error(`Error executing View User Avatar: ${error.message}`);

if (interaction.deferred || interaction.replied) {
await interaction.editReply({
embeds: [errorEmbed],
ephemeral: true,
});
} else {
await interaction.reply({
embeds: [errorEmbed],
ephemeral: true,
});
}
}
},
};
const { ContextMenuCommandBuilder, PermissionFlagsBits } = require("@discordjs/builders");
const { MediaEmbed, ErrorEmbed } = require("../utils/embeds");
const { Error, Debug } = require("../utils/logging");

module.exports = {
data: new ContextMenuCommandBuilder()
.setName("View User Avatar")
.setType(2)
.setDMPermission(true)
.setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers),
async execute(interaction) {
try {
const targetUser = await interaction.client.users.fetch(
interaction.targetId,
{ force: true }
);
const avatarURL = targetUser.avatarURL({ size: 4096 });

if (!avatarURL) {
const errorEmbed = ErrorEmbed(
"Error",
`Could not retrieve ${targetUser}'s avatar`
);

if (interaction.deferred || interaction.replied) {
return await interaction.editReply({
embeds: [errorEmbed],
ephemeral: true,
});
} else {
return await interaction.reply({
embeds: [errorEmbed],
ephemeral: true,
});
}
}

const mediaEmbed = MediaEmbed(avatarURL);
await interaction.reply({ embeds: [mediaEmbed] });
} catch (error) {
const errorEmbed = ErrorEmbed(
"Error executing View User Avatar",
error.message
);
Error(`Error executing View User Avatar: ${error.message}`);

if (interaction.deferred || interaction.replied) {
await interaction.editReply({
embeds: [errorEmbed],
ephemeral: true,
});
} else {
await interaction.reply({
embeds: [errorEmbed],
ephemeral: true,
});
}
}
},
};
Harnes
Harnes6mo ago
You should import it from discord.js and all your builders too discord.js exports every package like builders in it
d.js docs
d.js docs6mo ago
discord.js includes multiple sub-packages, installing these separately can mess with internal code:
npm uninstall discord-api-types @discordjs/rest @discordjs/builders
yarn remove discord-api-types @discordjs/rest @discordjs/builders
pnpm remove discord-api-types @discordjs/rest @discordjs/builders
npm uninstall discord-api-types @discordjs/rest @discordjs/builders
yarn remove discord-api-types @discordjs/rest @discordjs/builders
pnpm remove discord-api-types @discordjs/rest @discordjs/builders
dyLn
dyLnOP6mo ago
that helped, thanks o7
Want results from more Discord servers?
Add your server