繁体   English   中英

discord.js v13 交互按钮删除原始消息

[英]discord.js v13 Interaction button delete orginal message

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')

根据文档,有一个可供您使用的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();  // :)
}

过于复杂的版本:从interaction中获取消息并使用delete()方法将其删除

简单/勺子喂养版本: interaction.message.delete();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM