From e71ec34dca4f9426c4acb2b7e5b180f38816ed70 Mon Sep 17 00:00:00 2001 From: Santiago Ramirez <94815926+SantiiRepair@users.noreply.github.com> Date: Sun, 8 Oct 2023 18:30:08 +0000 Subject: [PATCH] Update __init__.py and requirements-dev.txt --- bot/models/redis/__init__.py | 91 +++++++++++++++++++++++++----------- requirements-dev.txt | 1 + 2 files changed, 66 insertions(+), 26 deletions(-) diff --git a/bot/models/redis/__init__.py b/bot/models/redis/__init__.py index 7e98446..393eca4 100644 --- a/bot/models/redis/__init__.py +++ b/bot/models/redis/__init__.py @@ -5,46 +5,85 @@ def add_to_queue( - group_id: str, from_user: str, position: int, date: str, file: str, type: str + group_id: str, + from_user: str, + is_playing: bool, + date: str, + file: str, + type: str, + position=1, ) -> bool: - """Add or create queue from group_id""" - values = { - "from_user": from_user, - "position": position, - "date": date, - "file": file, - "type": type, - } + """Add or create queue in `group_id` field""" + values = [ + { + "from_user": from_user, + "is_playing": is_playing, + "position": position, + "date": date, + "file": file, + "type": type, + } + ] + queue = r.hgetall("queues") + if group_id in queue: + queue[group_id].append(values) + hset = r.hset("queues", group_id, queue[group_id]) + if hset == 0: + return True + return False hset = r.hset("queues", group_id, values) - return hset + if hset == 1: + return True + return False def next_in_queue(group_id: str) -> Union[Tuple, None]: queue = r.hgetall("queues") value = queue[group_id] - if not value: + if group_id not in queue: return None - values = ( - value["from_user"], - value["position"], - value["date"], - value["file"], - value["type"], - ) - return values + for i, v in range(len(value)): + if value[i].get("is_playing"): + next = value[i + 1] + values = ( + next["from_user"], + next["is_playing"], + next["position"], + next["date"], + next["file"], + next["type"], + ) + return values + return None -def remove_queue(group_id: str) -> bool: +def previous_in_queue(group_id: str) -> Union[Tuple, None]: queue = r.hgetall("queues") - hdel = r.hdel("queues", group_id) - if hdel <= 0: - return False - return True + value = queue[group_id] + if group_id not in queue: + return None + for i, v in range(len(value)): + if value[i].get("is_playing"): + next = value[i - 1] + values = ( + next["from_user"], + next["is_playing"], + next["position"], + next["date"], + next["file"], + next["type"], + ) + return values + return None + + +def remove_queue(group_id: str) -> None: + r.hdel("queues", group_id) -def get_queue_position(group_id: str) -> Union[int, None]: +def get_last_queue_position(group_id: str) -> Union[int, None]: queue = r.hgetall("queues") - if not queue: + if group_id not in queue: return None value = list(queue.values())[-1] return value["position"] diff --git a/requirements-dev.txt b/requirements-dev.txt index f454f39..2a49141 100755 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,2 +1,3 @@ black==23.7.0 +black[jupyter] pylint==2.17.5 \ No newline at end of file