【问题标题】:Embed in cogs (discord.py)嵌入 cogs (discord.py)
【发布时间】:2021-09-15 12:41:00
【问题描述】:

我尝试在我的 cogs 文件上实现嵌入消息。当我尝试使用该命令时,我的控制台上会弹出此错误。

An exception has occurred while executing command `cal`:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord_slash/client.py", line 1352, in invoke_command
    await func.invoke(ctx, **args)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord_slash/model.py", line 209, in invoke
    return await self.func(self.cog, *args, **kwargs)
  File "/home/runner/calculator/cogs/update.py", line 16, in cal
    self.username = ctx.message.author.display_name
AttributeError: 'NoneType' object has no attribute 'author'

错误指出作者没有返回(根据我的理解)。这是我的代码 sn-p :

@ cog_ext.cog_slash(description = "Calculate your math's query.")
  async def cal(self,ctx,query : str) :
    self.operation = ("").join(query)
    self.evaluate = eval(self.operation)
    self.username = ctx.message.author.display_name
    self.embed = discord.Embed(title = "/Cal", description = "Calculate your math queries.", colour = discord.Color.blurple())
    self.embed.set_footer(text = f"{self.username}'s query evaluated.")
    self.embed.set_author(name = f"{self.username}'s' query.", icon_url = ctx.message.author.avatar_url)
    self.embed.add_field(value = f"{self.operation}", inline = False)
    self.embed.add_field(value = "After evaluated, the result is :", inline = True)
    self.embed.add_field(value = f"{self.evaluate}", inline = True)
    await ctx.send(embed = self.embed)

根据我的直觉,我认为在 cogs 文件中,作者使用不同的关键字表示。也许我的代码有问题。提前致谢。

【问题讨论】:

  • 斜杠命令使用什么库?
  • discord_slash 或 discord-py-slash-command
  • 嗯,错误说messageNone,而不是.author,所以你的直觉是错误的。我对这个库一无所知,所以我无法指出实际问题来自哪里。

标签: python discord.py


【解决方案1】:

这里的问题是,discord-py-slash-commands(或重命名为 discord-interactions)返回一个 SlashContext 对象,而不是 discord.py 使用的常规上下文对象。并且,由于没有使用消息调用斜杠命令,因此消息对象是 None,由错误确认。

要解决此问题,您应该能够将ctx.message.author 替换为ctx.author,这应该可以工作,as stated in their documentation

【讨论】:

    【解决方案2】:

    我不知道您正在使用什么库或它是如何工作的,因为它看起来不像 discord.py,但您是否尝试过使用 ctx.author.display_name 而不是 ctx.message.author.display_name

    【讨论】:

    • 是的,我试过了,但还是不行。但是感谢您的帮助。
    猜你喜欢
    • 2019-04-30
    • 2021-04-19
    • 2021-04-29
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    相关资源
    最近更新 更多