Home > Software engineering >  How to delete a single message using messages.fetch?
How to delete a single message using messages.fetch?

Time:11-23

I'm trying to delete a single message from a channel (specifically the last message sent). Here is the code I've been trying to use:

try {
    await interaction.channel.messages
    .fetch({
        limit: 1,
    })
    .then((message) => message.delete());
} catch (error) {
    console.error(error);
}

I'm getting no errors, but it doesn't seem to be doing anything. Any help would be appreciated.

CodePudding user response:

There are 2 problems here. The first is that you are mixing await and .then. This is bad practice and should be avoided. The second is that TextChannel.messages.fetch() will give a Collection of Messages. Use .first() to get the wanted message

interaction.channel.messages
.fetch({
    limit: 1,
})
.then((message) => message.first().delete()).catch(console.error)
  • Related