Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

230
Visualizações
(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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda