公会成员

时间:2020-11-05 21:07:28

标签: python discord discord.py discord.py-rewrite

我有此代码,该代码仅在具有公会ID的情况下才可以发送漫游器所在的服务器中的成员列表。这是我的代码:

@client.command(name='members')
async def _members(ctx, guild_id: int):
  guild = client.get_guild(guild_id)
  for m in guild.fetch_members(limit=None):
    await ctx.send(f"{m}")
    await ctx.send("Done!")

但是它似乎没有用,我不知道为什么。有时候我很傻。 这是我得到的错误:

172.18.0.1 - - [05/Nov/2020 20:59:40] "HEAD / HTTP/1.1" 200 -
Ignoring exception in command members:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 154, in _members
    for m in guild.fetch_members(limit=None):
TypeError: 'MemberIterator' object is not iterable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'MemberIterator' object is not iterable

此外,有没有办法从列表中删除所有机器人?

2 个答案:

答案 0 :(得分:0)

guild.fetch_members返回一个 asyncIterator ,这意味着您应该在for之前添加async关键字。但是,在您的情况下,如果您使用循环,则将导致垃圾邮件,因此我们将使列表变平并使命令生效。霍雷! 像这样:

@client.command(name='members')
async def _members(ctx, guild_id: int):
  guild = client.get_guild(guild_id)
  x = await guild.fetch_members(limit=None).flatten()
  await ctx.send(x)
  await ctx.send("DONE")
  

答案 1 :(得分:0)

根据API参考:

获取一个AsyncIterator,该AsyncIterator可以接收行会的成员。

也说:

注意:该方法是一个API调用。对于一般用法,请考虑使用members

但是,如果您想使用fetch_members而不是guild.members,则可以做两件事来防止出现此错误。

  • 您可以使用async for member in guild.fetch_members(limit=None):
  • for member in await guild.fetch_members(limit=150).flatten():

但是我建议您按照API参考所描述的那样使用guild.members

此外,如果您在for循环中发送邮件,则bot会发送大量邮件,因此您应该使用str.join()方法在一封邮件中发送所有成员。这就是我的意思:

@client.command(name='members')
async def _members(ctx, guild_id: int):
  guild = client.get_guild(guild_id)
  await ctx.send(', '.join(guild.members))

或者,如果您仍要发送单独的消息,则可以使用以下方法:

@client.command(name='members')
async def _members(ctx, guild_id: int):
  guild = client.get_guild(guild_id)
  for m in guild.members():
    await ctx.send(f'{m}')
    await ctx.send('DONE!')