【问题标题】:How can I get my bot to DM a specific user ID without a command given?如何在没有给出命令的情况下让我的机器人 DM 特定用户 ID?
【发布时间】:2019-12-11 15:58:10
【问题描述】:

当某人 DM 是我的机器人时,我会将其打印到发送消息的控制台,消息是什么以及消息发送的时间。我希望机器人然后 DM 我通知我有人给它发了消息,和/或谁和什么时间。

我已经通过不同的解决方案看到了很多关于此的问题。其中很多已经过时,因此不适用于我的代码。我在 Spyder 上使用 Python 3.7。

我可以让机器人通过 bot 命令向用户发送 DM,但无法将它发送给我的特定用户 ID。

这是将收到的 DM 消息打印到控制台的代码。同样,这部分没有问题。只需将此信息私信给我。

if isinstance(message.channel, discord.DMChannel):
        print("******************************")
        print("DM Recieved by: " + message.author.name)
        print("Time:", str(datetime.datetime.now()))
        print("Message: " + message.content)
        print("******************************")

更新代码:

if client.user.mentioned_in(message) and message.mention_everyone is False:
        await message.delete()
        channel = message.channel
        await channel.trigger_typing()
        await channel.send("{0.mention} Please don't tag me.".format(message.author))
        print("**************************************************")
        print("Mentioned By: " + message.author.name)
        print("Time:", str(datetime.datetime.now()))
        print("Message: " + message.clean_content)
        print("Channel: " + str(message.channel))
        print("**************************************************\n")

        #DM me when bot get's mentioned.
        client.get_user(305508822485827584)
        await user.send("Test")

【问题讨论】:

    标签: python bots discord private dm


    【解决方案1】:

    在我看来,您使用的是旧的异步版本 discord.py - 我强烈建议您转移到较新的重写分支,因为对异步版本的支持现已停止。

    Migrating to v1.0 (rewrite)

    client.get_user(ID) 是一种重写方法,在 discord.py 的异步版本中不存在。在这种情况下,您可以使用client.get_user_info(ID)

    希望这会有所帮助 - 编码愉快!

    编辑:这是您需要使用的代码:

    user = client.get_user(305508822485827584)
    await user.send("Test")
    

    【讨论】:

    • 我试了一下,我得到了这个 AttributeError: 'Client' object has no attribute 'get_user_info'
    • 查看迁移,我使用了很多变化。我对机器人命令用户的 DM 遵循 discord.Embed,除非我感到困惑?我是不和谐机器人的新手,对 python 有一些经验
    • 您使用的是什么版本的 discord.py?可以使用discord.__version__查看
    • 说我使用的是 1.2.3
    • 使用相同的代码,但将await client.send(user, 'INSERT MESSAGE CONTENT HERE')替换为await user.send('message')
    猜你喜欢
    • 2020-07-28
    • 2018-05-10
    • 2021-12-01
    • 2021-11-25
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 2021-03-17
    • 2021-12-23
    相关资源
    最近更新 更多