Discord.py 我不能在 python 中使用 dict 来保存用户数据

Discord.py I cant use dict in python to save user data

我的代码需要你的帮助。我正在尝试编写一个水平系统,用户可以在其中通过写作来提高水平。但是我无法将用户信息保存到字典中。

“.setuplevel”命令应将公会的每个成员(机器人除外)写入字典,并给出值 [0] = 0 和 [1] = 0。[0] 是玩家的经验,[ 1]是实际水平。

谁能帮帮我?

代码:

users = {}

async def add_experience(self, user, users):
    client.users[f'{user.id}'][0] += 500
    with open('users.yml', 'w') as outfile:
        yaml.dump(client.users, outfile, default_flow_style=False)
    if ((client.users[user.id][1] + 1) * 300) < client.users[user.id][0]:
        print("levelup")

def saveUser(self, user_id):
    print(user_id)
    client.users[f'{user_id}'] = {}
    client.users[f'{user_id}'][0] = 0
    client.users[f'{user_id}'][1] = 0
    with open('users.yml', 'w') as outfile:
        yaml.dump(client.users, outfile, default_flow_style=False)

async def on_ready(self):
    with open("users.yml", 'r') as stream:
        client.users = yaml.safe_load(stream)

async def on_message(self, message):
    if message.author == client.user:
        return

            elif message.content == ".setuplevel":
        await message.delete()
        print(message.guild.members)
        for user in message.guild.members:
            if not user == client.user:
                print(user.id)
                client.saveUser(message.author.id)

    elif message.author.id not in client.last_message:
        client.last_message[message.author.id] = message.created_at
    else:
        if (message.created_at - client.last_message[message.author.id]).total_seconds() > 25:
            await client.add_experience(message.author, client.users)

这是调用“.setuplevel”后出现的错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\sanel\PycharmProjects\Log-Host\venv\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
  File "C:/Users/sanel/PycharmProjects/Log-Host/main.py", line 306, in on_message
    if not user == client.user:
  File "C:/Users/sanel/PycharmProjects/Log-Host/main.py", line 79, in saveUser
    client.users[f'{user_id}'] = {}
TypeError: 'NoneType' object does not support item assignment

如回溯所示,'NoneType' 对象不支持项目分配。这意味着 userid 的值为 None 并且您正在尝试使用 None 作为字典中的键。