[英]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.