Python 3.x 我已经开始在discord中创建一个bot,但是bot没有向服务器的成员发送dms

Python 3.x 我已经开始在discord中创建一个bot,但是bot没有向服务器的成员发送dms,python-3.x,discord,bots,discord.py,Python 3.x,Discord,Bots,Discord.py,我一直在运行以下代码,在各种更改之后,bot仍然不会向用户返回欢迎dm,即使在确保启用了从服务器向用户发送消息的权限之后也是如此 import os import discord from dotenv import load_dotenv load_dotenv() TOKEN = os.getenv('DISCORD_TOKEN')# gets the token of the bot from the .env file client = discord.Client()# make

我一直在运行以下代码,在各种更改之后,bot仍然不会向用户返回欢迎dm,即使在确保启用了从服务器向用户发送消息的权限之后也是如此

import os
import discord

from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')# gets the token of the bot from the .env file

client = discord.Client()# makes and discord client

@client.event #client on the following event:
async def on_ready():
        print(f'{client.user} has connected to Discord!')# tells us if the bot is connected
        
@client.event
async def on_member_join(member):
    await member.create_dm()# creates an dm channel
    await member.dm_channel.send( # sends the string given as argument to the channel created
        f'Hiiiiiiiiiii {member.name}, welcomeeeeeeeee icyyyyyy here'
    )

client.run(TOKEN)# runs client for the bot token in TOKEN 

并且代码成功返回bot已连接到discord并且在服务器中联机。

您需要启用
dm
意图(有很多意图,因此我建议您启用默认意图)和
意图。成员

intents=discord.intents.default()
intents.members=True
客户端=不一致。客户端(意图=意图)
不要忘记在中启用特权成员意图

另外,
dm_频道
可以是
None
如果没有创建,这里有一个更好的解决方案

await member.send('whatever')