Add storage, syncing db, rename module
This commit is contained in:
1
bot/modules/fsm/__init__.py
Normal file
1
bot/modules/fsm/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .in_db import InDbStorage
|
||||
36
bot/modules/fsm/in_db.py
Normal file
36
bot/modules/fsm/in_db.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from bot.modules.database import db
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, DefaultDict, Dict, Optional
|
||||
|
||||
from aiogram.fsm.state import State
|
||||
from aiogram.fsm.storage.base import (
|
||||
BaseStorage,
|
||||
StateType,
|
||||
StorageKey,
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class MemoryStorageRecord:
|
||||
data: Dict[str, Any] = field(default_factory=dict)
|
||||
state: Optional[str] = None
|
||||
|
||||
|
||||
class InDbStorage(BaseStorage):
|
||||
def __init__(self) -> None:
|
||||
self.storage: DefaultDict[StorageKey, MemoryStorageRecord] = db.fsm
|
||||
|
||||
async def close(self) -> None:
|
||||
pass
|
||||
|
||||
async def set_state(self, key: StorageKey, state: StateType = None) -> None:
|
||||
self.storage[key].state = state.state if isinstance(state, State) else state
|
||||
|
||||
async def get_state(self, key: StorageKey) -> Optional[str]:
|
||||
return self.storage[key].state
|
||||
|
||||
async def set_data(self, key: StorageKey, data: Dict[str, Any]) -> None:
|
||||
self.storage[key].data = data.copy()
|
||||
|
||||
async def get_data(self, key: StorageKey) -> Dict[str, Any]:
|
||||
return self.storage[key].data.copy()
|
||||
Reference in New Issue
Block a user