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();