按钮处理程序和awaitmessage组件

问题描述 投票:0回答:1

我正在尝试制作一个按钮来确认命令中的操作,但我不能,因为我的按钮处理程序阻止了它。它知道没有为该按钮编程的功能,并拒绝在那里进行的任何操作。有办法解决吗

命令

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);
    }
  }
javascript node.js button discord discord.js
1个回答
0
投票

我认为您可以通过一种方法解决该问题。您可以在按钮的

customId
中添加前缀,以区分全局按钮(由按钮处理程序处理)和本地按钮(特定于某些命令),在按钮处理程序中,您可以检查处理按钮交互之前的前缀。

  1. 更新您的命令以包含按钮
    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);
    
  2. 更新按钮处理程序以检查 customId 是否以本地前缀开头。如果是这样,请忽略交互。
    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);
     }
    }
    
© www.soinside.com 2019 - 2024. All rights reserved.