首页 > 解决方案 > How to send a DM to a user on another server

问题描述

This code is designed to automatically send a DM to a user ID registered in the database at a specific time, but typically it comes to me and an error occurs when attempting to DM to another user (Error Content:'NoneType' object has no attribute 'send'). Therefore, my question is to know how to send a DM to a user with a user ID and to the server to which the bot is connected.

标签: pythondiscorddiscord.py

解决方案


当我谈论机器人名称时,我的意思是您将前缀设置为 ( 的变量的名称client= commands.Bot(command_prefix='+'),客户端将是我的机器人名称)

在 discord.py 的重写中,您不会通过机器人发送消息。要向频道发送消息,请使用 channel.send(),因此要向用户发送消息,请使用 user.send()

要获取用户,请使用您的机器人名称。进入:

user = <bot_name>.get_user(<user_id>) # this is the user you will dm
print(type(user)) # this prints the object type of user, this is just to make sure that the bot actually found a user
user.send("msg") # this sends the message, and msg is the message content

下次,请发布代码和您尝试过的内容。


推荐阅读