Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
(DiscordJS) Trying to fetch messages before message ID but keep getting the most recent one

I have a range of messages I want to fetch to be deleted. If I pass 3-5 for example and I have 5 messages * a b c d e*

I expect my command ^delmsg 3-5 to delete d and c.

I managed to get the ID (a snowflake) of d and tried to pass it to before, but it keeps fetching my command and e instead of d and c. I don't know what else I can do with this. Maybe I'm doing something wrong with promises that I'm not aware of.

Code:

const deleter = (message, range) => {
    const range1 = range[0]; //indexes are already an array so we get the first index 
    const amnt = parseInt(range[1]) - parseInt(range[0]); //makes the str index ints
    var msgs, nID;
    
    message.channel.messages.fetch({
        limit: range1,
    }).then(a => {
        msgs = Array.from(a);
        nID = msgs[range1-1][0];
    }).then(message.channel.messages.fetch({
        limit: amnt,
        before: nID,
    }).then(b => {
        //message.channel.bulkDelete(b);
    }));

}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

.then expects an uncalled function. You should use an arrow function there too, and it should work as intended

 message.channel.messages.fetch({
        limit: range1,
    }).then(a => {
        msgs = Array.from(a);
        nID = msgs[range1-1][0];
    }).then(() => message.channel.messages.fetch({ //notice it's an arrow function
        limit: amnt,
        before: nID,
    })).then(b => {
        message.channel.bulkDelete(b);
    })

Also you should note the extra bracket at the end should go after the .then where you fetch the messages.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!