Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列出在单个服务器上具有多个服务器的所有公会_Python_List_For Loop_Discord_Discord.py - Fatal编程技术网

Python 列出在单个服务器上具有多个服务器的所有公会

Python 列出在单个服务器上具有多个服务器的所有公会,python,list,for-loop,discord,discord.py,Python,List,For Loop,Discord,Discord.py,这是我当前的代码,显示了它当前所在的服务器。这正是我希望它做的,但它不是有效的,可以避免,而不是为它得到的每台服务器发送嵌入消息 @client.command() @commands.is_owner() async def list_guilds(ctx): servers = client.guilds for guild in servers: embed = discord.Embed(colour=0x7289DA) embed.set

这是我当前的代码,显示了它当前所在的服务器。这正是我希望它做的,但它不是有效的,可以避免,而不是为它得到的每台服务器发送嵌入消息

@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)
我想做的是循环它所在的所有服务器,只有在一个嵌入中包含10个服务器后才发送一个嵌入,然后它会发送另一个,避免使用嵌入垃圾邮件

for client.guilds in range(10):
然后,例如,嵌入应该如下所示:

Guilds list (page 1) showing 10 per embed

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

这将简单地创建一个包含10台服务器的嵌入,包括名称和服务器所有者等,但目前只需要在一个嵌入上发送多个服务器名称的帮助,而不是为每个服务器发送多个嵌入,它当前将发送600多个服务器。有谁能帮忙吗?

我想你应该在这种情况下使用描述。你可以在每个公会的描述中保存你想要的所有信息。在我创建的下面的示例中,我检查了机器人所在的每个公会,并将名称和成员数添加到现有的
description\u info
变量中。它只是不断堆积,直到你到达列表的末尾。然后,您可以在嵌入中使用这一个变量

@client.command()
@commands.is_owner()
异步定义列表协会(ctx):
servers=client.guilds
description_info=“”
对于服务器中的公会:
description_info+=“**”+str(guild.name)+“**\n”+str(guild.member\u count)+“members\n\n”#这将循环通过机器人所在的每个公会,并将名称和成员计数添加到一个包含所有内容的变量
embed=discord.embed(description=description_info,color=0x7289DA)#编辑嵌入行以包含我们在上面创建的描述
embed.set_footer(text=f“由{ctx.author}请求的行会”,icon_url=ctx.author.avatar_url)
等待ctx.send(嵌入=嵌入)

列表中每十个值迭代一次,下面是一个示例:

lst=list(范围(1,26)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 25] >>> >>>对于范围内的i(0,len(lst),10): ... 打印(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] 以下内容适用于您的代码:

范围内的i(0,len(客户协会),10):
嵌入=discord.embed(title='Guilds',color=0x7289DA)
公会=客户.公会[i:i+10]
对于公会中的公会:
embed.add_字段(name=guild.name,value='whatever')
等待ctx.send(嵌入=嵌入)

我会遇到一个错误,因为我的机器人在500多个公会中,所以它不允许将它们放在一个嵌入式上。啊,是的,对不起,我忘了给每个嵌入式增加10个公会的限制,我的错。