#how can i get servers member count ??
1 messages · Page 1 of 1 (latest)
like
${gGuilds.map(g => `${g.name} ${g.memberCount}`).join('\n')}
``` ??
Looks good to me. Try it
fetch info about guild and add resolve
How ?
(await g.fetch()).memberCount
^^
${gGuilds.map(g => `${g.name} (${(await g.fetch()).memberCount} Members)`).join('\n')}
Tell us the error, not just a piece of code
.setDescription(`>>> ${gGuilds.map(g => `${g.name} (${(await g.fetch()).memberCount} Members)`).join('\n')}`)
Probably it complains about await keyword
Make the function async or resolve the promise other way
how can i make that function async ?
That's pretty hard to explain. It is JS knowledge. First learn what async functions are
i know what is async function
You should make the function that contains ${} async
like : (async (g) => ...
.setDescription(`>>> ${gGuilds.map(async (g) => `${g.name} (${(await g.fetch()).memberCount} Members)`).join('\n')}`)
Not within the line itself
So you call this .setDescription in a function. Make that function async
Not what's inside .setDescription
${gGuilds.map(g => `${g.name} (${(async (gu) => await gu.fetch()).memberCount} Members)`).join('\n')}
bro im so confused -.-
Bruh, show the whole function
thats an embed
Doesn't matter. Show the function that creates the embed
async execute(client, message) {
const gGuilds = await client.guilds.fetch()
const serverEmbed = new Discord.MessageEmbed()
.setColor("RANDOM")
.setDescription(`>>> ${gGuilds.map(g => `${g.name}`).join('\n')}`)
await message.channel.send({ content: `||${message.member}||`, embeds: [serverEmbed] }).then(async () => { try { await message.delete() } catch { } })
},
Hmm, the function is fine, can you show the code you used when you got that first error?
This one
async execute(client, message) {
const gGuilds = await client.guilds.fetch()
const serverEmbed = new Discord.MessageEmbed()
.setColor("RANDOM")
.setDescription(`>>> ${gGuilds.map(g => `${g.name} (${(await g.fetch()).memberCount} Members)`).join('\n')}`)
await message.channel.send({ content: `||${message.member}||`, embeds: [serverEmbed] }).then(async () => { try { await message.delete() } catch { } })
},
the problem is from await !
i should use async for (await g.fetch()).memberCount
but i dono how
Ok so, client.guilds.fetch() likely returns a collection
You need to use .values() after awaiting it
const gGuilds = await client.guilds.fetch().values()
or
const gGuilds = await client.guilds.values()
oh
result of console.log const gGuilds = (await client.guilds.fetch()).values()
same as await client.guilds.fetch()
It is clearly not the same
wait
Now you get what you need. The next step is to covert this guild partial into an actual guild
You can do that by using await guild.fetch()
+1
like
const Guilds = (await client.guilds.fetch()).values()
const gGuilds = await Guilds.fetch()
No
Let the code how it is, but change just:
gGuilds.map(g => await g.fetch()).map(g => `${g.name} (${(await g.fetch()).memberCount} Members)`).join('\n')
oh
Oh, wait
Typo
gGuilds.map(g => await g.fetch()).map(g => `${g.name} (${g.memberCount} Members)`).join('\n')
You don't need that await anymore, cause you now fetched it already beforehand
maybe js gGuilds.map(async (g) => await g.fetch()).map(g => `${g.name} (${g.memberCount} Members)`).join('\n')
??
No, because that would do something else
so what ?
Actually, does it work with just
gGuilds.map(g => `${g.name} (${(await g.fetch()).memberCount} Members)`).join('\n')
?
So you just have to run the code. I might've overdone it
Post the whole function
async execute(client, message) {
const gGuilds = await client.guilds.fetch()
const serverEmbed = new Discord.MessageEmbed()
.setColor('PURPLE')
.setDescription(`>>> ${gGuilds.map(g => `${g.name} (${(await g.fetch()).memberCount} Members)`).join('\n')}`)
await message.channel.send({ content: `||${message.member}||`, embeds: [serverEmbed] }).then(async () => { try { await message.delete() } catch { } })
},
bruh -.-
Put it back and run. If still doesn't work, post error and code
but still
async execute(client, message) {
const gGuilds = (await client.guilds.fetch()).values()
const serverEmbed = new Discord.MessageEmbed()
.setColor('PURPLE')
.setDescription(`>>> ${gGuilds.map(g => await g.fetch()).map(g => `${g.name} (${g.memberCount} Members)`).join('\n')}`)
await message.channel.send({ content: `||${message.member}||`, embeds: [serverEmbed] }).then(async () => { try { await message.delete() } catch { } })
},
-.-
async execute(client, message) {
const gGuilds = (await client.guilds.fetch()).values()
const serverEmbed = new Discord.MessageEmbed()
.setColor('PURPLE')
.setDescription(`>>> ${gGuilds.map(g => `${g.name} (${(await g.fetch()).memberCount} Members)`).join('\n')}`)
await message.channel.send({ content: `||${message.member}||`, embeds: [serverEmbed] }).then(async () => { try { await message.delete() } catch { } })
},
async execute(client, message) {
const gGuilds = (await client.guilds.fetch()).values()
const serverEmbed = new Discord.MessageEmbed()
.setColor('PURPLE')
.setDescription(`>>> ${gGuilds.map(g => `${g.name} (${(await g.fetch()).memberCount} Members)`).join('\n')}`)
await message.channel.send({ content: `||${message.member}||`, embeds: [serverEmbed] }).then(async () => { try { await message.delete() } catch { } })
},
-.-
so what should i do ?
const gGuilds = (await client.guilds.fetch()).values();
let descriptions = [];
for(let g of gGuilds)
descriptions.push(`${g.name} (${(await g.fetch()).memberCount} Members)`);
...
.setDescription(descriptions.join(`\n`))
you are awsome
just how can i sort them ?
biggest servers on top and smallers down
@queen oasis
Well, sorting also requires .memberCount which requires await. You could do it 2 ways:
- modify the code above to obtain an array of guilds (the ones after
await guild.fetch()) - declare an asyncronous
mapfunction which you can use across the whole project (I would go with this one, as it also solves lots of problems)
You can also use Promise.all to go around the second option if you don't want to declare additional stuff
async execute(client, message) {
const gGuilds = (await Promise.all((await client.guilds.fetch()).values().map(async g => await g.fetch())).sort((g, g2) => g.memberCount - g2.memberCount);
const serverEmbed = new Discord.MessageEmbed()
.setColor('PURPLE')
.setDescription(`>>> ${gGuilds.map(g => `${g.name} (${g.memberCount} Members)`).join('\n')}`)
await message.channel.send({ content: `||${message.member}||`, embeds: [serverEmbed] }).then(async () => { try { await message.delete() } catch { } })
},
If you want biggest on top, just do g2.memberCount - g1.memberCount
@soft kraken
<rejected> TypeError: (intermediate value).values(...).map is not a function
Always post code
const gGuilds = (await Promise.all((await client.guilds.fetch()).values().map(async g => await g.fetch())).sort((g, g2) => g.memberCount - g2.memberCount));
async execute(client, message) {
const gGuilds = (await Promise.all((await client.guilds.fetch()).values().map(async g => await g.fetch())).sort((g, g2) => g.memberCount - g2.memberCount));
const serverEmbed = new Discord.MessageEmbed()
.setDescription(`>>> ${gGuilds.map(g => `${g.name} (${g.memberCount} Members)`).join('\n')}`)
await message.channel.send({ content: `||${message.member}||`, embeds: [serverEmbed] }).then(async () => { try { await message.delete() } catch { } })
},
const gGuilds = (await Promise.all([...(await client.guilds.fetch()).values()].map(async g => await g.fetch()))).sort((g, g2) => g.memberCount - g2.memberCount);
Yeah, swap g with g2 for reverse order
@soft kraken
Tbh that line looks like a nightmare, but I don't see what you can do about it to make it shorter (still one line). You must await feching all the guilds. You must use .values() cause otherwise you won't have an iterator for the guilds. You must use [... or something similar cause otherwise you can't use .map on an iterator. You need Promise.all cause the mapping is done asyncronous. Only the sort thing looks natural
hi
well i think a lot
and found a way to make it smaller
i make it txt