List all guilds with multiple servers on a single embed

最后都变了- 提交于 2021-02-17 06:31:08

问题


This is my current code which shows the servers it is currently in. Which this does what I would like it to do, but it is not efficient and can be avoided instead of sending an embed message for each server it gets.

@client.command()
@commands.is_owner()
async def list_guilds(ctx):
    servers = client.guilds
    for guild in servers:
        embed = discord.Embed(colour=0x7289DA)
        embed.set_footer(text=f"Guilds requested by {ctx.author}", icon_url=ctx.author.avatar_url)
        embed.add_field(name=(str(guild.name)), value=str(guild.member_count)+ " members", inline=False)
        await ctx.send(embed=embed)

What I would like to do is loop though all servers it is in and send an embed only after it has included 10 servers on a single embed, then it would send another, avoiding the use of embed spam.

for client.guilds in range(10):

Then for example, the embed should look like:

Guilds list (page 1) showing 10 per embed

Discord server 0
Discord server 1
Discord server 2
Discord server 3
Discord server 4
...
....

Which would simply create an embed with 10 servers on it, the name and the server owner etc, but currently just need help with sending multiple server names on one embed, instead of sending multiple embeds for each server, which it would send 600+ currently. Would anyone please be able to help?


回答1:


Iterate every ten values of the list, here's an example:

>>> lst = list(range(1, 26))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 25]
>>>
>>> for i in range(0, len(lst), 10):
...     print(lst[i:i + 10])
...
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[21, 22, 23, 24, 25]

Here's applied to your code:

for i in range(0, len(client.guilds), 10):
    embed = discord.Embed(title='Guilds', colour=0x7289DA)
    guilds = client.guilds[i:i + 10]

    for guild in guilds:
        embed.add_field(name=guild.name, value='whatever')

    await ctx.send(embed=embed)



回答2:


I would say you should use a description in this case. You can hold all the information you want in the description about each guild. In the case below that I created, I go through every guild the bot is in and add the name and member count in the existing description_info variable. It just keeps piling on until you reach the end of the list. Then, you can just use that one variable in your embed.

@client.command()
@commands.is_owner()
async def list_guilds(ctx):
    servers = client.guilds
    description_info = ""
    
    for guild in servers:
        description_info += "**" + str(guild.name) + "**\n" + str(guild.member_count) + " members\n\n" # This will loop through each guild the bot is in and it the name and member count to a variable that holds everything

    embed = discord.Embed(description = description_info, colour=0x7289DA) # Edit the embed line to include the description we created above
    embed.set_footer(text=f"Guilds requested by {ctx.author}", icon_url=ctx.author.avatar_url)
    await ctx.send(embed=embed)


来源:https://stackoverflow.com/questions/65321658/list-all-guilds-with-multiple-servers-on-a-single-embed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!