Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To send a direct message to a user in Discord using a slash command in discord.py, you can use the following code:

@client.slash_command(description="Send a direct message to a user")
async def dm(ctx, user: discord.User, *, message: str):
    try:
        await user.send(message)
        await ctx.send(f"Message sent to {user.name}")
    except:
        await ctx.send("Failed to send message")

This code creates a slash command called "dm" that takes a user and a message as arguments. It then attempts to send the message to the specified user, and if successful, it sends a confirmation message back to the user who executed the slash command. If the message fails to send, it sends an error message instead.

To use this command, the user would need to use the following syntax:

/dm @username message

Replace @username with the username of the user you want to send the message to, and message with the message you want to send.