使用 ctx.guild.id 在 Discord.py cog 中不起作用。我该怎么办?

问题描述 投票:0回答:2

我有一个discord.py 机器人,我刚刚扩展到了cogs,但现在当我运行需要访问

ctx.guild.id
的命令时,但假设我正在尝试获取
cogname.guild.id
,我会收到错误。我应该改变什么来解决这个问题?

我尝试过:

Class cogname(commands.cog):
  @commands.hybrid_command(name=“testcommand”)
  async def testcommand(ctx):
    ctx.send(data[ctx.guild.id])

我期望发送公会的 json 数据,但我收到错误消息,指出类

mycog
没有属性
guild
。我该怎么办?

我正在使用一切的最新版本。

python discord discord.py
2个回答
2
投票

testcommand
是类中的实例方法。在每个实例方法中,都会传递一个引用类本身实例的参数。理想情况下,将其命名为“自我”

在您的例子中,它的名称为“ctx”。这就是为什么它引用类的实例并给出该错误。确保在函数中传递任何其他内容之前传递 self。

@commands.hybrid_command(name=“testcommand”)
async def testcommand(self, ctx):
    await ctx.send(data[ctx.guild.id])

您可以在此处阅读有关实例方法的更多信息

另外,应该等待 ctx.send!


0
投票

@commands.command() 异步 def p(self, ctx): wait ctx.send(ctx.author.id) #在 ctx.author 中没有得到任何有关 ctx.author 的智能感知或 ctx 下的任何属性,在调用命令后也显示 ctx 没有属性author.id 请提供任何解决方案

© www.soinside.com 2019 - 2024. All rights reserved.