How do I reply with mention pycord

127 Views Asked by At

I am running a discord bot. But it responds without mentioning.

In discord.py I could do something like this:

@bot.event
async def on_message(message):
    if message.content == "hi":
        await message.reply("Hello!", mention_author=True)

And it would mention the user and reply to their message. But since discord.py was depricated, I switched to pycord. And I just can't find how I can do the same thing in pycord.

Here is simple echo bot to replicate:

import discord

bot = discord.Bot()


@bot.event
async def on_ready():
    print(f"{bot.user} is ready and online!")


@bot.slash_command(name="chat", description="some desc")
async def chat(ctx, msg):
    await ctx.respond(msg)

bot.run(TOKEN)

The result I want to achieve, but with slash command enter image description here

2

There are 2 best solutions below

1
On BEST ANSWER

One way to mention the user would be:

@bot.slash_command(name="chat", description="some desc")
async def chat(ctx, msg):
    await ctx.response.send_message(f"{ctx.user.mention} {msg}")  
    # f string with user mention

It mentions the user from the string returned from .mention.

Because you're using slash commands (and in the example output, it's a text command), the bot can't mention the user (without using the above method) without a message object.

One way to get that message object would be:

ctx.message

You can reply to this message instead as well.

Documentation

1
On

If you want to reply to a message you would use response.send_message(), not respond

@bot.slash_command(name="chat", description="some desc")
async def chat(ctx, msg):
    await ctx.response.send_message(msg)