【问题标题】:discord.py bot can't see membersdiscord.py bot 看不到成员
【发布时间】:2021-01-21 15:23:55
【问题描述】:

我正在尝试创建一个不和谐的机器人,它创建一个类别并为角色和用户设置权限,将用户 ID 作为参数:

for x in range(2, len(args)):
member= client.get_user(int(args[x]))
await message.guild.categories[-1].set_permissions(member, send_messages = True)

其中 args 是包含命令、类别名称和用户 ID 的字符串数组。 用法应该是:

!create category-name 0000000 1111111

我对此有一些问题,因为就像机器人看不到服务器的成员一样,他唯一可以添加的用户是我,如果在第一个参数中指定了服务器的所有者(我的 000000例子)。 如果我输入其他人的 ID,则机器人不会在类别中为该用户添加权限。 我发现机器人可能看不到其他用户,事实上如果我把这条线:

print(message.guild.members)

它只会将机器人打印为成员,

打印:[]

我不知道为什么会统计成员但看不到其他用户,我该怎么办?

【问题讨论】:

  • 您使用的是什么版本的 Discord.py? (pip show Discord.py 在控制台查看)
  • 您可能需要启用网关意图,请参阅this answer
  • 兄弟,谢谢!这是意图,我决定将其添加到代码中:intents = discord.Intents.default() intents.members = True

标签: python discord discord.py


【解决方案1】:

安装库的 1.4.2 版本:pip install -U discord.py==1.4.2

【讨论】:

    【解决方案2】:

    由于 derw 链接的答案不再可用;

    正如 OP 所指出的,我设法通过实例化 discord bot 客户端来解决这个问题,如下所示:

    intents = discord.Intents.default()
    intents.members = True
    client = discord.Client(intents=intents)
    

    此外,我必须在 Discord 开发者门户中的机器人设置下启用“服务器成员意图”特权网关意图:

    Enabling Server Members Intent

    【讨论】:

      【解决方案3】:

      您不需要像那样拆分它,我认为您不知道,但您应该真正检查来自 here 的命令,指定 async def 函数的参数,然后您可以执行以下操作:

      from discord.ext import commands
      
      intents = discord.Intents.default()
      intents.members = True
      token = "YourBotsTokenHere"
      bot = commands.Bot(command_prefix='!', intents=intents) # Or whatever prefix you want
      
      @bot.event
      async def on_ready():
          print(f"Bot {bot.user} has connected to Discord!")
      
      @bot.command()
      async def create(ctx, type_, member: discord.Member, categoryName=None, *, name):
          # member is going to return a member object so u can do stuff like member.id or member.name, etc...
          if type_.lower() == "category":
              # Code to create a category with the name variable
              # some pseudo-code that might look similar to what u need
              role = get(member.server.roles, name="NameOfTheRole")
              await bot.add_roles(member, role)
              # insert a part of ur own code here for category thingy
      
          elif type_.lower() == "channel":
              # Code to create a channel with the name variable, category name must be also specified so we can check to what category the user is reffering to, also check if the user has the role to create that channel
              if categoryName == None: return
              catgName = categoryName.replace('-', ' ') # Replaces "-" with an empty space
      
      bot.run(token)
      

      至于其他东西,启用members 意图可以正常工作,我已经在示例中编写了它

      【讨论】:

        猜你喜欢
        • 2021-07-19
        • 2021-09-03
        • 2021-05-07
        • 2021-07-05
        • 1970-01-01
        • 2021-06-18
        • 2021-02-20
        • 2021-02-18
        • 2020-02-22
        相关资源
        最近更新 更多