简体   繁体   English

discord.py 警告系统检查警告

[英]discord.py warning system checking warnings

So I'm still making a warning system but in the process I am having this issue with the embed showing the warning multiple times.所以我仍在制作一个警告系统,但在此过程中我遇到了多次显示警告的嵌入问题。 Shown in the image provided.如提供的图片所示。 https://i.stack.imgur.com/ks4Gm.png I'm not sure what could be causing this but I think it might be the for loop I made? https://i.stack.imgur.com/ks4Gm.png我不确定是什么原因造成的,但我认为这可能是我制作的 for 循环?

@client.hybrid_command(name = "warnings", with_app_command=True, description="View the warnings of a member", aliases=["punishments"])
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:
            reason = check["reason"]
            moderator_id = check["moderator_id"]
            embed = discord.Embed(color=embedcolor, title=f"{member.name}'s warnings")
            for w in check:
                embed.add_field(name=f"{reason}", value=f"<@{moderator_id}>", inline=False)
            await ctx.send(embed=embed)

There is no error and it works fine it just shows the warning(s) multiple times没有错误,它工作正常,只是多次显示警告

You query with find_one so there is no point in looping over check .您使用find_one查询,因此没有必要循环check

I would try to just remove the line for w in check: .我会尝试for w in check:行。

If it's suppose to be an array(change to find_all), maybe you manually adding data to the table when testing and it's displaying all previous warns you've added.如果它假设是一个数组(更改为 find_all),也许您在测试时手动将数据添加到表中并且它显示了您之前添加的所有警告。

I figured it out finally.我终于想通了。 The issue is you need to turn the check = warndb.warn_logs.find_one({"user_id": member.id}) into a list by doing check = list(warndb.warn_logs.find_one({"user_id": member.id})) .问题是您需要通过 check = check = list(warndb.warn_logs.find_one({"user_id": member.id})) (warndb.warn_logs.find_one({"user_id": member.id}) 将check = warndb.warn_logs.find_one({"user_id": member.id})变成一个列表check = list(warndb.warn_logs.find_one({"user_id": member.id})) Once you do that you should use the .get() function to get the values of the keys一旦你这样做了,你应该使用.get() function 来获取键的值

In this case:在这种情况下:

for w in check:
    reason = w.get('reason')
    moderatorid = w.get('moderator_id')
    guildid = w.get('guild_id')
    caseid = w.get('_id')
    embed.add_field(name=f"Case ID: {caseid}—Reason: {reason}", value=f"<@{moderatorid}>", inline=False)

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

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