I'm trying to have my Discord bot delete messages when reacting with an emoji. I have some code for it, which works in a Discord server, but it does not work in DMs
This is my code
self = discord.Bot()
@self.event
async def on_raw_reaction_add(ctx):
if ctx.emoji.name == '❌':
message = await self.get_channel(ctx.channel_id).fetch_message(ctx.message_id)
await message.delete()
This is the error
AttributeError: 'NoneType' object has no attribute 'fetch_message'
Since I figure DMs apparently don't have "channels", I've tried getting just the specific message ID with
message = ctx.message_id
but I can't find any way to delete messages by ID that works with on_raw_reaction_add
All the similar questions I've found here about deleting bot DMs aren't working with on_raw_reaction_add. Any help is appreciated, thank you.
You need to use user.fetch_message
to fetch a message in a DMChannel
. So you could check if the channel exists, and if it does not, you can get the user instead.
Example:
@self.event
async def on_raw_reaction_add(ctx):
if ctx.emoji.name == "❌":
channel = self.get_channel(ctx.channel_id)
if not channel:
channel = await self.fetch_user(ctx.user_id)
message = await channel.fetch_message(ctx.message_id)
await message.delete()
Please keep in mind that you will not be able to delete the messages of the other person, only your own bot's messages. This is a discord limitation.