【问题标题】:Discord.py: Embed author returning embed.Empty on editDiscord.py:嵌入作者在编辑时返回 embed.Empty
【发布时间】:2021-04-01 13:04:40
【问题描述】:

在处理我的编辑嵌入命令时,我遇到了一个问题。除作者外,一切都按预期工作。在编辑嵌入的作者时,如果在原版中它是空的,则在编辑后机器人会用“embed.Empty”填充它,而不是让它空着。但是,如果在原作者中有一个值,则机器人不会像预期的那样更改它。我附上了一张图片,所以你也可以明白我的意思。

  @embed.command()
  @commands.has_permissions(manage_permissions = True) 
  @commands.bot_has_permissions(manage_permissions = True)
  async def edit(self, ctx, msgID: discord.Message):
    await ctx.send('What part of the embed would you like to edit? The `title`, `body`, `footer`, `color`, `image`, `thumbnail`, `author name`, or `author icon`?')

    og_embed = msgID.embeds[0]
    og_embed_footer = msgID.embeds[0].footer
    og_embed_image = msgID.embeds[0].image
    og_embed_thumbnail = msgID.embeds[0].thumbnail
    og_embed_author = msgID.embeds[0].author

    def check(m):
      return m.channel == ctx.channel and m.author == ctx.author

    while True:
        try:
            msg = await self.bot.wait_for('message', check=check, timeout=30.0)

            if str(msg.content) == 'title':
              await ctx.send('What should the new title of the embed be?')
              new_title_input = await self.bot.wait_for('message', check=check, timeout=30.0)
              new_embed=discord.Embed(title=new_title_input.content, description=og_embed.description, color=og_embed.color)
              new_embed.set_footer(text=og_embed_footer.name)
              new_embed.set_image(url=og_embed_image.url)
              new_embed.set_thumbnail(url=og_embed_thumbnail.url)
              new_embed.set_author(name=og_embed_author.name, icon_url=og_embed_author.icon_url)
              await msgID.edit(content=None, embed=new_embed)

            elif str(msg.content) == 'body':
              await ctx.send('You chose to edit the body.')

        except asyncio.TimeoutError:
            return

我不知道为什么会发生这种情况,并且没有出现错误。

【问题讨论】:

  • 为什么不直接使用 if 语句来检查作者字段是否为空?
  • 我尝试使用if og_embed_author != None or 'Embed.Empty':,但仍然遇到同样的问题。似乎现在页脚也发生了这种情况,但方式相反。
  • 添加到@NuKeFluffy 评论使用\u200b 作为新值,它将只是一个空行。
  • 你的 if 语句应该是这样的:if (og_embed_author.name != discord.Embed.Empty):
  • 您将 'Embed.Empty' 放在一个字符串中,但它是一个不和谐的嵌入对象,因此将其更改为 discord.Embed.Empty 即可

标签: python discord discord.py embed discord.py-rewrite


【解决方案1】:

感谢 Abdulaziz 和 NuKeFluffy 的帮助,我先放入了一些 if 语句来检查该字段是否为空,如果是则保持原样(:这是新代码:

  @embed.command()
  @commands.has_permissions(manage_permissions = True) 
  @commands.bot_has_permissions(manage_permissions = True)
  async def edit(self, ctx, msgID: discord.Message):
    await ctx.send('What part of the embed would you like to edit? The `title`, `body`, `footer`, `color`, `image`, `thumbnail`, `author name`, or `author icon`?')

    og_embed = msgID.embeds[0]
    og_embed_footer = msgID.embeds[0].footer
    og_embed_image = msgID.embeds[0].image
    og_embed_thumbnail = msgID.embeds[0].thumbnail
    og_embed_author = msgID.embeds[0].author

    def check(m):
      return m.channel == ctx.channel and m.author == ctx.author

    while True:
        try:
            msg = await self.bot.wait_for('message', check=check, timeout=30.0)

            if str(msg.content) == 'title':
              await ctx.send('What should the new title of the embed be?')
              new_title_input = await self.bot.wait_for('message', check=check, timeout=30.0)

              # The one being changed here
              new_embed=discord.Embed(title=new_title_input.content, description=og_embed.description, color=og_embed.color)
              
              if (og_embed_footer.text != discord.Embed.Empty):
                new_embed.set_footer(text=og_embed_footer.text)

              new_embed.set_image(url=og_embed_image.url)

              new_embed.set_thumbnail(url=og_embed_thumbnail.url)

              if (og_embed_author.name != discord.Embed.Empty):
                new_embed.set_author(name=og_embed_author.name, icon_url=og_embed_author.icon_url)

              await msgID.edit(content=None, embed=new_embed)
              return

            elif str(msg.content) == 'body':
              await ctx.send('You chose to edit the body.')

        except asyncio.TimeoutError:
            return

【讨论】:

    猜你喜欢
    • 2020-03-17
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 2021-04-20
    • 2021-07-31
    • 2023-01-26
    • 1970-01-01
    相关资源
    最近更新 更多