#how can i get servers member count ??

1 messages · Page 1 of 1 (latest)

queen oasis
#

For each guild, do guild.memberCount

soft kraken
soft kraken
#

i try it before

olive oar
#

fetch info about guild and add resolve

soft kraken
queen oasis
#

(await g.fetch()).memberCount

olive oar
#

^^

soft kraken
#
${gGuilds.map(g => `${g.name} (${(await g.fetch()).memberCount} Members)`).join('\n')}
queen oasis
#

Tell us the error, not just a piece of code

soft kraken
queen oasis
#

Probably it complains about await keyword

#

Make the function async or resolve the promise other way

soft kraken
queen oasis
#

That's pretty hard to explain. It is JS knowledge. First learn what async functions are

queen oasis
#

You should make the function that contains ${} async

soft kraken
queen oasis
#

Do it at a higher level

#

The function that contains that line should be async

soft kraken
#
.setDescription(`>>> ${gGuilds.map(async (g) => `${g.name} (${(await g.fetch()).memberCount} Members)`).join('\n')}`)
queen oasis
#

Not within the line itself

#

So you call this .setDescription in a function. Make that function async

#

Not what's inside .setDescription

soft kraken
#
${gGuilds.map(g => `${g.name} (${(async (gu) => await gu.fetch()).memberCount} Members)`).join('\n')}
#

bro im so confused -.-

queen oasis
#

Bruh, show the whole function

soft kraken
queen oasis
#

Doesn't matter. Show the function that creates the embed

soft kraken
#

    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 { } })
    },
queen oasis
#

Hmm, the function is fine, can you show the code you used when you got that first error?

soft kraken
#
    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

queen oasis
#

Ok so, client.guilds.fetch() likely returns a collection

#

You need to use .values() after awaiting it

soft kraken
#

const gGuilds = await client.guilds.fetch().values()

#

or

#

const gGuilds = await client.guilds.values()

queen oasis
#

No one

#

(await client.guilds.fetch()).values()

soft kraken
#

oh

#

result of console.log const gGuilds = (await client.guilds.fetch()).values()

#

same as await client.guilds.fetch()

queen oasis
#

It is clearly not the same

soft kraken
#

wait

queen oasis
#

Now you get what you need. The next step is to covert this guild partial into an actual guild

soft kraken
queen oasis
#

You can do that by using await guild.fetch()

queen oasis
soft kraken
#

like

        const Guilds = (await client.guilds.fetch()).values()
        const gGuilds = await Guilds.fetch()
queen oasis
#

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')
soft kraken
#

oh

queen oasis
#

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

soft kraken
#

maybe js gGuilds.map(async (g) => await g.fetch()).map(g => `${g.name} (${g.memberCount} Members)`).join('\n')
??

queen oasis
#

No, because that would do something else

soft kraken
#

so what ?

queen oasis
#

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

soft kraken
#

no

queen oasis
#

Post the whole function

soft kraken
#
    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 { } })
    },
queen oasis
#

Bruh

#

You removed the .values()

#

That part is good

soft kraken
#

bruh -.-

queen oasis
#

Put it back and run. If still doesn't work, post error and code

soft kraken
#

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 { } })
    },
queen oasis
#

...

#

Just copy-paste this:

soft kraken
#

-.-

queen oasis
#
    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 { } })
    },
soft kraken
#

    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 { } })
    },
queen oasis
#

Ooooh

#

You can't use async inside .map()

soft kraken
#

-.-

queen oasis
#

Similar to how we avoided it above

#

It will map to a promise

soft kraken
#

so what should i do ?

queen oasis
#
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`))
soft kraken
#

you are awsome

#

just how can i sort them ?

#

biggest servers on top and smallers down

#

@queen oasis

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 map function 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

soft kraken
#

<rejected> TypeError: (intermediate value).values(...).map is not a function

queen oasis
#

Always post code

soft kraken
#
        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 { } })
    },
queen oasis
#
const gGuilds = (await Promise.all([...(await client.guilds.fetch()).values()].map(async g => await g.fetch()))).sort((g, g2) => g.memberCount - g2.memberCount);
soft kraken
#

fixd tnx

#

just need little change

queen oasis
#

Yeah, swap g with g2 for reverse order

soft kraken
#

tnx a lot

#

you are the best !

queen oasis
#

@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

soft kraken
#

hi

soft kraken
#

and found a way to make it smaller

#

i make it txt

queen oasis
#

@soft kraken

#

I think you misundestood me. I was talking about the code, not output

#

But if I think again, the code looks good too

#

I guess that's the beauty of coding