Skip to content

Commit

Permalink
Fixed queues error
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiiRepair committed Oct 11, 2023
1 parent 6d41274 commit a48bb5a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
10 changes: 4 additions & 6 deletions bot/commands/play_song.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

@kreacher.on_message(filters.regex(pattern="^[!?/]play_song"))
async def _(client: Client, message: Message):
QUEUES = get_queues()
print(QUEUES)
data = await user_info(message.from_user)
file_name = os.path.join(_cwd, f"../../downloads/songs/{str(uuid.uuid4())}.mp3")
if message.chat.type == ChatType.PRIVATE:
Expand Down Expand Up @@ -86,7 +84,7 @@ async def _(client: Client, message: Message):
return await msg.edit(
"**__Can't find song.\n\nTry searching with more specific title.__**",
)
if str(message.chat.id) in QUEUES:
if str(message.chat.id) in get_queues():
position = get_last_position_in_queue(str(message.chat.id)) + 1
add_or_create_queue(
str(message.chat.id),
Expand All @@ -102,7 +100,7 @@ async def _(client: Client, message: Message):
[[InlineKeyboardButton("cʟᴏꜱᴇ", callback_data="close")]]
),
)
if str(message.chat.id) not in QUEUES:
if str(message.chat.id) not in get_queues():
add_or_create_queue(
str(message.chat.id),
from_user=str(message.from_user.id),
Expand Down Expand Up @@ -157,7 +155,7 @@ async def _(client: Client, message: Message):
)
url_mention = f"https://t.me/c/{message.chat.id}/{message.reply_to_message.id}"
msg_mention = url_mention.replace("/c/-100", "/c/")
if str(message.chat.id) in str(QUEUES):
if str(message.chat.id) in get_queues():
position = get_last_position_in_queue(str(message.chat.id)) + 1
add_or_create_queue(
str(message.chat.id),
Expand All @@ -173,7 +171,7 @@ async def _(client: Client, message: Message):
[[InlineKeyboardButton("cʟᴏꜱᴇ", callback_data="close")]]
),
)
if str(message.chat.id) not in str(QUEUES):
if str(message.chat.id) not in get_queues():
add_or_create_queue(
str(message.chat.id),
from_user=str(message.from_user.id),
Expand Down
13 changes: 6 additions & 7 deletions bot/commands/play_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

@kreacher.on_message(filters.regex(pattern="^[!?/]play_video"))
async def _(client: Client, message: Message):
QUEUES = get_queues()
data = await user_info(message.from_user)
try:
msg = await message.reply("\u23F3 **__Processing...__**")
Expand All @@ -47,7 +46,7 @@ async def _(client: Client, message: Message):
if " " in message.text:
query = message.text.split(maxsplit=1)[1]
if "cdn" in query:
if str(message.chat.id) in str(QUEUES):
if str(message.chat.id) in get_queues():
position = get_last_position_in_queue(str(message.chat.id)) + 1
add_or_create_queue(
str(message.chat.id),
Expand All @@ -63,7 +62,7 @@ async def _(client: Client, message: Message):
[[InlineKeyboardButton("cʟᴏꜱᴇ", callback_data="close")]]
),
)
if str(message.chat.id) not in str(QUEUES):
if str(message.chat.id) not in get_queues():
add_or_create_queue(
str(message.chat.id),
from_user=str(message.from_user.id),
Expand Down Expand Up @@ -115,7 +114,7 @@ async def _(client: Client, message: Message):
return await msg.edit(
"**__Can't find YouTube video.\n\nTry searching with more specific title.__**",
)
if str(message.chat.id) in str(QUEUES):
if str(message.chat.id) in get_queues():
position = get_last_position_in_queue(str(message.chat.id)) + 1
add_or_create_queue(
str(message.chat.id),
Expand All @@ -131,7 +130,7 @@ async def _(client: Client, message: Message):
[[InlineKeyboardButton("cʟᴏꜱᴇ", callback_data="close")]]
),
)
if str(message.chat.id) not in str(QUEUES):
if str(message.chat.id) not in get_queues():
add_or_create_queue(
str(message.chat.id),
from_user=str(message.from_user.id),
Expand Down Expand Up @@ -178,7 +177,7 @@ async def _(client: Client, message: Message):
progress=progress,
progress_args=(client, message.chat, msg),
)
if str(message.chat.id) in str(QUEUES):
if str(message.chat.id) in get_queues():
position = get_last_position_in_queue(str(message.chat.id)) + 1
add_or_create_queue(
str(message.chat.id),
Expand All @@ -194,7 +193,7 @@ async def _(client: Client, message: Message):
[[InlineKeyboardButton("cʟᴏꜱᴇ", callback_data="close")]]
),
)
if str(message.chat.id) not in str(QUEUES):
if str(message.chat.id) not in get_queues():
add_or_create_queue(
str(message.chat.id),
from_user=str(message.from_user.id),
Expand Down
15 changes: 7 additions & 8 deletions bot/helpers/queues.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json
import pickle
from bot import r
from typing import Dict, Tuple, Union

Expand All @@ -23,12 +23,12 @@ def add_or_create_queue(
"type_of": type_of,
}
]
values: bytes = json.dumps(kw).encode("utf-8")
values: bytes = pickle.dumps(kw)
queue: dict = get_queues()
if group_id in queue:
giq: list = queue[group_id]
giq.append(values)
values: bytes = json.dumps(giq).encode("utf-8")
giq.extend(kw)
values: bytes = pickle.dumps(giq)
hset = r.hset("queues", group_id, values)
if hset == 0:
return position
Expand Down Expand Up @@ -88,7 +88,7 @@ def remove_queue(group_id: str) -> None:

def get_queues() -> Union[Dict, None]:
rqueues = r.hgetall("queues")
queues = {f.decode(): json.loads(v.decode()) for f, v in rqueues.items()}
queues = {f.decode(): pickle.loads(v) for f, v in rqueues.items()}
return queues


Expand All @@ -110,7 +110,6 @@ def get_last_position_in_queue(group_id: str) -> Union[int, None]:
if group_id not in queue:
return None
value: dict = queue[group_id][-1]
print(value)
return value["position"]


Expand All @@ -125,9 +124,9 @@ def update_is_played_in_queue(group_id: str, action: str) -> Union[bool, None]:
if action == "previous":
values[i]["is_playing"] = False
values[i - 1]["is_playing"] = True
return r.hset("queues", group_id, json.dumps(values).encode("utf-8"))
return r.hset("queues", group_id, pickle.dumps(values))
if action == "next":
values[i]["is_playing"] = False
values[i + 1]["is_playing"] = True
return r.hset("queues", group_id, json.dumps(values).encode("utf-8"))
return r.hset("queues", group_id, pickle.dumps(values))
return True

0 comments on commit a48bb5a

Please sign in to comment.