如何获取discord.py中嵌入的字段的数量?

时间:2020-10-16 07:19:07

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

我正在尝试为我的机器人发出帮助命令(有点花哨) 因此,我为命令设置了不同的类别!像这样-

    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```

1 个答案:

答案 0 :(得分:0)

在回溯中说明发生错误的原因-您正在尝试遍历一个函数。

因此,无论您在调用fields()的哪个位置,都意外地传递了函数而不是可迭代对象(例如list)。