python 我如何回复提到pycord

ryevplcw  于 11个月前  发布在  Python
关注(0)|答案(2)|浏览(102)

我在运行一个不和谐的机器人。但它的React没有提到。
在discord.py中,我可以这样做:

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

字符串
它会提到用户并回复他们的消息。但是自从discord.py被删除后,我改用了pycord。我只是不知道如何在pycord中做同样的事情。
下面是一个简单的echo bot:

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)


我想要实现的结果,但使用斜杠命令

e4eetjau

e4eetjau1#

如果你想回复一条消息,你可以使用response.send_message(),而不是respond。

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

字符串

ar5n3qh5

ar5n3qh52#

提到用户的一种方式是:

@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

字符串
它从.mention返回的字符串中提到用户。
因为您使用的是斜杠命令(在示例输出中,它是一个文本命令),所以机器人在没有消息对象的情况下无法提及用户(不使用上述方法)。
获取该消息对象的一种方法是:

ctx.message


您也可以回复此邮件。
文件

相关问题