回复discord.py中的消息

13

我希望我的机器人能够在用户输入特定的句子时对其消息做出反应。

用于回复的代码:

await ctx.message.reply("I just replied to you")

我遇到了错误:

ctx.message has no attribute "reply"

我应该使用哪些代码让机器人回复消息?

当我说“回复”时,我的意思是与用户按下消息中的“回复”按钮相同。

5个回答

30

对于任何新用户,从1.6.0 discord.py-rewrite版本开始,您现在可以回复消息了!

每条消息或上下文中现在都有一个回复属性。要进行回复,只需使用:

await ctx.reply('Hello!')

你还可以使用mention_author=False来在回复中不提及作者。

await ctx.reply('Hello!', mention_author=False)

无回复示例 回复示例

你也可以在这里找到一个基本的例子。


我收到一个错误,上面写着AttributeError: 'Context' object has no attribute 'reply',有人可以帮忙吗? - Nicholas Chen
1
@NicholasChen 你需要升级到 discord.py 1.6。 - Frederick Reynolds
谢谢,我会这样做。 - Nicholas Chen

4
@bot.event
async def on_message(message):
    if message.content == "hi":
        await message.channel.send("hello", reference=message)

这里的重点是 reference=message。它自 Discord.py 1.6 版本起可用。

更多信息: 1, 2.


-1

目前 Discord.py 还不支持新的“回复”功能。很遗憾,除非他们将其添加到库中,否则你无法真正使用回复功能。


这已经过时了。 - Blue Robin

-2

一种选择是使用 Cog.listener,你可以在这里找到Cog.listener的文档,但是回答你的问题,我实现Cog.listener的方式是:

@bot.listen('on_message') 
async def stuff(message):

    if message.content.startswith("buttlerprefix"): # this tells the bot what to listen for. If a user types `buttlerprefix` in any text channel, it will respond with what's below
        msg = await message.channel.send("my prefix is `>`") # set the sending message equal to a variable so that you can manipulate it later like I did with the timer, and delete function below
        await asyncio.sleep(10) # tells the bot to wait 10 seconds before continuing below
        await msg.delete() # deletes the send message after 10 seconds

如果您有任何进一步的问题,或者遇到我没有发现的错误,请随时与我联系 :)


嗨,当我说“回复”时,我的意思是用户右键点击并按下回复。 - user13524876
啊,我明白了。非常抱歉,但我不确定该怎么做:( 我会继续关注这个问题,如果有人找到了解决方法,那么我也可以学到新的东西:) - shellbyy

-4
尝试用以下内容替换这行代码
await ctx.send('I just replied to you')

我知道可以使用那个方法发送消息,但我想要特别回复该消息,就像右键单击消息并按回复一样。 - user13524876
你使用的discord.py版本是什么?文档中说它是在1.6版本中添加的。 - Zeref
我不知道这是否与我使用PyCharm有关,但根据它的说法,discord.py的最新版本是1.5.1,所以也许这是尚未实现的东西。 - Zeref

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接