首页 > 解决方案 > discord.js v13 交互按钮删除原始消息

问题描述

const row = new Discord.MessageActionRow()
  .addComponents(
    new Discord.MessageButton()
      .setCustomId(`deletable`)
      .setLabel('❌')
      .setStyle(4)
  );

user.send({content: 'hi', components: [row]});

单击按钮时:

client.ws.on('INTERACTION_CREATE', async (interaction) => {
  const {
    data: {
      custom_id
    }
  } = interation;

  if (custom_id && custom_id === "deletable") {
    let channel = await client.messages.fetch({
      around: interaction.message.id,
      limit: 1
    }).then((msg) => {
      const fetchedMsg = msg.first();
      console.log(msg);
      fetchedMsg.delete();
    });
  }
});

如何删除单击按钮的消息?(DM)

我找不到从 dm 发送的消息的频道。

日志:

TypeError: Cannot read properties of undefined (reading 'fetch')

标签: javascriptdiscorddiscord.js

解决方案


根据文档,有一个interaction.channel属性可供您使用:

if (custom_id && custom_id === "deletable") {
    const channel = interaction.channel;
    const fetchedMsg = await channel.messages.fetch({ around: interaction.message.id, limit: 1 });
    await fetchedMsg.delete();

    // Alternatively, you could also use this:
    await interaction.message.delete();  // :)
}

推荐阅读