【问题标题】:What other objects can I get from the context of a message我可以从消息的上下文中获得哪些其他对象
【发布时间】:2020-08-27 20:51:15
【问题描述】:

我对不和谐编程唯一感到困惑的部分是,什么上下文?当您从消息中获取上下文时,上下文可以实际访问什么。在我的一个齿轮中,我有这个命令test

@commands.command()
async def test(self, ctx):
    await ctx.send("Hello world!")

ctx 中有什么内容?例如,如果我想访问Message 对象,我会通过ctx.message 得到它。我通过谷歌搜索知道这一点。上下文中还有哪些其他内容?有某种UML图吗?我想知道如何获取guild 对象或member 对象。文档中几乎列出了所有 Discord 模型。

【问题讨论】:

    标签: python python-3.x discord discord.py discord.py-rewrite


    【解决方案1】:

    添加到 Lu M 的答案,作为获取对象属性的“笨拙”方法,您始终可以运行:

    print(dir(ctx)) # or replace ctx with whatever object's attributes you are trying to find
    
    # equivalent:
    
    print(dir(type(ctx))) # once again, replacing ctx with any other object you might try to find
    

    这将返回对象对您可用的每个属性的列表。
    但是,最好使用可用的文档。将此用作“最后的手段”或“懒惰的方法”。

    【讨论】:

    • 非常好的答案!我没有考虑过使用类的 dir() 。
    【解决方案2】:

    这正是documentation 的用途。

    【讨论】:

    • 如何从上下文中获取member 对象?因为我看不到?我想向发送消息的人添加一个。我看到的只是author,但那是User的类型
    • @ZoeyMalkov 服务器中消息的作者自动成为成员对象:discordpy.readthedocs.io/en/latest/…
    猜你喜欢
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 2017-04-13
    相关资源
    最近更新 更多