繁体   English   中英

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

[英]How to get the amount of field in a embed in discord.py?

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

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

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

因此,无论您在何处调用fields()都会不小心传递一个函数而不是可迭代对象(如list )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM