【问题标题】:How to get the amount of field in a embed in discord.py?如何在 discord.py 中获得嵌入的字段数量?
【发布时间】:2020-10-16 15:24:36
【问题描述】:

我正在尝试为我的机器人创建一个帮助命令(有点花哨) 所以为此我为命令做了不同的分类!像这样-

    embed = discord.Embed()
    embed.title = ":computer: **Help Centre: Meta!**"
    embed.description=":pen_ballpoint: Type `am:[command]` to use a command!"
    embed.add_field(name="am:hi",value="To say hi to me!")
    embed.add_field(name="am:report [issue]",value="To report a bug/issue about the bot!")
    embed.add_field(name='am:feedback [feedback\suggestion]',value="To sent a feedback or suggestion to my cretor!")
    embed.colour = discord.Colour.green()
    embed.set_footer(text="Among Us help centre!")
    await ctx.send(embed=embed)


async def wikipe(ctx):
    embed = discord.Embed()
    embed.title = ":computer: **Help Centre: Wiki!**"
    embed.description=":pen_ballpoint: Type `am:[command]` to use a command!"
    embed.add_field(name="am:wiki [query]",value="To know more about among us game!")
    embed.add_field(name="am:find [query]",value="To find anything other than Among Us!")
    embed.colour = discord.Colour.green()
    embed.set_footer(text="Among Us help centre!")
    await ctx.send(embed=embed)

async def tools(ctx):
    embed = discord.Embed()
    embed.title = ":computer: **Help Centre: Tools!**"
    embed.description=":pen_ballpoint: Type `am:[command]` to use a command!"
    embed.add_field(name="am:dm [@user]\n[message]",value="To send an instant message to anyone from the server!")
    embed.colour = discord.Colour.green()
    embed.set_footer(text="Among Us help centre!")
    await ctx.send(embed=embed) 

所以现在我需要每个类别中的字段数(即每个类别中的命令数)! 我做了一个函数。像这样-

def fields(cate):
    i=0
    for emb in cate:
        for fields in emb.fields:
            i+=1

但是我遇到了一个错误,你们中的任何人都可以帮助我吗? 错误是 -

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 93, in help
    embed.add_field(name="Meta",value = str(fields(meta)))
  File "main.py", line 81, in fields
    for emb in cate:
TypeError: 'function' object is not iterable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'function' object is not iterable```

【问题讨论】:

  • 请帮助任何人

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


【解决方案1】:

回溯中说明了错误的原因 - 您正在尝试迭代函数。

所以无论你在哪里调用fields(),你都会意外地传递一个函数而不是一个可迭代对象(比如list)。

【讨论】:

  • 那么如何获取字段数呢?
猜你喜欢
  • 2021-11-09
  • 1970-01-01
  • 2021-03-04
  • 2019-12-04
  • 2023-01-08
  • 2021-08-29
  • 1970-01-01
  • 2021-02-22
  • 2020-12-26
相关资源
最近更新 更多