尝试在@event中访问时JSON编辑问题,如何解决?

时间:2019-02-12 20:56:17

标签: python python-3.x async-await

很抱歉,如果我将此代码部分做错了,但是我想我正在学习如何做。无论如何,我的问题是我试图访问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的]

0 个答案:

没有答案