diff --git a/driver/decorators.py b/driver/decorators.py index 7e548a5..e0d94cd 100644 --- a/driver/decorators.py +++ b/driver/decorators.py @@ -1,3 +1,4 @@ +import traceback from typing import Callable from pyrogram import Client from pyrogram.types import Message @@ -19,6 +20,7 @@ def errors(func: Callable) -> Callable: try: return await func(client, message) except Exception as e: + traceback.print_exc() await message.reply(f"{type(e).__name__}: {e}") return decorator 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 9461136..5d06413 100644 --- a/program/admins.py +++ b/program/admins.py @@ -1,4 +1,5 @@ -import os +import traceback + from cache.admins import admins from driver.core import calls, bot from pyrogram import Client, filters @@ -7,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 ( @@ -71,6 +72,7 @@ async def stop(client, m: Message): clear_queue(chat_id) await m.reply("āœ… The userbot has disconnected from the video chat.") except Exception as e: + traceback.print_exc() await m.reply(f"šŸš« **error:**\n\n`{e}`") else: await m.reply("āŒ **nothing is streaming**") @@ -97,6 +99,7 @@ async def pause(client, m: Message): "āø **Track paused.**\n\nā€¢ **To resume the stream, use the**\nĀ» /resume command." ) except Exception as e: + traceback.print_exc() await m.reply(f"šŸš« **error:**\n\n`{e}`") else: await m.reply("āŒ **nothing is streaming**") @@ -123,6 +126,7 @@ async def resume(client, m: Message): "ā–¶ļø **Track resumed.**\n\nā€¢ **To pause the stream, use the**\nĀ» /pause command." ) except Exception as e: + traceback.print_exc() await m.reply(f"šŸš« **error:**\n\n`{e}`") else: await m.reply("āŒ **nothing is streaming**") @@ -161,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:" @@ -201,6 +205,7 @@ async def mute(client, m: Message): "šŸ”‡ **Userbot muted.**\n\nā€¢ **To unmute the userbot, use the**\nĀ» /unmute command." ) except Exception as e: + traceback.print_exc() await m.reply(f"šŸš« **error:**\n\n`{e}`") else: await m.reply("āŒ **nothing is streaming**") @@ -227,6 +232,7 @@ async def unmute(client, m: Message): "šŸ”Š **Userbot unmuted.**\n\nā€¢ **To mute the userbot, use the**\nĀ» /mute command." ) except Exception as e: + traceback.print_exc() await m.reply(f"šŸš« **error:**\n\n`{e}`") else: await m.reply("āŒ **nothing is streaming**") @@ -251,6 +257,7 @@ async def cbpause(_, query: CallbackQuery): await music_off(chat_id) await query.answer("āø The music has paused !\n\nĀ» to resume the music click on resume button !", show_alert=True) except Exception as e: + traceback.print_exc() await query.edit_message_text(f"šŸš« **error:**\n\n`{e}`", reply_markup=close_mark) else: await query.answer("āŒ nothing is currently streaming", show_alert=True) @@ -275,6 +282,7 @@ async def cbresume(_, query: CallbackQuery): await music_on(chat_id) await query.answer("ā–¶ļø The music has resumed !\n\nĀ» to pause the music click on pause button !", show_alert=True) except Exception as e: + traceback.print_exc() await query.edit_message_text(f"šŸš« **error:**\n\n`{e}`", reply_markup=close_mark) else: await query.answer("āŒ nothing is currently streaming", show_alert=True) @@ -297,6 +305,7 @@ async def cbstop(_, query: CallbackQuery): clear_queue(chat_id) await query.edit_message_text("āœ… **this streaming has ended**", reply_markup=close_mark) except Exception as e: + traceback.print_exc() await query.edit_message_text(f"šŸš« **error:**\n\n`{e}`", reply_markup=close_mark) else: await query.answer("āŒ nothing is currently streaming", show_alert=True) @@ -321,6 +330,7 @@ async def cbmute(_, query: CallbackQuery): await music_off(chat_id) await query.answer("šŸ”‡ The stream userbot has muted !\n\nĀ» to unmute the userbot click on unmute button !", show_alert=True) except Exception as e: + traceback.print_exc() await query.edit_message_text(f"šŸš« **error:**\n\n`{e}`", reply_markup=close_mark) else: await query.answer("āŒ nothing is currently streaming", show_alert=True) @@ -345,6 +355,7 @@ async def cbunmute(_, query: CallbackQuery): await music_on(chat_id) await query.answer("šŸ”Š The stream userbot has unmuted !\n\nĀ» to mute the userbot click on mute button !", show_alert=True) except Exception as e: + traceback.print_exc() await query.edit_message_text(f"šŸš« **error:**\n\n`{e}`", reply_markup=close_mark) else: await query.answer("āŒ nothing is currently streaming", show_alert=True) @@ -371,6 +382,7 @@ async def change_volume(client, m: Message): f"āœ… **volume set to** `{range}`%" ) except Exception as e: + traceback.print_exc() await m.reply(f"šŸš« **error:**\n\n`{e}`") else: await m.reply("āŒ **nothing in streaming**") 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 59effd4..bd32f79 100644 --- a/program/downloader.py +++ b/program/downloader.py @@ -4,6 +4,8 @@ import os import re import time import asyncio +import traceback + import lyricsgenius import aiofiles @@ -21,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) @@ -82,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) @@ -126,6 +129,7 @@ async def video_downloader(_, message): ytdl_data = ytdl.extract_info(link, download=True) file_name = ytdl.prepare_filename(ytdl_data) except Exception as e: + traceback.print_exc() return await msg.edit(f"šŸš« error: `{e}`") preview = wget.download(thumbnail) await msg.edit("šŸ“¤ uploading video...") @@ -136,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) @@ -173,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/extra.py b/program/extra.py index 33cea8a..68884f1 100644 --- a/program/extra.py +++ b/program/extra.py @@ -2,6 +2,7 @@ import asyncio +import traceback from pyrogram.types import Message from pyrogram import Client, filters, __version__ as pyrover @@ -159,6 +160,7 @@ async def active_calls(c: Client, message: Message): for chat in chats: served_chats.append(int(chat["chat_id"])) except Exception as e: + traceback.print_exc() await message.reply_text(f"šŸš« error: `{e}`") text = "" j = 0 diff --git a/program/music.py b/program/music.py index a1bf67d..4f14d52 100644 --- a/program/music.py +++ b/program/music.py @@ -2,8 +2,9 @@ # Commit Start Date 20/10/2021 # Finished On 28/10/2021 -import os # pyrogram stuff +import traceback + from pyrogram import Client from pyrogram.errors import UserAlreadyParticipant, UserNotParticipant from pyrogram.types import InlineKeyboardMarkup, Message @@ -19,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 @@ -81,6 +82,7 @@ async def play(c: Client, m: Message): try: aing = await c.get_me() except Exception as e: + traceback.print_exc() return await m.reply_text(f"error:\n\n{e}") a = await c.get_chat_member(chat_id, aing.id) if a.status != "administrator": @@ -136,6 +138,7 @@ async def play(c: Client, m: Message): except UserAlreadyParticipant: pass except Exception as e: + traceback.print_exc() return await m.reply_text( f"āŒ **userbot failed to join**\n\n**reason**: `{e}`" ) @@ -172,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 @@ -204,10 +207,11 @@ 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) + traceback.print_exc() await m.reply_text(f"šŸš« error:\n\nĀ» {e}") else: if len(m.command) < 2: @@ -247,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...") @@ -273,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) @@ -315,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...") @@ -339,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 bd74111..04b7bb1 100644 --- a/program/video.py +++ b/program/video.py @@ -3,9 +3,9 @@ # Finished On 28/10/2021 -import os import re import asyncio +import traceback # repository stuff from config import BOT_USERNAME, IMG_1, IMG_2, IMG_5 from program.utils.inline import stream_markup @@ -17,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 @@ -96,6 +97,7 @@ async def vplay(c: Client, m: Message): try: aing = await c.get_me() except Exception as e: + traceback.print_exc() return await m.reply_text(f"error:\n\n{e}") a = await c.get_chat_member(chat_id, aing.id) if a.status != "administrator": @@ -151,6 +153,7 @@ async def vplay(c: Client, m: Message): except UserAlreadyParticipant: pass except Exception as e: + traceback.print_exc() return await m.reply_text( f"āŒ **userbot failed to join**\n\n**reason**: `{e}`" ) @@ -197,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 @@ -233,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( @@ -274,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...") @@ -299,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) @@ -345,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...") @@ -370,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) @@ -398,6 +401,7 @@ async def vstream(c: Client, m: Message): try: aing = await c.get_me() except Exception as e: + traceback.print_exc() return await m.reply_text(f"error:\n\n{e}") a = await c.get_chat_member(chat_id, aing.id) if a.status != "administrator": @@ -453,6 +457,7 @@ async def vstream(c: Client, m: Message): except UserAlreadyParticipant: pass except Exception as e: + traceback.print_exc() return await m.reply_text( f"āŒ **userbot failed to join**\n\n**reason**: `{e}`" )