pythondiscordsyntax-errordiscord.pydm

discord.py - Error command with DM member


I wanted to make an error command for my code, that gets shown when the dms are turned off or the user wasn't found, here's my code:

@client.command()
async def DM(ctx, user: discord.User, *, message=None):
        message = message or "DM message to be sent"
        await user.send(message)

Solution

  • It is as simple as:

    You can read the docs here. It will raise a Forbidden error if you can't send the message.

    @client.command()
    async def DM(ctx, user: discord.User, *, message=None):
            message = message or "DM message to be sent"
            try:
                await user.send(message)
            except: 
                await ctx.send("Failed to send Dm")