我正在尝试制作一个按钮来确认命令中的操作,但我不能,因为我的按钮处理程序阻止了它。它知道没有为该按钮编程的功能,并拒绝在那里进行的任何操作。有办法解决吗
命令
const response = await interaction.reply({
embeds: [embed],
components: [actionRow],
ephemeral: true,
});
const collector = (i) => i.user.id === interaction.user.id;
try {
const confirmation = await response.awaitMessageComponent({
filter: collector,
time: 60000,
});
if (confirmation.customId === "channel_confirm") {
await confirmation.update({
content: `Mensagem enviada!`,
components: [],
embeds: [],
});
}
if (confirmation.customId === "channel_cancelar") {
await confirmation.update({
content: `Ação cancelada.`,
components: [],
embeds: [],
});
}
} catch (err) {
await interaction.editReply({
content: `Não recebi nenhuma confirmação em 1 minutos, acho que vou cancelar...`,
components: [],
embeds: [],
});
}
按钮处理程序
if (interaction.isButton()) {
const { buttons } = client;
const { customId } = interaction;
const button = buttons.get(customId);
if (!button)
return interaction.reply({
content: `Esse botão ainda não possui nenhuma função.`,
ephemeral: true,
});
try {
await button.execute(client, interaction);
} catch (err) {
console.error(err);
}
}
customId
中添加前缀,以区分全局按钮(由按钮处理程序处理)和本地按钮(特定于某些命令),在按钮处理程序中,您可以检查处理按钮交互之前的前缀。
customId
的前缀。
// In your command, when creating the buttons
const confirmButton = new MessageButton()
.setCustomId("local_channel_confirm")
.setLabel("Confirm")
.setStyle("SUCCESS");
const cancelButton = new MessageButton()
.setCustomId("local_channel_cancelar")
.setLabel("Cancelar")
.setStyle("DANGER");
const actionRow = new MessageActionRow().addComponents(confirmButton, cancelButton);
if (interaction.isButton()) {
const { buttons } = client;
const { customId } = interaction;
// Ignore local buttons
if (customId.startsWith("local_")) {
return;
}
const button = buttons.get(customId);
if (!button)
return interaction.reply({
content: `Esse botão ainda não possui nenhuma função.`,
ephemeral: true,
});
try {
await button.execute(client, interaction);
} catch (err) {
console.error(err);
}
}