0

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")
Brandon Weeks
  • 69
  • 1
  • 2
  • 11

1 Answers1

2

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")
aryan
  • 141
  • 5
  • I try that and I get this AttributeError: 'Client' object has no attribute 'get_user_info' – Brandon Weeks Aug 03 '19 at 07:59
  • Looking through the migration I use a lot of whats changed. My DM to user on bot commands follow discord.Embed, unless im getting confused? Im new to discord bot, some experience with python – Brandon Weeks Aug 03 '19 at 08:01
  • What version of discord.py are you using? You can use ``discord.__version__`` to check – aryan Aug 03 '19 at 18:58
  • Says im using 1.2.3 – Brandon Weeks Aug 03 '19 at 21:59
  • Use the same code then but replace ``await client.send(user, 'INSERT MESSAGE CONTENT HERE')`` with ``await user.send('message')`` – aryan Aug 04 '19 at 05:54
  • Doing that I get: TypeError: object NoneType can't be used in 'await' expression if I take the await out, I get this: AttributeError: 'NoneType' object has no attribute 'send' – Brandon Weeks Aug 04 '19 at 06:04
  • You need the ``await`` for that statement as it is an asynchronous operation, but the error means that ``client.get_user(ID)`` is returning nothing. Are you sure it's a valid User ID? – aryan Aug 04 '19 at 16:59
  • That gives me: await user.send('Test') NameError: name 'user' is not defined – Brandon Weeks Aug 05 '19 at 03:50
  • Could you paste the snippet of code again? That error means you're not declaring the variable "user", which shouldn't be the case – aryan Aug 05 '19 at 07:14
  • Updated my original post to show when im trying to get the bot to DM me. At the bottom is where im getting my id and trying to send a message – Brandon Weeks Aug 05 '19 at 07:21
  • Ah you haven't assigned ``client.get_user(ID)`` to ``user`` - I've edited the answer to incorporate that. – aryan Aug 05 '19 at 16:04
  • That did it! Thank you so much for your time, very appreciated! – Brandon Weeks Aug 05 '19 at 23:19