【问题标题】:How do I trigger my bot to DM a user after an on_message event discord.py如何在 on_message 事件 discord.py 后触发我的机器人向用户发送消息
【发布时间】:2021-04-22 03:03:52
【问题描述】:

如果我的机器人输入某个单词(例如字母“E”),我试图让他们直接向用户发送消息,但我不知道该怎么做。任何帮助表示赞赏!

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    我猜这就是你的意思:

    @bot.event
    async def on_message(message):
       if(message.content == 'E'):
          await message.author.send('You typed E!')
    

    【讨论】:

    • 感谢您的回复,我试过了,但由于某种原因没有任何反应。我没有收到错误消息,但没有发送任何消息,知道为什么会这样吗?
    • @Matt 你到底在 discord 中输入了什么?
    • @Matt 你的机器人在运行吗?在 on_message 事件中添加打印消息,首先检查事件是否触发,然后尝试发送消息。请注意,单独“输入”E 不会触发事件,您必须发送仅包含“E”的消息才能触发答案。
    • 顺便说一句,此代码也区分大小写。所以,你必须把大写的E
    【解决方案2】:

    这里有几点需要注意:
    message.channel.send(<message>) 函数用于像服务器上的公共响应。 message.author.send(<message>) 函数用于私人回复或直接消息(DM)。

    @bot.event
    async def on_message(message):
       if(message.content == 'E'):
          await message.author.send('You typed E!')`enter code here`
    

    【讨论】:

    • 用户想将Direct Message (DM)改为messaging author不公开留言,请尝试将message.channel改为message.author。但之后你的回答将与FlameDev 回答相同,请尝试更新它
    【解决方案3】:

    您可以使用Member.create_dm() method

    @bot.event
    async def on_message(message):
        if message.content.lower() == "e":
            dmchannel = await message.author.create_dm()
            await dmchannel.send("You typed E!")
    

    【讨论】:

    • 对于直接复制粘贴此内容的任何人,都会出现拼写错误。它应该说“如果 message.content.lower()”
    猜你喜欢
    • 2021-12-03
    • 2021-10-14
    • 2019-09-19
    • 2021-02-19
    • 2022-01-03
    • 2021-01-26
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    相关资源
    最近更新 更多