Takaso
Takaso

Reputation: 39

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)

Upvotes: 0

Views: 651

Answers (1)

Axisnix
Axisnix

Reputation: 2907

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")

Upvotes: 0

Related Questions