【问题标题】:discord.py KeyError with warnings command and mongodb带有警告命令和 mongodb 的 discord.py KeyError
【发布时间】:2022-12-10 09:29:08
【问题描述】:

每当我尝试使用此命令时,我都会不断收到此错误,并且我一直在测试它,但尽管查看了其他问题,我仍然无法弄清楚问题是什么。

Traceback (most recent call last):
  File "/home/container/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 190, in wrapped
    ret = await coro(*args, **kwargs)
  File "/home/container/main.py", line 398, in warnings
    embed.add_field(name=f"{warn['reason']}", value=f"<@{warn['moderator_id']}>", inline=False)
TypeError: string indices must be integers
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/home/container/.local/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 1347, in invoke
    await ctx.command.invoke(ctx)
  File "/home/container/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 986, in invoke
    await injected(*ctx.args, **ctx.kwargs)  # type: ignore
  File "/home/container/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 199, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: string indices must be integers

这是我的代码

@client.hybrid_command(name = "warnings", with_app_command=True, description="View the warnings of a member", aliases=["punishments"])
@commands.is_owner()
async def warnings(ctx, member: discord.Member = None):
    if member == None:
        await ctx.reply("A Member is required")
    else:
        check = warndb.warn_logs.find_one({"user_id": member.id})
        if check is None:
            await ctx.reply("This user has no warns")
        else:
            embed = discord.Embed(color=embedcolor, title=f"{member.name}'s warnings")
            print(check)
            for warn in check:
                embed.add_field(name=f"{warn['reason']}", value=f"<@{warn['moderator_id']}>", inline=False)
            await ctx.send(embed=embed)

如果有人知道请帮助我我已经处理这个问题一段时间了

【问题讨论】:

  • 不是真的,我仍然很困惑
  • 您是否尝试过打印 warn 的样子?因为它是一个字符串而不是你认为的任何东西

标签: python discord discord.py


【解决方案1】:

(声望值不够评论)

但错误是说无论你试图索引,看起来它在这里警告,你可能期待一个字典,你会用“原因”索引但警告是一个字符串,我建议你尝试打印警告并看看是什么您实际上是在尝试索引

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 2021-07-28
    • 2020-03-31
    • 2021-10-20
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多