const row = new Discord.MessageActionRow()
.addComponents(
new Discord.MessageButton()
.setCustomId(`deletable`)
.setLabel('❌')
.setStyle(4)
);
user.send({content: 'hi', components: [row]});
When the button is clicked:
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();
});
}
});
How can I delete the message that the button was clicked on? (DM)
I can't find the channel of messages sent from dm.
Log:
TypeError: Cannot read properties of undefined (reading 'fetch')
According to the docs, there is a interaction.channel property available for you to use:
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(); // :)
}
Over-complicated version: Get the message from interaction and use the delete() method to delete it
Simple/spoon-feeding version: interaction.message.delete();