Discord.py Self Bot获取所有用户ID

时间:2020-07-23 20:23:11

标签: python discord discord.py

我的源代码:

@bot.command()
async def logall(ctx, meesage):
  await ctx.message.delete()
  with open('logs.json', 'r') as file:
    user_list = json.load(file)
  members = await guild.fetch_members(limit=150).flatten()
  for member in members:
    user_list.append(member.id)
  with open('logs.json', 'w') as file:
    json.dump(user_list, file)
  print('Logged All Users!')

我使用了不同的方法来获取用户ID,但实际上没有任何方法可以获取任何服务器的所有用户ID。
错误:

Traceback (most recent call last):
  File "C:\Users\JasonTorrez\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\JasonTorrez\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\JasonTorrez\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'guild' is not defined

我试图能够从我加入的任何服务器中收集用户ID。

2 个答案:

答案 0 :(得分:0)

Discord禁用了自助机器人的该功能,除非您的帐户有权踢/禁服务器中的成员。现在,您需要一种称为“意图”的东西,而您无法作为一个自我机器人来实现。

答案 1 :(得分:-1)

只需使用discord.client.guilds返回一个Guild对象的列表,然后使用discord.Guild.members返回一个Member对象的列表:

@bot.command()
async def logall(ctx, meesage):
    await ctx.message.delete()
    with open('logs.json', 'r') as file:
        user_list = json.load(file)
    for guild in bot.guilds:
        for member in guild.members:
            if not member in user_list:
                user_list.append(member.id)
    with open('logs.json', 'w') as file:
        json.dump(user_list, file)
    print('Logged All Users!')

参考: discord.py documentation