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

242
Visualizações
How to fetch more than 1000 bans of a discord server using discord.js?

I have a command serverinfo and it shows how many bans there are on the server. If the server has 1200 bans, the bot will show that the server has 1000 bans.

Is it possible to somehow make the bot shows how many bans really are?

In the documentation the following parameters are specified limit?| number | number of users to return (up to maximum 1000) | default 1000

How to change default 1000 to e.g. 1500?

I am using discord.js vers 13.8.0 and node.js version 16.15.1.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Although the max limit is 1000, you can use the limit and before options to fetch the previous bans, too. before is a snowflake, it tells Discord to consider only bans before this id.

So, if you have around 1200 bans, you can fetch the first 1000, grab the id of the last item in the returned collection and use its id as the before option in your second fetch:

let firstHalf = await guild.bans.fetch({ limit: 1000 });
let secondHalf = await guild.bans.fetch({ before: firstHalf.last().id, limit: 1000 });

You can also create a new function that fetches more than 1000 bans. There is an example below. This function returns a collection of GuildBans, so you can use the usual collection methods, like filter, each, etc.


async function fetchMoreBans(guild, limit = 2000) {
  if (!guild || typeof guild !== 'object')
    throw new Error(`Expected a guild, got "${typeof guild}"`);
  if (!Number.isInteger(limit))
    throw new Error(`Expected an integer, got ${typeof limit}.`);
  if (limit <= 1000) return guild.bans.fetch({ limit });

  let collection = new Collection();
  let lastId = null;
  let options = {};
  let remaining = limit;

  while (remaining > 0) {
    options.limit = remaining > 1000 ? 1000 : remaining;
    remaining = remaining > 1000 ? remaining - 1000 : 0;

    if (lastId) options.before = lastId;

    let bans = await guild.bans.fetch(options);

    if (!bans.last()) break;

    collection = collection.concat(bans);
    lastId = bans.last().id;
  }

  return collection;
}

Example usage:

client.on('messageCreate', async (message) => {
  if (message.author.bot) return;

  let banList = await fetchMore(message.guild, 1500);
  let filteredBanList = banList.filter(({ user }) =>
    user.username.toLowerCase().startsWith('e'),
  );

  console.log(`There are ${banList.size} bans fetched`);
  console.log(
    `Number of bans where the user's username starts with the letter "e": ${filteredBanList.size}`,
  );
});
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