python - 尝试在 @event 中访问时出现 JSON 编辑问题,我该如何解决?
问题描述
对不起,如果我做错了这个代码部分,但我想我正在学习如何去做。无论如何,我的问题是我正在尝试访问 users.json(过去曾工作过)以获取用户硬币,但是我需要按照我假设的 @event 执行此操作,因为我需要能够访问我设置的变量json.load() 也相等。这是我的错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/discord/client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "/home/ubuntu/workspace/main/paeth.py", line 283, in on_reaction_add
users = json.load(f)
File "/usr/local/lib/python3.6/json/__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/local/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Ignoring exception in on_reaction_add
如果您正在寻找导致这种情况发生的代码,这里是 on_reaction_add 的 @client.event
@client.event
async def on_reaction_add(reaction, user):
with open('users.json', 'r') as f:
users = json.load(f)
global QUEUE
print(str(reaction.emoji))
if str(reaction.emoji) == "<:csgo:544232345620709376>":
role = discord.utils.get(user.server.roles, name="CS:GO")
await client.add_roles(user, role)
print("Added role CS:GO to user: " + str(user))
if str(reaction.emoji) == "<:overwatch:544232310896328739>":
role = discord.utils.get(user.server.roles, name="Overwatch")
await client.add_roles(user, role)
print("Added role Overwatch to user: " + str(user))
if str(reaction.emoji) == "<:apex:544232279325540359>":
role = discord.utils.get(user.server.roles, name="Apex Legends")
await client.add_roles(user, role)
print("Added role Apex Legends to user: " + str(user))
if str(reaction.emoji) == "<:cod:544232280374247453>":
role = discord.utils.get(user.server.roles, name="Call of Duty")
await client.add_roles(user, role)
print("Added role Call of Duty to user: " + str(user))
if str(reaction.emoji) == "<:fortnite:544232365313228800>":
role = discord.utils.get(user.server.roles, name="Fortnite")
await client.add_roles(user, role)
print("Added role Fortnite to user: " + str(user))
if str(reaction.emoji) == "✅":
print("User " + str(user) + " has just joined the queue.")
QUEUE.append(str(user))
if str(QUEUE[0]) == "PaethBot#1265":
QUEUE.remove(QUEUE[0])
else:
await client.send_message(user, "You have been added to the queue!")
print(QUEUE)
if str(reaction.emoji) == "":
print("User " + str(user) + " has just attempted to join the bank heist")
if str(user) == "PaethBot#1265":
print("Removing Bot's Eligibility")
else:
tempCoins = users[user]['coins']
if tempCoins < joinIn:
await client.send_message(user, "Sorry, but your heist-mates saw that you didn't have enough coins to join them, and kicked you to the curb!")
else:
await client.send_message(user, "Congrats! Your heist-mates accepted your join request and took your " + str(joinIn) + " coins!")
await client.send_message(user, "Check in on the bankheist channel to see progress")
users[user]['coins'] -= int(joinIn)
HEIST.append(str(user))
with open('users.json', 'r') as f:
json.dump(users, f)
这是我的 users.json 文件:刚刚检查过,显然它是空的?让我尝试修复。
整个 users.json:
{"265749200468180992": {"coins": 10}, "543968234685071370": {"coins": 0}}
新问题是它不断被擦除,这是我的代码:
【因为太长了不能加,而且我知道你们不喜欢hastebin的】
解决方案
推荐阅读
- c# - 这是 C# 中委托的有效使用吗?
- php - 使用 [unixODBC][FreeTDS][SQL Server] 从 PHP 查询 MS-SQL 数据库在尝试使用二进制列的 WHERE 时出错
- java - Gluon Mobile 中的文件选择器
- c++ - 如何使用 gRPC 在客户端和服务器之间双向发送和接收流元数据?
- firefox - Firefox 不尊重主机文件
- c# - 混合表达式体与多个参数的抛出
- aws-lambda - 将 Apollo 服务器调试为 AWS Lambda 函数
- excel - 获取用户窗体上复选框的状态
- android - Android Studio onStop() 中的一个数组
- sql - 通配符搜索的工作与预期相反,我是否错误地编写了查询?