Discord.py 无法向会员发送私信

Discord.py cannot send dm to member

错误代码为:

member = message.guild.get_member(message.author.id)
await member.send(embed=level_up)

尝试执行第二行时,出现错误:

AttributeError: 'ClientUser' object has no attribute 'create_dm'

我在文档中读到 message.guild.get_member() 应该 return 一个 Member 对象,但是由于某种原因它 return 是一个 ClientUser 对象,您不能向其发送消息。 我想发私信给 message.author

全功能代码:

    @commands.Cog.listener()
    async def on_message(self, message):
        if not isinstance(message.channel, discord.channel.DMChannel) \
                and not message.content.startswith(yaml_data["Bot"]["Prefix"]):

            management = message.guild.get_role(yaml_data["Tickets"]["Management_Role"])
            operations = message.guild.get_role(yaml_data["Tickets"]["Operator_Role"])
            ownership = message.guild.get_role(yaml_data["Tickets"]["Owner_Role"])

            with open(arrays_folder + r"/members.json") as members_file:
                members_data = json.load(members_file)

            if random.random() < yaml_data["Xp"]["Xp_Chance"]:

                level_before = self.get_level(members_data["members"][str(message.author.id)]["Xp"])
                members_data["members"][str(message.author.id)]["Xp"] += 1
                level_after = self.get_level(members_data["members"][str(message.author.id)]["Xp"])

                with open(arrays_folder + r"/members.json", "w+") as f:
                    json.dump(members_data, f, indent=4)

                if level_after > level_before \
                        and not any(role in message.author.roles for role in (ownership, operations, management)):
                    if level_after < yaml_data["Xp"]["Levels"] and message.author.id != self.client.user.id:

                        level_up = discord.Embed(
                            description="You have leveled up!\n\n"
                                        f"You are now **Level {level_after}**!",
                            color=yaml_data["Customisation"]["General_Embed_Colour"])

                        new_role = discord.utils.get(message.guild.roles, name="Level " + str(level_after))

                    else:

                        level_up = discord.Embed(
                            description="You have leveled up!\n\n"
                                        f"You are now **{yaml_data['Xp']['Max_Level_Name']}**!",
                            color=yaml_data["Customisation"]["General_Embed_Colour"])

                        new_role = discord.utils.get(message.guild.roles, name=yaml_data['Xp']['Max_Level_Name'])

                    member = message.guild.get_member(message.author.id)
                    await member.send(embed=level_up)
                    await message.author.send(embed=level_up)
                    old_role = discord.utils.get(message.guild.roles, name="Level " + str(level_before))
                    await member.remove_roles(old_role)
                    await member.add_roles(new_role)

问题是 Privileged Gateway Intents. 如果您不知道 Discord 最近带来了 Gateway Intents。跟踪服务器成员的机器人需要这些意图。当您发送“会员”DM 时,您需要会员意图。如果没有意图,“get_member”功能仅 returns 机器人用户。
要获得意图,请前往开发人员门户和机器人部分(您有机器人的令牌)。向下滚动直到你看到;特权网关意图。打开 Presence 和 Member 的意图并保存。完成后,转到您的代码。 (确保您使用的是 Discord.py 1.5+,因为该版本引入了意图。)因此,在您的代码中:

# after importing libraries or modules, where you are declaring client or bot variable;
client = commands.Bot(command_prefix="prefix", intents=discord.Intents.all())

# rest of the code with your command or function [...]

client.run("token")

此解释对您来说可能不够,请务必阅读有关该主题的官方文档以获得更好的解释和处理意图的步骤。单击 here 以获取文档参考。

注意:如果您的机器人在 75 台以上的服务器中,您将需要从 discord 验证才能使用意图,而且,对于 75 台以上的服务器,您还必须仅启用你需要像如果你不需要存在意图你不能使用它但是如果你的机器人在少于 75 个服务器中自由使用意图没有限制(现在,一旦机器人达到 75 个服务器,你将需要验证)