discord,py:AttributeError:“上下文”对象没有属性“引用”

2024-06-28 20:58:45 发布

您现在位置:Python中文网/ 问答频道 /正文

(插入问候语)

我想让discord机器人回复命令消息中回复的消息,以下是迄今为止的代码:

@bot.command(brief="Iri? Bilang Bos!", description="Iri? Bilang Bos!")
async def iri(ctx):
    ref = ctx.reference
    x=['https://i.imgur.com/8mfw6Nv.jpg', 'https://tenor.com/view/iri-bilang-bos-spell-power-up-skill-gif-17176211', 'https://i.imgur.com/hOvruLZ.jpg']
    await ctx.delete()
    await ctx.send(random.choice(x), reference=ref)

这会引发异常AttributeError:“上下文”对象没有属性“引用”

我如何解决这个问题?谢谢


Tags: httpscomref消息awaitjpgreferencectx
1条回答
网友
1楼 · 发布于 2024-06-28 20:58:45

您的想法是正确的,但没有充分研究文档。通常,如果您不知道需要使用什么属性,那么在documentation中搜索会很有帮助

  1. ^{}没有属性reference。这是因为^{}是我们的^{}的一个属性。幸运的是,上下文(ctx)有一个message属性:^{}。我们使用它来获取message对象,然后使用它来获取reference属性:ctx.message.reference
  2. delete方法也是如此Context对象没有delete方法,因此我们首先需要获得message对象:ctx.message,然后使用message对象:await ctx.message.delete()^{}方法(我们必须使用wait,因为delete方法是异步的)

注意:给变量起一个有意义的名字通常是很好的做法。它提高了代码的可读性。这就是为什么我将x变量更改为choices

所以最终结果应该是这样的:

@bot.command(brief="Iri? Bilang Bos!", description="Iri? Bilang Bos!")
async def iri(ctx):
    ref = ctx.message.reference
    choices = ['https://i.imgur.com/8mfw6Nv.jpg', 'https://tenor.com/view/iri-bilang-bos-spell-power-up-skill-gif-17176211', 'https://i.imgur.com/hOvruLZ.jpg']
    await ctx.message.delete()
    await ctx.send(random.choice(choices), reference = ref)

希望这能回答你的问题:)

相关问题 更多 >