Discord.js: channel.bulkDelete returns incorrect information

Created on 21 Jul 2020  ·  5Comments  ·  Source: discordjs/discord.js

Please describe the problem you are having in as much detail as possible:
I use a loop to clear more than 100 messages inside a single command call. if I specify a number greater than 100, such as 150, discord.js will return me the information that 100 messages were cleared and then 50 messages were cleared, but in fact the remaining 50 messages will not be cleared.

oaoaommm)20-07-21-15-03-57

oaoaommm)20-07-21-15-07-02

oaoaommm)20-07-21-15-10-26

Include a reproducible code sample here, if possible:

// the code looks a little different, I simplified it so that it can be executed outside of my project
async function clear(msg, requestedToClear) {
  if (!msg) return null
  if (isNaN(requestedToClear) || requestedToClear <= 0) return msg.reply('do not joke with me')

  let toClearCycleNumber = Math.ceil(requestedToClear / 100),
    deletedMessagesCount = 0

  if (!msg.channel.permissionsFor(msg.guild.me).has('MANAGE_MESSAGES'))
    return msg.reply('give me permissions :c')

  for (let i = 0; i < toClearCycleNumber; i++) {
    let messagesToFetchNumber = requestedToClear - deletedMessagesCount
    if (messagesToFetchNumber > 100) messagesToFetchNumber = 100

    console.log('requested: ', messagesToFetchNumber)
    let messages = await msg.channel.messages.fetch({ before: msg.id, limit: messagesToFetchNumber })
    console.log('fetched: ', messages.size)

    await msg.channel.bulkDelete(messages, true)
      .then(deletedMessages => {
        console.log('cleared: ', deletedMessages.size)
        deletedMessagesCount += deletedMessages.size
        if(deletedMessages.size < 100) console.log('total cleared: ', deletedMessagesCount)
      })
      .catch(e => {
        i = toClearCycleNumber
        console.error(e)
      })
  }
}

Further details:

  • discord.js version: 12.2.0
  • Node.js version: 12.6.3
  • Operating system: Windows 10 2004 & Ubuntu server 18.04 LTS
  • Priority this issue should have – please be realistic and elaborate if possible: medium

Relevant client options:

  • partials: message, reaction
  • gateway intents: none
  • other: none
  • [ ] I have also tested the issue on latest master, commit hash:
unverified bug

Most helpful comment

кто согласен что смоки ебанутый - ставим класс

All 5 comments

Нихуя не понял, но очень интересно

Нихуя не понял, но очень интересно

wut?

Ban SmokyPlay pls

кто согласен что смоки ебанутый - ставим класс

okay, I found the problem. MessageManager.fetch() returns a collection of messages, but some of them are returned again after deletion, as if they were not deleted. I tried not to add them to the cache, but it didn't help. I decided that I would use the ID of the message that was the last in the collection of deleted messages as the starting point, and it worked. since the problem was not on discord.js side, I'm closing this issue.

Was this page helpful?
5 / 5 - 1 ratings

Related issues

iCrawl picture iCrawl  ·  3Comments

ghost picture ghost  ·  3Comments

Blumlaut picture Blumlaut  ·  3Comments

Alipoodle picture Alipoodle  ·  3Comments

BrandonCookeDev picture BrandonCookeDev  ·  3Comments