discord.py | How do I check if the message being sent is being sent directly to the bot (DM) or in a chat?

﹥>﹥吖頭↗ 提交于 2021-02-05 10:57:05

问题


I want to check to see if the message being sent (on_message()) is in a DM sent to the bot or just sent in any other place like a chat in a guild. Any help would be greatly appreciated.

@client.event
async def on_message(message):
    if message type is dm:
        #do stuff
    elif message type is not dm:
        #do something else```

回答1:


You can check message.guild.

@client.event
async def on_message(message):
    if message.guild: # message is not DM
        # do stuff
    else: # message is DM
        # do something else```


来源:https://stackoverflow.com/questions/60733171/discord-py-how-do-i-check-if-the-message-being-sent-is-being-sent-directly-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!