字典里查不到

问题描述 投票:0回答:1

导入时,我得到一个空字典

在player.py文件中创建字典

local_user_dict = {}

然后在异步函数 def get_nameofteam 中我更新了它

    local_user_dict.update({message.chat.id: data}) local_user_dict[message.chat.id]['username']

其中数据是状态机阶段。

然后我导入我需要的文件

from player import local_user_dict

当我这样做时

print(local_user_dict)
,然后我得到一本空字典

import aiogram

from aiogram.dispatcher.filters.state import State, StatesGroup

local_user_dict = {}

class UserState(StatesGroup):
    name = State()

async def cmd_start(message: types.Message):
await message.answer(f'{welcomemessage}\n\n*Введите имя команды*', parse_mode='Markdown', reply_markup=types.ReplyKeyboardRemove())
                await UserState.name.set()
                DB.DATA[message.chat.id] = {}
                DB.BDJSON.save(DB.DATA)

async def get_nameofteam(message: types.Message, state: FSMContext):
    await state.update_data(username=message.text)
    await message.answer("Отлично! Теперь можете использовать бота дальше", reply_markup=kb.main_menu)
    data = await state.get_data()
    local_user_dict.update({message.chat.id: data})
    user = local_user_dict[message.chat.id]['username']
    print(user)
    if message.chat.id in DB.DATA:
        DB.DATA[message.chat.id]["nameofteam"] = data['username']
        DB.DATA[message.chat.id]["tg_username"] = message.chat.username
        DB.BDJSON.save(DB.DATA)
    else:
        DB.DATA[message.chat.id] = {
            "nameofteam" : "NoTeam",
            "username" : 'default'
        }
        DB.BDJSON.save(DB.DATA)
    await state.reset_state(with_data=False)

另一个文件

    from player import local_user_dict
    
    class FSMQuestion(StatesGroup):
        text = State()
    
    async def newquestion(message: types.Message, state: FSMContext):
        async with state.proxy() as data:
            if (message.content_type == 'photo'):
                data['text'] = message.caption
            elif (message.content_type == 'video'):
                data['text'] = message.caption
            else:
                data['text'] = message.text
        await state.finish()
        if (message.chat.username == None):
            who = "Ник не установлен"
        else:
            who = "@" + message.chat.username
        question = data['text']
        print(local_user_dict)
        nameofteam = DB.DATA.get(str(message.chat.id))["nameofteam"] if str(message.chat.id) in DB.DATA else 'NoTeam'
python python-3.x pycharm aiogram
1个回答
0
投票

这个:

from player import local_user_dict

创建

local_user_dict
的新实例。

它应该是空的。

如果您想获取在异步函数中分配给它的值,您需要确保两者都调用同一个实例。

您没有向我们提供足够的代码来帮助您进一步保证这一点。

© www.soinside.com 2019 - 2024. All rights reserved.