-2

I have a bot that I was testing by making it send DMs to my account but now I want to delete the messages that are already sent. I have searched all over and I did not find any thing. In short I need to know how to make my bot delete it’s own messages from the command user's DM.

I try the method @Mars Buttfield-Addison told me and I am geting a error ERROR:

Ignoring exception in command clear_dm:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 50, in clear_dm
    async for message in client.user.dm_channel.history(limit=messages_to_remove):
AttributeError: 'ClientUser' object has no attribute 'dm_channel'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'ClientUser' object has no attribute 'dm_channel'
Broken DEV
  • 39
  • 7

1 Answers1

1

You can access the dm_channel property on a Member object.

Now, I don't fully understand what you are trying to say, but if you want to receive a message on a DM and check if it was on a dm, you can do:

import discord
import asyncio

TOKEN = ''
intents = discord.Intents(messages=True, guilds=False, members=False)
client = discord.Client(intents=intents)

@client.event
async def on_message(message):
    if isinstance(message.channel, discord.channel.DMChannel):
        print("got a DM")

client.run(TOKEN)

Now if you want to delete things, you can just access this channel like a normal one, by doing message.channel (you can delete, send message, or do whatever you like just like you would on a guild)

Berlm
  • 447
  • 3
  • 7
  • what if the dm already there and i want to delete all messages send by the bot – Broken DEV Aug 15 '21 at 07:09
  • To delete messages, you can look [here](https://stackoverflow.com/questions/43465082/python-discord-py-delete-all-messages-in-a-text-channel). It will be similar to deleting messages on a guild channel, since both inherit from the same class and have the same shared methods. You can also delete using the [history](https://discordpy.readthedocs.io/en/latest/api.html#discord.User.history), which is a newer way – Berlm Aug 15 '21 at 07:11
  • @Hiddencoder you are accessing the `dm_channel` on the user, but like I said you have to check the channel on the message. My code works perfectly when I run it – Berlm Aug 16 '21 at 14:57