Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不和谐,py:AttributeError:';上下文';对象没有属性';参考';_Python_Python 3.x_Discord.py - Fatal编程技术网

Python 不和谐,py:AttributeError:';上下文';对象没有属性';参考';

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

(插入问候语)

我想让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:“上下文”对象没有属性“引用”


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

您的想法是正确的,但对文档的了解不够。通常,如果您不知道需要使用什么属性,则可以在中搜索

  • 没有属性
    引用
    。这是因为这是我们的一个特点。幸运的是,上下文(
    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,但这不起作用,因此我在这里询问。我也很感激这张纸条,有点让我想起我有多懒呵呵。干杯