diff --git a/driver/utils.py b/driver/utils.py index 11e7635..c56a33a 100644 --- a/driver/utils.py +++ b/driver/utils.py @@ -1,4 +1,5 @@ import asyncio +import os from driver.core import bot, calls from driver.database.dbqueue import remove_active_chat from driver.queues import ( @@ -152,3 +153,8 @@ async def bash(cmd): err = stderr.decode().strip() out = stdout.decode().strip() return out, err + + +def remove_if_exists(path): + if os.path.exists(path): + os.remove(path) diff --git a/program/admins.py b/program/admins.py index 036fda9..5d06413 100644 --- a/program/admins.py +++ b/program/admins.py @@ -1,4 +1,3 @@ -import os import traceback from cache.admins import admins @@ -9,7 +8,7 @@ from driver.design.chatname import CHAT_TITLE from driver.queues import QUEUE, clear_queue from driver.filters import command, other_filters from driver.decorators import authorized_users_only -from driver.utils import skip_current_song, skip_item +from driver.utils import skip_current_song, skip_item, remove_if_exists from driver.database.dbpunish import is_gbanned_user from driver.database.dbqueue import ( @@ -166,7 +165,7 @@ async def skip(c: Client, m: Message): reply_markup=InlineKeyboardMarkup(buttons), caption=f"⏭ **Skipped** to the next track.\n\n🗂 **Name:** [{op[0]}]({op[1]})\n💭 **Chat:** `{chat_id}`\n🧸 **Request by:** {requester}", ) - os.remove(image) + remove_if_exists(image) else: skip = m.text.split(None, 1)[1] track = "🗑 removed song from queue:" diff --git a/program/developer.py b/program/developer.py index ad2c8eb..1307371 100644 --- a/program/developer.py +++ b/program/developer.py @@ -1,4 +1,3 @@ -import os import re import sys import shutil @@ -16,6 +15,7 @@ from driver.filters import command from pyrogram import Client, filters from driver.database.dbchat import remove_served_chat from driver.decorators import bot_creator, sudo_users_only, errors +from driver.utils import remove_if_exists from pyrogram.types import Message, InlineKeyboardButton, InlineKeyboardMarkup @@ -86,7 +86,7 @@ async def executor(client, message): reply_markup=keyboard, ) await message.delete() - os.remove(filename) + remove_if_exists(filename) else: t2 = time() keyboard = InlineKeyboardMarkup( @@ -165,7 +165,7 @@ async def shellrunner(client, message): reply_to_message_id=message.message_id, caption="`OUTPUT`", ) - return os.remove("output.txt") + return remove_if_exists("output.txt") await edit_or_reply(message, text=f"`OUTPUT:`\n\n```{output}```") else: await edit_or_reply(message, text="`OUTPUT:`\n\n`no output`") diff --git a/program/downloader.py b/program/downloader.py index c0eadd2..bd32f79 100644 --- a/program/downloader.py +++ b/program/downloader.py @@ -23,6 +23,7 @@ from yt_dlp import YoutubeDL from config import BOT_USERNAME as bn from driver.filters import command, other_filters from driver.database.dbpunish import is_gbanned_user +from driver.utils import remove_if_exists @Client.on_message(command(["song", f"song@{bn}"]) & ~filters.edited) @@ -84,8 +85,8 @@ async def song_downloader(_, message): await m.edit("❌ error, wait for bot owner to fix") print(e) try: - os.remove(audio_file) - os.remove(thumb_name) + remove_if_exists(audio_file) + remove_if_exists(thumb_name) except Exception as e: print(e) @@ -139,7 +140,7 @@ async def video_downloader(_, message): caption=ytdl_data["title"], ) try: - os.remove(file_name) + remove_if_exists(file_name) await msg.delete() except Exception as e: print(e) @@ -176,6 +177,6 @@ async def get_lyric_genius(_, message: Message): caption=f"**OUTPUT:**\n\n`attached lyrics text`", quote=False, ) - os.remove(filename) + remove_if_exists(filename) else: await m.edit(xxx) diff --git a/program/music.py b/program/music.py index 0d0fa6d..4f14d52 100644 --- a/program/music.py +++ b/program/music.py @@ -2,7 +2,6 @@ # Commit Start Date 20/10/2021 # Finished On 28/10/2021 -import os # pyrogram stuff import traceback @@ -21,7 +20,7 @@ from driver.design.chatname import CHAT_TITLE from driver.filters import command, other_filters from driver.queues import QUEUE, add_to_queue from driver.core import calls, user, bot -from driver.utils import bash +from driver.utils import bash, remove_if_exists from driver.database.dbpunish import is_gbanned_user from driver.database.dblockchat import blacklisted_chats from driver.database.dbqueue import add_active_chat, remove_active_chat, music_on @@ -176,7 +175,7 @@ async def play(c: Client, m: Message): reply_markup=InlineKeyboardMarkup(buttons), caption=f"💡 **Track added to queue »** `{pos}`\n\n🗂 **Name:** [{songname}]({link}) | `music`\n⏱️ **Duration:** `{duration}`\n🧸 **Request by:** {requester}", ) - os.remove(image) + remove_if_exists(image) else: try: gcname = m.chat.title @@ -208,7 +207,7 @@ async def play(c: Client, m: Message): caption=f"🗂 **Name:** [{songname}]({link}) | `music`\n⏱️ **Duration:** `{duration}`\n🧸 **Request by:** {requester}", ) await idle() - os.remove(image) + remove_if_exists(image) except Exception as e: await suhu.delete() await remove_active_chat(chat_id) @@ -252,7 +251,7 @@ async def play(c: Client, m: Message): reply_markup=InlineKeyboardMarkup(buttons), caption=f"💡 **Track added to queue »** `{pos}`\n\n🗂 **Name:** [{songname}]({url}) | `music`\n**⏱ Duration:** `{duration}`\n🧸 **Request by:** {requester}", ) - os.remove(image) + remove_if_exists(image) else: try: await suhu.edit("🔄 Joining Group Call...") @@ -278,7 +277,7 @@ async def play(c: Client, m: Message): caption=f"🗂 **Name:** [{songname}]({url}) | `music`\n**⏱ Duration:** `{duration}`\n🧸 **Request by:** {requester}", ) await idle() - os.remove(image) + remove_if_exists(image) except Exception as ep: await suhu.delete() await remove_active_chat(chat_id) @@ -320,7 +319,7 @@ async def play(c: Client, m: Message): reply_markup=InlineKeyboardMarkup(buttons), caption=f"💡 **Track added to queue »** `{pos}`\n\n🗂 **Name:** [{songname}]({url}) | `music`\n**⏱ Duration:** `{duration}`\n🧸 **Request by:** {requester}", ) - os.remove(image) + remove_if_exists(image) else: try: await suhu.edit("🔄 Joining Group Call...") @@ -344,7 +343,7 @@ async def play(c: Client, m: Message): caption=f"🗂 **Name:** [{songname}]({url}) | `music`\n**⏱ Duration:** `{duration}`\n🧸 **Request by:** {requester}", ) await idle() - os.remove(image) + remove_if_exists(image) except Exception as ep: await suhu.delete() await remove_active_chat(chat_id) diff --git a/program/speedtest.py b/program/speedtest.py index 00dc782..23a56fe 100644 --- a/program/speedtest.py +++ b/program/speedtest.py @@ -1,6 +1,5 @@ # credit to TeamYukki for this speedtest module -import os import wget import speedtest @@ -9,6 +8,7 @@ from driver.filters import command, other_filters from driver.decorators import sudo_users_only from config import BOT_USERNAME as bname from driver.core import bot as app +from driver.utils import remove_if_exists from pyrogram import Client, filters from pyrogram.types import Message @@ -48,5 +48,5 @@ async def run_speedtest(_, message: Message): msg = await app.send_photo( chat_id=message.chat.id, photo=path, caption=output ) - os.remove(path) + remove_if_exists(path) await m.delete() diff --git a/program/video.py b/program/video.py index bc0b568..04b7bb1 100644 --- a/program/video.py +++ b/program/video.py @@ -3,7 +3,6 @@ # Finished On 28/10/2021 -import os import re import asyncio import traceback @@ -18,6 +17,7 @@ from driver.core import calls, user, bot from driver.database.dbpunish import is_gbanned_user from driver.database.dblockchat import blacklisted_chats from driver.database.dbqueue import add_active_chat, remove_active_chat, music_on +from driver.utils import remove_if_exists # pyrogram stuff from pyrogram import Client from pyrogram.errors import UserAlreadyParticipant, UserNotParticipant @@ -200,7 +200,7 @@ async def vplay(c: Client, m: Message): reply_markup=InlineKeyboardMarkup(buttons), caption=f"💡 **Track added to queue »** `{pos}`\n\n🗂 **Name:** [{songname}]({link}) | `video`\n⏱️ **Duration:** `{duration}`\n🧸 **Request by:** {requester}", ) - os.remove(image) + remove_if_exists(image) else: await loser.edit("🔄 Joining Group Call...") gcname = m.chat.title @@ -236,7 +236,7 @@ async def vplay(c: Client, m: Message): caption=f"🗂 **Name:** [{songname}]({link}) | `video`\n⏱️ **Duration:** `{duration}`\n🧸 **Request by:** {requester}", ) await idle() - os.remove(image) + remove_if_exists(image) else: if len(m.command) < 2: await m.reply( @@ -277,7 +277,7 @@ async def vplay(c: Client, m: Message): reply_markup=InlineKeyboardMarkup(buttons), caption=f"💡 **Track added to queue »** `{pos}`\n\n🗂 **Name:** [{songname}]({url}) | `video`\n⏱ **Duration:** `{duration}`\n🧸 **Request by:** {requester}", ) - os.remove(image) + remove_if_exists(image) else: try: await loser.edit("🔄 Joining Group Call...") @@ -302,7 +302,7 @@ async def vplay(c: Client, m: Message): caption=f"🗂 **Name:** [{songname}]({url}) | `video`\n⏱ **Duration:** `{duration}`\n🧸 **Request by:** {requester}", ) await idle() - os.remove(image) + remove_if_exists(image) except Exception as ep: await loser.delete() await remove_active_chat(chat_id) @@ -348,7 +348,7 @@ async def vplay(c: Client, m: Message): reply_markup=InlineKeyboardMarkup(buttons), caption=f"💡 **Track added to queue »** `{pos}`\n\n🗂 **Name:** [{songname}]({url}) | `video`\n⏱ **Duration:** `{duration}`\n🧸 **Request by:** {requester}", ) - os.remove(image) + remove_if_exists(image) else: try: await loser.edit("🔄 Joining Group Call...") @@ -373,7 +373,7 @@ async def vplay(c: Client, m: Message): caption=f"🗂 **Name:** [{songname}]({url}) | `video`\n⏱ **Duration:** `{duration}`\n🧸 **Request by:** {requester}", ) await idle() - os.remove(image) + remove_if_exists(image) except Exception as ep: await loser.delete() await remove_active_chat(chat_id)