我希望我的机器人能够在用户输入特定的句子时对其消息做出反应。
用于回复的代码:
await ctx.message.reply("I just replied to you")
我遇到了错误:
ctx.message has no attribute "reply"
我应该使用哪些代码让机器人回复消息?
当我说“回复”时,我的意思是与用户按下消息中的“回复”按钮相同。
对于任何新用户,从1.6.0 discord.py-rewrite版本开始,您现在可以回复消息了!
每条消息或上下文中现在都有一个回复属性。要进行回复,只需使用:
await ctx.reply('Hello!')
你还可以使用mention_author=False
来在回复中不提及作者。
await ctx.reply('Hello!', mention_author=False)
你也可以在这里找到一个基本的例子。
目前 Discord.py 还不支持新的“回复”功能。很遗憾,除非他们将其添加到库中,否则你无法真正使用回复功能。
一种选择是使用 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
如果您有任何进一步的问题,或者遇到我没有发现的错误,请随时与我联系 :)
await ctx.send('I just replied to you')
AttributeError: 'Context' object has no attribute 'reply'
,有人可以帮忙吗? - Nicholas Chen