From 06c494d86dcc5d2c96f01a2bfe1188a67ed7100d Mon Sep 17 00:00:00 2001 From: Santiago Ramirez Date: Mon, 9 Oct 2023 23:24:15 +0000 Subject: [PATCH] Fixed postgres issues --- .env.example | 23 +++++++------- webdriver.sh => .webdriver.sh | 3 +- Dockerfile | 4 ++- bot/__init__.py | 21 +++++++++++++ bot/__main__.py | 22 ------------- bot/callbacks/misc.py | 4 +-- bot/callbacks/streams.py | 52 +++++++++++-------------------- bot/helpers/handler.py | 49 ----------------------------- bot/plugins/commands/leave.py | 4 +-- bot/plugins/commands/streaming.py | 4 +-- bot/plugins/filters/on_joined.py | 2 ++ docker-compose.yml | 14 ++++++++- 12 files changed, 77 insertions(+), 125 deletions(-) rename webdriver.sh => .webdriver.sh (52%) delete mode 100755 bot/helpers/handler.py diff --git a/.env.example b/.env.example index 2d92e4d..1528ff6 100755 --- a/.env.example +++ b/.env.example @@ -5,23 +5,24 @@ BOT_USERNAME="YOUR-TELEGRAM-BOT-USERNAME" CHANNEL="YOUR-TELEGRAM-BOT-CHANNEL" ES_MOVIES_CHANNEL="https://t.me/+ZHVk4AoouP85NGVh" ES_SERIES_CHANNEL="https://t.me/+tf2D067PgIY1ODdh" -MANAGEMENT_MODE="" +MANAGEMENT_MODE="..." MANTAINER="DEV-USERNAME" -SESSION_STRING="" +SESSION_STRING="..." # POSTGRES SETTINGS PGUSER="postgres" -POSTGRES_DB="..." -POSTGRES_USER="..." -POSTGRES_PASSWORD="..." -POSTGRES_HOST="localhost" -POSTGRES_PORT="5432" +POSTGRES_DB="bot" +POSTGRES_USER="kreacher" +POSTGRES_PASSWORD="password" +POSTGRES_HOST="localhost" # 'db' for docker +POSTGRES_PORT="5433" POSTGRES_HOST_AUTH_METHOD="trust" # PGADMIN SETTINGS -PGADMIN_DEFAULT_EMAIL="" -PGADMIN_DEFAULT_PASSWORD="" +PGADMIN_DEFAULT_EMAIL="bot@kreacher.com" +PGADMIN_DEFAULT_PASSWORD="password" # REDIS SETTINGS -REDIS_PORT="" -REDIS_PASSWORD="" \ No newline at end of file +REDIS_HOST="localhost" # 'cache' for docker +REDIS_PORT="6379" +REDIS_PASSWORD="password" \ No newline at end of file diff --git a/webdriver.sh b/.webdriver.sh similarity index 52% rename from webdriver.sh rename to .webdriver.sh index 74b6c41..8e09bb8 100644 --- a/webdriver.sh +++ b/.webdriver.sh @@ -1,5 +1,6 @@ #!/bin/bash +sudomapt-get update && sudo apt-get upgrade -y wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install +sudo dpkg -i google-chrome-stable_current_amd64.deb; sudo apt-get -fy install rm -rf google-chrome-stable_current_amd64.deb \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 8188441..31b3550 100755 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,8 @@ RUN apt-get install ffmpeg tree -y RUN pip install --upgrade pip COPY . /kreacher/ WORKDIR /kreacher/ +RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb +RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install +RUN rm -rf google-chrome-stable_current_amd64.deb RUN make install -CMD ["bash", "webdriver.sh"] ENTRYPOINT ["make", "start"] \ No newline at end of file diff --git a/bot/__init__.py b/bot/__init__.py index 5708c5f..55ff32c 100755 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -4,6 +4,7 @@ import sqlalchemy as db from pyrogram import Client from termcolor import colored +from datetime import datetime from pytgcalls import GroupCallFactory from bot.config import config @@ -97,3 +98,23 @@ kreacher.start() assistant.start() driver = get_driver() + +START_TIME = datetime.utcnow() +TIME_DURATION_UNITS = ( + ("week", 60 * 60 * 24 * 7), + ("day", 60 * 60 * 24), + ("hour", 60 * 60), + ("min", 60), + ("sec", 1), +) + + +async def execution_time(seconds): + if seconds == 0: + return "inf" + parts = [] + for unit, div in TIME_DURATION_UNITS: + amount, seconds = divmod(int(seconds), div) + if amount > 0: + parts.append("{}{}{}".format(amount, unit, "" if amount == 1 else "s")) + return ", ".join(parts) diff --git a/bot/__main__.py b/bot/__main__.py index 2d85cbc..8a0d4ab 100755 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -1,6 +1,5 @@ import asyncio from pyrogram import idle -from datetime import datetime from termcolor import colored from pyrogram.types import BotCommand @@ -36,27 +35,6 @@ async def start_bot(): print(f'{colored("[INFO]", "blue")}: SUCCESSFULLY STARTED BOT!') -START_TIME = datetime.utcnow() -TIME_DURATION_UNITS = ( - ("week", 60 * 60 * 24 * 7), - ("day", 60 * 60 * 24), - ("hour", 60 * 60), - ("min", 60), - ("sec", 1), -) - - -async def execution_time(seconds): - if seconds == 0: - return "inf" - parts = [] - for unit, div in TIME_DURATION_UNITS: - amount, seconds = divmod(int(seconds), div) - if amount > 0: - parts.append("{}{}{}".format(amount, unit, "" if amount == 1 else "s")) - return ", ".join(parts) - - if __name__ == "__main__": try: idle() diff --git a/bot/callbacks/misc.py b/bot/callbacks/misc.py index c3a823e..13c7f9e 100755 --- a/bot/callbacks/misc.py +++ b/bot/callbacks/misc.py @@ -1,15 +1,15 @@ import os from time import time -from bot import kreacher from datetime import datetime from pyrogram import filters, Client from pyrogram.types import CallbackQuery from pyrogram.enums.chat_type import ChatType from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup + from bot.config import config -from bot.__main__ import execution_time, START_TIME from bot.plugins.commands.start import PM_START_TEXT +from bot import kreacher, execution_time, START_TIME thumb = "https://telegra.ph/file/3e14128ad5c9ec47801bd.jpg" diff --git a/bot/callbacks/streams.py b/bot/callbacks/streams.py index 853ba66..9983094 100755 --- a/bot/callbacks/streams.py +++ b/bot/callbacks/streams.py @@ -4,9 +4,13 @@ from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup from bot import kreacher, VOICE_CHATS -from bot.helpers.pkl import load_pkl, dump_pkl from bot.decorators.only_managers import only_managers -from bot.helpers.handler import next_item, skip_current +from bot.helpers.queues import ( + get_queues, + next_in_queue, + previous_in_queue, + remove_queue, +) _cwd = os.path.dirname(os.path.abspath(__file__)) @@ -59,39 +63,19 @@ async def _(client: Client, callback: CallbackQuery): @kreacher.on_callback_query(filters.regex("next")) async def _(client: Client, callback: CallbackQuery): - chat = callback.message.chat - QUEUE = await load_pkl(queues, "rb", "dict") - if len(callback.text.split()) < 2: - op = await skip_current(chat) - if op == 0: - await callback.reply("**Nothing Is Streaming**") - elif op == 1: - await callback.reply("empty queue, leaving voice chat") - else: - await callback.reply( - f"**⏭ Skipped**\n**🎧 Now Playing** - [{op[0]}]({op[1]})", - link_preview=False, - ) - else: - skip = callback.text.split(maxsplit=1)[1] + await callback.reply( + f"**⏭ Skipped**\n**🎧 Now Playing**", + link_preview=False, + ) + this = callback.text.split(maxsplit=1)[1] + if callback.message.chat.id in get_queues(): DELQUE = "**Removing Following Songs From Queue:**" - if callback.id in QUEUE: - items = [int(x) for x in skip.split(" ") if x.isdigit()] - items.sort(reverse=True) - for x in items: - if x != 0: - hm = await next_item(chat, x) - if hm != 0: - DELQUE = DELQUE + "\n" + f"**#{x}** - {hm}" - await callback.reply(DELQUE) + await callback.reply(DELQUE) -@kreacher.on_callback_query(filters.regex("controls")) +@kreacher.on_callback_query(filters.regex("remove_queues")) async def _(client: Client, callback: CallbackQuery): - chat = callback.message.chat - QUEUE = await load_pkl(queues, "rb", "dict") - QUEUE.pop(chat.id) - dump_pkl(queues, "wb", QUEUE) - await VOICE_CHATS[chat.id].stop_media() - await VOICE_CHATS[chat.id].stop() - VOICE_CHATS.pop(chat.id) + remove_queue(str(callback.message.chat)) + await VOICE_CHATS[callback.message.chat.id].stop_media() + await VOICE_CHATS[callback.message.chat.id].stop() + VOICE_CHATS.pop(callback.message.chat.id) diff --git a/bot/helpers/handler.py b/bot/helpers/handler.py deleted file mode 100755 index ee1c4bd..0000000 --- a/bot/helpers/handler.py +++ /dev/null @@ -1,49 +0,0 @@ -import os -from bot import VOICE_CHATS -from bot.helpers.pkl import load_pkl, dump_pkl -from bot.helpers.queues import clear_queue, get_queue, pop_an_item - -_cwd = os.path.dirname(os.path.abspath(__file__)) -queues = os.path.join(_cwd, "../dbs/queues.pkl") -actives = os.path.join(_cwd, "../dbs/actives.pkl") - - -async def skip_current(chat): - QUEUE = await load_pkl(queues, "rb", "dict") - if chat.id not in QUEUE: - return 0 - chat_queue = get_queue(chat) - if len(chat_queue) == 1: - await VOICE_CHATS[chat.id].stop_video() - await VOICE_CHATS[chat.id].stop() - clear_queue(chat) - VOICE_CHATS.pop(chat.id) - ACTIVE = await load_pkl(actives, "rb", "list") - ACTIVE.remove(chat.id) - dump_pkl(actives, "wb", ACTIVE) - return 1 - name = chat_queue[1][0] - url = chat_queue[1][1] - link = chat_queue[1][2] - media_type = chat_queue[1][3] - if media_type == "audio": - await VOICE_CHATS[chat.id].start_audio(url, repeat=False) - elif media_type == "video": - await VOICE_CHATS[chat.id].start_video(url, with_audio=True, repeat=False) - pop_an_item(chat) - return [name, link, media_type] - - -async def next_item(chat, x: int): - QUEUE = await load_pkl(queues, "rb", "dict") - if chat.id not in QUEUE: - return 0 - chat_queue = get_queue(chat) - try: - name = chat_queue[x][0] - chat_queue.pop(x) - dump_pkl(queues, "wb", chat_queue) - return name - except Exception as e: - print(e) - return 0 diff --git a/bot/plugins/commands/leave.py b/bot/plugins/commands/leave.py index fc55fb1..041ed5a 100755 --- a/bot/plugins/commands/leave.py +++ b/bot/plugins/commands/leave.py @@ -1,7 +1,7 @@ from pyrogram.types import Message from pyrogram import filters, Client from bot import kreacher, VOICE_CHATS -from bot.helpers.queues import clear_queue +from bot.helpers.queues import remove_queue from bot.decorators.only_admins import only_admins from bot.decorators.only_grps_chnns import only_grps_chnns @@ -15,7 +15,7 @@ async def _(client: Client, message: Message): raise Exception("Streaming is not active") await VOICE_CHATS[message.chat.id].leave_current_group_call() VOICE_CHATS.pop(message.chat.id) - await clear_queue(message.chat) + await remove_queue(str(message.chat.id)) await message.reply( "**__Goodbye master, just call me if you need me \U0001FAE1 \n\nVoice Chat left successfully__** \u2728", ) diff --git a/bot/plugins/commands/streaming.py b/bot/plugins/commands/streaming.py index 8781b4d..58283a3 100755 --- a/bot/plugins/commands/streaming.py +++ b/bot/plugins/commands/streaming.py @@ -12,7 +12,7 @@ from bot import assistant, kreacher, on_call, VOICE_CHATS from bot.decorators.only_grps_chnns import only_grps_chnns from bot.helpers.queues import ( - clear_queue, + remove_queue, ) _cwd = os.path.dirname(os.path.abspath(__file__)) @@ -123,5 +123,5 @@ async def _(client: Client, message: Message): ) if message.chat.id in VOICE_CHATS: await VOICE_CHATS[message.chat.id].stop() - await clear_queue(message.chat) + await remove_queue(str(message.chat.id)) VOICE_CHATS.pop(message.chat.id) diff --git a/bot/plugins/filters/on_joined.py b/bot/plugins/filters/on_joined.py index 1042a78..f7a8c24 100755 --- a/bot/plugins/filters/on_joined.py +++ b/bot/plugins/filters/on_joined.py @@ -1,3 +1,4 @@ +""" import os from bot import kreacher from tinydb import TinyDB, Query @@ -29,3 +30,4 @@ async def _(client: Client, message: Message): "subscription": None, } ) +""" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index a3a240d..633b409 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,12 +10,15 @@ services: - ./.env ports: - "$POSTGRES_PORT:$POSTGRES_PORT" + networks: + - kreacher cache: image: redis:7.2.1-alpine3.18 restart: always - user: root volumes: - cache:/data + networks: + - kreacher command: redis-server --save 20 1 --loglevel warning --requirepass $REDIS_PASSWORD ports: - "$REDIS_PORT:$REDIS_PORT" @@ -28,15 +31,24 @@ services: - ./.env depends_on: - db + networks: + - kreacher ports: - "5050:80" expose: - 5050 app: build: . + restart: always depends_on: - db - cache + networks: + - kreacher + +networks: + kreacher: + driver: bridge volumes: db: