Tengo un comando serverinfo
y muestra cuántas prohibiciones hay en el servidor. Si el servidor tiene 1200 bloqueos, el bot mostrará que el servidor tiene 1000 bloqueos.
¿Es posible hacer que el bot muestre de alguna manera cuántas prohibiciones son realmente?
En la documentación se especifican los siguientes parámetros limit?| number | number of users to return (up to maximum 1000) | default 1000
¿Cómo cambiar el valor predeterminado de 1000 a, por ejemplo, 1500?
Estoy usando discord.js versión 13.8.0 y node.js versión 16.15.1.
Aunque el límite máximo es 1000, también puede usar el limit
y before
de las opciones para obtener las prohibiciones anteriores. before
es un copo de nieve, le dice a Discord que considere solo prohibiciones antes de esta identificación.
Por lo tanto, si tiene alrededor de 1200 bloqueos, puede obtener los primeros 1000, obtener la identificación del último elemento en la colección devuelta y usar su id
como la opción before
en su segunda recuperación:
let firstHalf = await guild.bans.fetch({ limit: 1000 }); let secondHalf = await guild.bans.fetch({ before: firstHalf.last().id, limit: 1000 });
También puede crear una nueva función que obtenga más de 1000 prohibiciones. Hay un ejemplo a continuación. Esta función devuelve una collection
de GuildBan
s , por lo que puede usar los métodos de recolección habituales , como 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; }
Ejemplo de uso:
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}`, ); });