Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

231
Vistas
(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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda