好的,这就是我设置 JSON 文件的方法:
async def openinf():
with open("infractions.json", "r") as f:
users = json.load(f)
return users
async def createinfraction(user, type, mod, date, caseid):
users = await openinf()
users[str(user.id)] = {}
users[str(user.id)]["type"] = type
users[str(user.id)]["mod"] = mod.id
users[str(user.id)]["date"] = date
users[str(user.id)]["caseid"] = caseid
with open("infractions.json", "w") as f:
json.dump(users, f)
print("done function")
return True
这将在 json 文件中打印: {"581457749724889102": {"type": "Warning", "mod": 581457749724889102, "date": "04/12/2021 18:52:28", "caseid": "f59427df-552a-11ec-8dfd-e03f49c55de5"}}
现在,我尝试这样做:
try:
users = await openinf()
type = users[str(member.id)]["type"]
modid = users[str(member.id)]["mod"]
date = users[str(member.id)]["date"]
caseid = users[str(member.id)]["caseid"]
embed = discord.Embed(title = f"Infractions for {member}", colour = 0x0f9492)
i = 1
mod = await client.fetch_user(modid)
for user in users:
if user == member.id:
print("yes")
i += 1
embed.add_field(name= f"Infraction #{i}", value = f"**{type}** \nCase ID: {caseid}\nModerator: {mod}\nDate:{date}\n", inline = False )
但这对我没有帮助,它不会返回任何错误(我放了一个 except 会在之后打印错误)。我假设我没有正确获得 id 对象。