【发布时间】:2021-06-19 18:13:28
【问题描述】:
discord.py 如何获取用户加入的所有服务器的列表?
我试过了,但失败了
await ctx.send(str(ctx.author.guilds))
但是,失败了……
如何获取用户加入的所有服务器的列表?
【问题讨论】:
标签: discord.py
discord.py 如何获取用户加入的所有服务器的列表?
我试过了,但失败了
await ctx.send(str(ctx.author.guilds))
但是,失败了……
如何获取用户加入的所有服务器的列表?
【问题讨论】:
标签: discord.py
可能不是最快的方法,但一种方法是检查你的机器人所在的每个公会,然后检查 ctx.author 是否在该公会中。如果作者在该公会中,则该公会会附加到您的列表中,在这种情况下为shared_guilds。
@client.command()
async def test(ctx):
shared_guilds = []
for guild in client.guilds:
if ctx.author in guild.members:
shared_guilds.append(guild)
# This is just a clean way to send the message
message = ""
for guild in shared_guilds:
message += f"`{guild.name}` \n"
await ctx.send(f"Guilds I share with {ctx.author}: \n{message}")
上面的代码如下所示:
如果上述方法不起作用,您可能需要启用 intents。 You can view how to do this here.
编辑
discord.py 版本 1.7+ 有 User.mutual_guilds 属性:
@client.command()
async def test(ctx):
shared_guilds = ctx.author.mutual_guilds
# This is just a clean way to send the message
message = ""
for guild in shared_guilds:
message += f"`{guild.name}` \n"
await ctx.send(f"Guilds I share with {ctx.author}: \n{message}")
版本1.7尚未发布,你可以等到发布,或者直接从github安装。
【讨论】:
User.mutual_guilds 属性
ctx.author.mutual_guilds