如何在没有给出命令的情况下让我的机器人 DM 特定用户 ID? [英] How can I get my bot to DM a specific user ID without a command given?

查看:77
本文介绍了如何在没有给出命令的情况下让我的机器人 DM 特定用户 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When someone DM's my bot, I have it print to the console who messaged, what the message was, and the time the message was sent. I would like the bot to then DM me to either notify me someone messaged it, and/or who and what and when.

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

I've seen many issues about this with different solutions. A lot of them were outdated therefore do not work with my code. I use Python 3.7 on Spyder.

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

I can get the bot to DM a user on bot command but cannot get it to DM a specific user ID which would be mine.

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

Here's the code that prints DM messages received to the console. Again, no issues with this part. Just need this info private messaged to me.

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")

推荐答案

在我看来,您正在使用 discord.py 的旧异步版本 - 我强烈建议移至较新的 rewrite 分支以支持异步版本现已停止.

It seems to me that you're using the old async version of discord.py - I highly recommend moving to the newer rewrite branch as support for the async version has now ceased.

迁移到 v1.0(重写)

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

client.get_user(ID) is a rewrite method and doesn't exist in the async version of discord.py. You can use client.get_user_info(ID) in this case.

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

Hope this helps - happy coding!

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

EDIT: Here's the code you need to use:

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

这篇关于如何在没有给出命令的情况下让我的机器人 DM 特定用户 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆