首页 > 解决方案 > 如何在 discord.py 中向特定标签发送消息?

问题描述

这是我尝试过的:

import discord

client = discord.Client()

@client.event
async def on_ready():
    print("Logged in as: {}".format(client.user))
    user = client.get_user('741143876563370086')
    await user.send("Hi") 
client.run('token')

但是,它没有用。我知道有一个类似的问题,但是那个问题使用了一个命令,而我只想发送给特定的用户。

编辑:我用一些新代码编辑了它

标签: pythondiscorddiscord.py

解决方案


可悲的是,您需要首先启用意图,与其他命令不同,此命令在on_ready事件上与 dm 用户不同。您可以在https://discord.com/developers/applications上启用它们,然后转到您的机器人,然后在下面的这张图片上启用它们(对不起,糟糕的圈子事情 lmao)e

现在您启用了这些功能,您就可以开始编码了。

我们把我们的意图放在首位client

intents = discord.Intents(messages = True, members = True)
client = commands.Client(intents = intents)

有关意图的更多信息将显示在这里

好的,关于on_ready事件:

@client.event
async def on_ready():
    print("We are logged in as: {}".format(client.user.name))
    user = client.get_user(User_Id_Here) # You need to copy your user ID to make it work
    await user.send('Your Message Here') # Your message here, something like "Logged in!"

它对我有用。常见问题解答:我如何发送 DM?

稍后谢谢我 :D


推荐阅读