Last message will not get deleted

async function fetchAllMessages(chan) {
const channel = client.channels.cache.get(chan);
let messages = [];

// Create message pointer
let message = await channel.messages
.fetch({ limit: 1 })
.then(messagePage => (messagePage.size === 1 ? messagePage.at(0) : null));

while (message) {
await channel.messages
.fetch({ limit: 100, before: message.id })
.then(messagePage => {
messagePage.forEach(msg => {
messages.push(msg)
msg.delete()
});

// Update our message pointer to be last message in page of messages
message = 0 < messagePage.size ? messagePage.at(messagePage.size - 1) : null;
})
}
}
async function fetchAllMessages(chan) {
const channel = client.channels.cache.get(chan);
let messages = [];

// Create message pointer
let message = await channel.messages
.fetch({ limit: 1 })
.then(messagePage => (messagePage.size === 1 ? messagePage.at(0) : null));

while (message) {
await channel.messages
.fetch({ limit: 100, before: message.id })
.then(messagePage => {
messagePage.forEach(msg => {
messages.push(msg)
msg.delete()
});

// Update our message pointer to be last message in page of messages
message = 0 < messagePage.size ? messagePage.at(messagePage.size - 1) : null;
})
}
}
Hello! I use this function to clear a channel but he always not delete the last message that was posted in the channel why?
3 Replies
d.js docs
d.js docs3y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
monbrey
monbrey3y ago
Because you always fetch before that id?
Want results from more Discord servers?
Add your server