Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python discord.py向新服务器成员发送直接消息_Python_Discord_Discord.py - Fatal编程技术网

Python discord.py向新服务器成员发送直接消息

Python discord.py向新服务器成员发送直接消息,python,discord,discord.py,Python,Discord,Discord.py,我的代码是: import discord class MyClient(discord.Client): async def on_ready(self): print("Bot ready") async def on_member_join(member): await member.send("nice to see u on the server") client = MyClient()

我的代码是:

import discord

class MyClient(discord.Client):

    async def on_ready(self):
        print("Bot ready")

    async def on_member_join(member):
        await member.send("nice to see u on the server")

client = MyClient()
client.run(My token)

但是消息没有被发送。也许有人能给我一个正确的工作代码。谢谢您的时间。

这是一个很好的问题,但是您可以通过查找找到更简单的结果。一个好的开始来源是它也回答了你的问题

#@bot or #@client
@client.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(
        f'Hi {member.name}, #message'
    )

有错误吗?这回答了你的问题吗?你需要让会员能够在“会员加入”活动上接收这不是真正的问题,member.send internal calls member.create_dm和member.dm_channel.send除了@ukaszKwieciński所说的,如果您使用try-except,则更好,因为某些成员不允许DMs,这将导致错误,并且如果您使用错误处理程序,它将捕获错误。