因此,我在其下方嵌入了 1 个按钮,如果您单击该按钮,您应该会获得该角色,但在 Discord 中却显示“此交互失败”。我只想让具有管理 Webhooks 的成员执行命令,让机器人发送嵌入,并且每个人都可以使用该按钮。我在 Visual Studio Code 中没有收到错误。
const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits, ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } = require('discord.js');
module.exports = {
category: "command",
data: new SlashCommandBuilder()
.setName('nda')
.setDescription('Make the NDA embed + button for the role!')
.setDefaultMemberPermissions(PermissionFlagsBits.ManageWebhooks),
async execute(client, interaction) {
const ndaRole = interaction.guild.roles.cache.get('1099050320660799609');
const ndaChannel = client.channels.cache.get('1145302828408111125');
var ndaEmbed = new EmbedBuilder()
.setTitle('Instant Editing Staff NDA')
.setColor('#e00c45')
.addFields(
{ name: '**1. __ Disclaimers__**', value: '...' },
{ name: '...', value: '...' },
...;
const row = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId("nda")
.setLabel("Confirm That You Have Read The NDA")
.setEmoji("1099073509692543068")
.setStyle(ButtonStyle.Success)
);
const collector = interaction.channel.createMessageComponentCollector({
componentType: ComponentType.Button
});
interaction.reply({ content: "I have posted the NDA embed!", ephemeral: true });
await ndaChannel.send({ embeds: [ndaEmbed], components: [row] });
collector.on("collect", async (interactionButton) => {
let id = interactionButton.customId;
let userID = interactionButton.user.id;
let userData = interactionButton.guild.members.cache.get(userID);
var message = "";
switch (id) {
case "nda":
userData.roles.cache.has(ndaRole.id)
? message = `You already have the ${ndaRole} role!`
: await userData.roles.add(ndaRole).then(() => {
message = `You now have the ${ndaRole} role!`;
})
return interactionButton.reply({ content: `${message}`, ephemeral: true });
}
});
},
};
我不知道该改变什么。
代码会在显示您已设置角色的消息之前尝试回复。这意味着它正在尝试发送一条没有任何内容的消息,discord.js 不接受并取消回复。
将
interactionButton.reply
放入 .then
方法中传递的函数中会有所帮助。解决此问题的更好方法是等待 userData.roles.add()