Python 不和谐,py:AttributeError:';上下文';对象没有属性';参考';
(插入问候语) 我想让discord机器人回复命令消息中回复的消息,以下是迄今为止的代码:Python 不和谐,py:AttributeError:';上下文';对象没有属性';参考';,python,python-3.x,discord.py,Python,Python 3.x,Discord.py,(插入问候语) 我想让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
@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:“上下文”对象没有属性“引用”
我如何解决这个问题?谢谢。您的想法是正确的,但对文档的了解不够。通常,如果您不知道需要使用什么属性,则可以在中搜索
引用
。这是因为这是我们的一个特点。幸运的是,上下文(ctx
)有一个消息
属性:。我们使用它来获取消息
对象,然后使用该对象来获取引用
属性:ctx.message.reference
delete
方法也是如此Context
对象没有delete
方法,因此我们首先需要获取message
对象:ctx.message
,然后使用message
对象的方法:await ctx.message.delete()
(我们必须使用await,因为delete
方法是异步的)x
变量更改为choices
所以最终结果应该是这样的:
@bot.command(brief=“Iri?Bilang Bos!”,description=“Iri?Bilang Bos!”)
异步def iri(ctx):
ref=ctx.message.reference
选项=['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']
等待ctx.message.delete()
等待ctx.send(random.choice(选项),reference=ref)
希望这能回答您的问题:)我还需要知道如何删除命令消息,代码引发了异常:AttributeError:“Context”对象没有属性“delete”可能首先检查文档-或者使用
print(dir(ctx))
查看对象ctx
中的所有可用属性和函数谢谢,效果很好。实际上,我检查了文档,在ctx中找到了message对象,然后只尝试了message.reference和message.delete,但这不起作用,因此我在这里询问。我也很感激这张纸条,有点让我想起我有多懒呵呵。干杯