async def create(cls, instance: 'Menu') -> 'Session': """Creates a new session based from a menu instance and adds it to the session store. Checks for existing sessions in the store and handles safe deletion.""" _ = Session.get(instance) if instance.ctx.author.id in sessions: if Session.check_user_limit(instance.ctx.author.id): earliest = min(sessions[instance.ctx.author.id].items(), key=itemgetter(0)) await earliest[1].instance.close() else: pass self = Session() self.key = instance.ctx.author.id instance._id = int(time.time()) self.instance = instance self.history = instance.history self.active = True if sessions.get(self.key, False): sessions[self.key][instance._id] = self else: sessions[self.key] = {} sessions[self.key][instance._id] = self return self
def check_user_limit(user_id): """Predicate check for the amount of sessions a user has total.""" return True if len( sessions.get(user_id)) >= SESSION_PER_USER_LIMIT else False
def check_guild_limit(self): """Predicate check for the amount of sessions a user has in a single channel total.""" return False if len(sessions.get( self.key)) >= SESSION_PER_USER_LIMIT else True
def get(instance: 'Menu') -> 'Session': """Returns an existing session object from the sessions store.""" if user_sessions := sessions.get(instance.ctx.author.id, {}): return user_sessions.get(instance._id, {})