Ad

How Can I Get My Bot To DM A Specific User ID Without A Command Given?

- 1 answer

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.

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.

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.

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

Updated Code:

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

Answer

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.

Migrating to v1.0 (rewrite)

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")
Ad
source: stackoverflow.com
Ad