简体   繁体   English

如何从 TinyDB 中的一组 3 个变量中仅发送 1 个变量

[英]How to send only 1 variable from a set of 3 in TinyDB

[DISCORD.PY and TINYDB] [DISCORD.PY 和 TINYDB]

I have set up a warning system for Discord.我已经为 Discord 建立了一个警告系统。 If someone gets warned, the data is saved like so:如果有人收到警告,数据将按如下方式保存:

{'userid': 264452325945376768, 'warnid': 37996302, 'reason': "some reason"}

Problem of this: I want the command ",warnings" to display these warnings, but I don't want it to have this ugly formatting of a JSON: instead I want it to display like so:问题:我希望命令“,警告”显示这些警告,但我不希望它具有 JSON 的这种丑陋格式:相反,我希望它显示如下:

{member} has {amount} warnings.
WARN-ID: {warning id here}
REASON: {reason here}

To do this, I need to somehow call only one variable at a time instead of having all 3 (as of above JSON) instantly.为此,我需要以某种方式一次只调用一个变量,而不是立即调用所有 3 个(如 JSON 上)。

My code is as follows:我的代码如下:

@Ralf.command()
async def warnings(ctx, *, member: discord.Member=None):
    if member is None:
        member = Ralf.get_user(ctx.author.id)
        member_id = ctx.author.id
    else:
        member = Ralf.get_user(member.id)
        member_id = member.id
    WarnList = Query()
    Result = warndb.search(WarnList.userid == member_id)
    warnAmt = len(Result)
    if warnAmt == 1:
        await ctx.send(f"**{member}** has `{warnAmt}` warning.")
    else:
        await ctx.send(f"**{member}** has `{warnAmt}` warnings.")
        for item in Result:
            await ctx.send(item)

This code is working, but it shows the ugly {'userid': 264452325945376768, 'warnid': 37996302, 'reason': "some reason"} as output.此代码有效,但它显示丑陋的{'userid': 264452325945376768, 'warnid': 37996302, 'reason': "some reason"}为 output。

MAIN QUESTION: How do I call only userid without having warnid and reason displayed?主要问题:如何在不显示警告和原因的情况下仅调用用户 ID?

EDIT 1: Trying to use dicts results in following:编辑 1:尝试使用 dicts 会导致以下结果:

For that I get the following:为此,我得到以下信息:

Ignoring exception in command warnings:
Traceback (most recent call last):
  File "C:\Users\entity2k3\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:\Users\entity2k3\Desktop\Discord Bots All\Entity2k3's Moderation\main.py", line 201, in warnings
    await ctx.send(f"WARN-ID: `{Result['warnid']}` REASON: {Result['reason']}")
TypeError: list indices must be integers or slices, not str

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

Traceback (most recent call last):
  File "C:\Users\entity2k3\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\entity2k3\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\entity2k3\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: list indices must be integers or slices, not str

You are getting the TypeError because your Result is a list of dictionaries.您收到TypeError是因为您的Result是一个字典列表。
Make sure to iterate through Result and process each dictionary separately.确保遍历Result并分别处理每个字典。
Your Result object is like this [{}, {}, {}...]您的Result object 是这样的[{}, {}, {}...]

Also you shouldn't capitalize the first letter of your variable.此外,您不应将变量的第一个字母大写。 You should name it results , because it may contain more than 1 result.您应该将其命名为results ,因为它可能包含超过 1 个结果。

for item in results:
    user_id = item.get("userid")
    warn_id = item.get("warnid")
    reason = item.get("reason")
    # do stuff with those

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

相关问题 如何在python中的tinydb查询中包含变量? - how to include a variable in tinydb query in python? 如何将联系人从MIT App Inventor导入数据库(tinydb或firebase)? - How do I import contacts from MIT App Inventor into a database (tinydb or firebase)? 从 android studio app 保存数据到 tinydb - Save data to tinydb from android studio app 如何将变量数据从 javascript 发送到节点 js? - how to send variable data from javascript to node js? 如何在反应中从另一个文件设置状态变量 - How to set state a variable from another file in react 如何更改只读权限以设置MySQL服务器系统变量的新值 - How to change read-only permission to set new value of MySQL server system variable 如何将 select 语句中的聚合值设置为 MySQL 自定义 function 中的变量 - How can I set an aggregate value from a select statement to a variable inside MySQL custom function 如何在 Hibernate 中设置只读列? - How to set read-only columns in Hibernate? 如何指定一个表的字段只能具有一组特定的值? - How can I specify that a field of a table can have a value take only from a specific set of values? 如何使用变量动态设置小数精度 - How to set decimal precision dynamically using a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM