I’m working on a Discord bot project using Python and I need help with a specific feature. When someone uses one of my bot commands, I want the bot to mention that person in its response message. So if a user types a command, the bot should reply with something that includes an @ mention of whoever sent the original command.
I’ve been trying different approaches but can’t seem to get the mention functionality working properly. The bot responds to commands fine, but I’m struggling with how to reference and ping the specific user who triggered the command.
What’s the correct way to capture the command sender and include them as a mention in the bot’s reply? Any code examples would be really helpful since I’m still learning the discord.py library.
If the mention isn’t working, check you’re using the right context parameter. I’ve seen people mix up message.author and ctx.author when combining message events with command handlers. ctx.author.mention is cleaner and more readable than ctx.author, especially for complex response strings. The mention shows up blue in Discord and pings the user - exactly what you want for command responses.
To mention the user who invoked the command, you can use ctx.author.mention. For example:
@bot.command()
async def hello(ctx):
await ctx.send(f"Hello {ctx.author.mention}! How can I assist you?")
This will create a mention by formatting the user’s ID as <@userid>, making it effectively highlight the user in the message. If you only wish to display their username without the mention, consider using ctx.author.display_name. This approach generally works well in various contexts.
yea, miar’s right but u can also just use ctx.author - it auto pings them. like await ctx.send(f'{ctx.author} ur response here') works perfectly. both methods do the job, just pick whichever u prefer.