【发布时间】:2021-03-18 07:56:24
【问题描述】:
所以,我创建了将用户输入存储到 json 文件中的命令,代码如下:
for user in client.users:
with open('suggestions.json', 'r') as f:
h = json.load(f)
h[str(f'{user.id} | {user.name} ')] = message.content
with open('suggestions.json', 'w') as f:
json.dump(h, f, indent=4)
问题在于,当两个或多个不同的用户使用该命令时,他们的所有输入都会在最后一个输入中发生变化,例如:
{
"ID| USER NAME ": "ol",
"ID | USER NAME ": "ol",
"ID | USER NAME ": "ol"
}
由于某种原因,还存储了用户输入的机器人名称和 ID,以及在那一秒输入的所有成员或机器人,其值与发出命令的人相同,我尝试了很多东西,我真的不知道,也许只是使用数据库,但 atm 我对此一无所知。
为了更好地查看,我的命令此时看起来像这样:
@client.command()
async def suggestion(ctx):
await ctx.send("You want to submit a suggestion?")
message = await client.wait_for ('message' ,timeout=25, check = lambda m: m.author == ctx.author and m.channel == ctx.channel)
if message.content == "yes" :
await ctx.send("Good choice, now let's hear your suggestion : ")
message = await client.wait_for('message', timeout=25, check=lambda m: m.author == ctx.author and m.channel == ctx.channel)
await ctx.send("Alirght, i will send this suggestion to the developer, thanks ! ")
for user in client.users:
with open('suggestions.json', 'r') as f:
h = json.load(f)
h[str(f'{user.id} | {user.name} ')] = message.content
with open('suggestions.json', 'w') as f:
json.dump(h, f, indent=4)
elif message.content == "no":
await ctx.send("Too bad, please use this command just if you have any ideas.")
【问题讨论】:
标签: python json python-3.x bots discord.py