Ask Your Question
1

How to send a direct message to a user in Discord using a slash command in discord.py?

asked 2022-06-09 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-01-14 22:00:00 +0000

david gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-06-09 11:00:00 +0000

Seen: 8 times

Last updated: Jan 14 '23