Create DM on message [discord.py]

Discord bots can’t send DM’s to eachother, only to Users. Your bot is trying to send a DM to itself, which it can’t. You need to check with an if-statement if the message’s author is a bot or not.

You’re already doing this later on in your code, to see if it should send the message or not, but the create_dm() function at the top will have already failed at that point (as it can’t create a dm), so it will never get there.

@commands.Cog.listener()
async def on_message(self, message):
    if message.author.bot:
        return # Ignore bot messages

    if not message.guild:
        return

    if not message.channel.name == 'roles':
        return

    elif message.content != 'role':
        await message.author.send(message.content)
        await message.delete()

PS. A few more things to note here:

  • You can just use User.send() instead of create_dm() & dm.send().

  • An f-string with nothing but the content can just be the content itself (f"{word}" == word, or str(word) in case it’s not a string).

  • You have an if checking if the channel name is not “roles”, and afterwards in the elif you check if it is – if it wouldn’t be “roles” then it would’ve gotten into the first if & returned so the second one is unnecessary.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top