From e6c2212943320c11b58450033fe9d1feeba63e44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CA=99=E1=B4=80=E1=B4=85=20=CA=99=E1=B4=80=CA=99=CA=8F?= <154504921+Badhacker98@users.noreply.github.com> Date: Sun, 22 Sep 2024 22:19:37 +0530 Subject: [PATCH] Update play.py --- Spotify_Music/utils/decorators/play.py | 464 ++++++++++++------------- 1 file changed, 222 insertions(+), 242 deletions(-) diff --git a/Spotify_Music/utils/decorators/play.py b/Spotify_Music/utils/decorators/play.py index e7b7a3a..17d002d 100644 --- a/Spotify_Music/utils/decorators/play.py +++ b/Spotify_Music/utils/decorators/play.py @@ -1,5 +1,6 @@ import asyncio +from pyrogram import filters from pyrogram.enums import ChatMemberStatus from pyrogram.errors import ( ChatAdminRequired, @@ -9,6 +10,10 @@ ) from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup +from config import PLAYLIST_IMG_URL, PRIVATE_BOT_MODE +from config import SUPPORT_GROUP as SUPPORT_CHAT +from config import adminlist +from strings import get_string from Spotify_Music import YouTube, app from Spotify_Music.misc import SUDOERS from Spotify_Music.utils.database import ( @@ -18,20 +23,74 @@ get_playmode, get_playtype, is_active_chat, + is_commanddelete_on, is_maintenance, + is_served_private_chat, ) from Spotify_Music.utils.inline import botplaylist_markup -from config import PLAYLIST_IMG_URL, SUPPORT_CHAT, adminlist -from strings import get_string links = {} -clinks = {} + +from pyrogram.errors import ChatAdminRequired + + +@app.on_callback_query(filters.regex("unban_userbot")) +async def unban_assistant_callback(client, callback_query): + chat_id = callback_query.message.chat.id + userbot = await get_assistant(chat_id) + ubot = userbot.username if username else userbot.id + + try: + # Unban the assistant + await app.unban_chat_member(chat_id, userbot.id) + + # Notify user of the success + await callback_query.answer( + "Assistant unbanned successfully✅\nNow trying to join the group⌛\n\nThanks for unbanning🥰" + ) + + # After unbanning, try to join the group using the method from your code + if callback_query.message.chat.username: + invitelink = callback_query.message.chat.username + try: + await userbot.resolve_peer(invitelink) + await asyncio.sleep(1) + await userbot.join_chat(invitelink) + await callback_query.message.reply_text( + "**Assistant has successfully joined the group. Now you can play songs✅**" + ) + except Exception as e: + await callback_query.message.reply_text( + f"**Failed to invite assistant after unbanning. Please give the bot [ (Invite Users Via Link) Admin Power ] to invite assistant in group.**\n\n**ID:** `{userbot.id}`\n**Username:** @{userbot.username}" + ) + else: + try: + invitelink = await client.export_chat_invite_link(chat_id) + await asyncio.sleep(1) + await userbot.join_chat(invitelink) + await callback_query.message.reply_text( + "**Assistant Joined successfully✅**\nThanks for unbanning🥰" + ) + except ChatAdminRequired: + await callback_query.message.reply_text( + f"**Please make the bot admin to invite the assistant**\n\n**ID:** `{userbot.id}`\n**Username:** @{userbot.username}" + ) + except Exception as e: + await callback_query.message.reply_text(f"Failed: {e}") + except Exception as e: + await callback_query.answer( + f"Failed to unban assistant: [ MAKE THE BOT ADMIN AND GIVE BAN POWER FOR UNBAN ASSISTANT ID ]", + show_alert=True, + ) def PlayWrapper(command): async def wrapper(client, message): + userbot = await get_assistant(message.chat.id) + userbot_id = userbot.id language = await get_lang(message.chat.id) _ = get_string(language) + if message.sender_chat: upl = InlineKeyboardMarkup( [ @@ -43,7 +102,7 @@ async def wrapper(client, message): ] ] ) - return await message.reply_text(_["general_3"], reply_markup=upl) + return await message.reply_text(_["general_4"], reply_markup=upl) if await is_maintenance() is False: if message.from_user.id not in SUDOERS: @@ -52,11 +111,20 @@ async def wrapper(client, message): disable_web_page_preview=True, ) - try: - await message.delete() - except: - pass + if PRIVATE_BOT_MODE == str(True): + if not await is_served_private_chat(message.chat.id): + await message.reply_text( + "**ᴘʀɪᴠᴀᴛᴇ ᴍᴜsɪᴄ ʙᴏᴛ**\n\nᴏɴʟʏ ғᴏʀ ᴀᴜᴛʜᴏʀɪᴢᴇᴅ ᴄʜᴀᴛs ғʀᴏᴍ ᴛʜᴇ ᴏᴡɴᴇʀ. ᴀsᴋ ᴍʏ ᴏᴡɴᴇʀ ᴛᴏ ᴀʟʟᴏᴡ ʏᴏᴜʀ ᴄʜᴀᴛ ғɪʀsᴛ." + ) + return await app.leave_chat(message.chat.id) + + if await is_commanddelete_on(message.chat.id): + try: + await message.delete() + except: + pass + # Telegram audio/video or URL check audio_telegram = ( (message.reply_to_message.audio or message.reply_to_message.voice) if message.reply_to_message @@ -78,6 +146,8 @@ async def wrapper(client, message): caption=_["playlist_1"], reply_markup=InlineKeyboardMarkup(buttons), ) + + # Get chat mode and play mode if message.command[0][0] == "c": chat_id = await get_cmode(message.chat.id) if chat_id is None: @@ -90,8 +160,11 @@ async def wrapper(client, message): else: chat_id = message.chat.id channel = None + playmode = await get_playmode(message.chat.id) playty = await get_playtype(message.chat.id) + + # Check for permissions if playty != "Everyone": if message.from_user.id not in SUDOERS: admins = adminlist.get(message.chat.id) @@ -100,266 +173,173 @@ async def wrapper(client, message): else: if message.from_user.id not in admins: return await message.reply_text(_["play_4"]) - if message.command[0][0] == "v": - video = True - else: - if "-v" in message.text: - video = True - else: - video = True if message.command[0][1] == "v" else None - if message.command[0][-1] == "e": - if not await is_active_chat(chat_id): - return await message.reply_text(_["play_18"]) - fplay = True - else: - fplay = None + # Video play check + video = True if (message.command[0][0] == "v" or "-v" in message.text) else None + fplay = True if message.command[0][-1] == "e" else None + + # Assistant join logic with modifications if not await is_active_chat(chat_id): - userbot = await get_assistant(chat_id) + # userbot = await get_assistant(message.chat.id) + + # Common chats check between bot and assistant + common_chats = await userbot.get_common_chats(app.username) + if chat_id in [chat.id for chat in common_chats]: + return await command( + client, message, _, chat_id, video, channel, playmode, url, fplay + ) + + # Handle public and private group cases try: - try: - get = await app.get_chat_member(chat_id, userbot.id) - except ChatAdminRequired: - return await message.reply_text(_["call_1"]) - if ( - get.status == ChatMemberStatus.BANNED - or get.status == ChatMemberStatus.RESTRICTED - ): - return await message.reply_text( - _["call_2"].format( - app.mention, userbot.id, userbot.name, userbot.username - ) - ) - except UserNotParticipant: - if chat_id in links: - invitelink = links[chat_id] - else: - if message.chat.username: - invitelink = message.chat.username - try: - await userbot.resolve_peer(invitelink) - except: - pass - else: - try: - invitelink = await app.export_chat_invite_link(chat_id) - except ChatAdminRequired: - return await message.reply_text(_["call_1"]) - except Exception as e: - return await message.reply_text( - _["call_3"].format(app.mention, type(e).__name__) - ) - if invitelink.startswith("https://t.me/+"): - invitelink = invitelink.replace( - "https://t.me/+", "https://t.me/joinchat/" - ) - myu = await message.reply_text(_["call_4"].format(app.mention)) - try: - await asyncio.sleep(1) - await userbot.join_chat(invitelink) - except InviteRequestSent: + get = await app.get_chat_member(chat_id, userbot_id) + + except UserNotParticipant: + if message.chat.username: + invitelink = message.chat.username try: - await app.approve_chat_join_request(chat_id, userbot.id) + await userbot.resolve_peer(invitelink) + await userbot.join_chat(invitelink) + except InviteRequestSent: + await app.approve_chat_join_request(chat_id, userbot_id) + return await command( + client, + message, + _, + chat_id, + video, + channel, + playmode, + url, + fplay, + ) except Exception as e: return await message.reply_text( - _["call_3"].format(app.mention, type(e).__name__) + f"**Failed to invite assistant. Please make the bot an admin to invite it.**\n\n**ID:** `{userbot.id}`\n**Username:** @{userbot.username}" ) - await asyncio.sleep(3) - await myu.edit(_["call_5"].format(app.mention)) - except UserAlreadyParticipant: - pass - except Exception as e: - return await message.reply_text( - _["call_3"].format(app.mention, type(e).__name__) - ) - - links[chat_id] = invitelink + else: + # If private, export invite link and try inviting + try: + invitelink = await client.export_chat_invite_link( + message.chat.id + ) + await asyncio.sleep(1) + await userbot.join_chat(invitelink) + return await command( + client, + message, + _, + chat_id, + video, + channel, + playmode, + url, + fplay, + ) + except ChatAdminRequired: + return await message.reply_text( + f"**Please make the bot admin to invite my assistant**\n\n**ID:** `{userbot.id}`\n**Username:** @{userbot.username}" + ) + except UserAlreadyParticipant: + pass + except Exception as e: + return await message.reply_text(f"Failed: {e}") + except ChatAdminRequired: + return await message.reply_text( + f"**Please make the bot admin to invite my assistant**\n\n**ID:** `{userbot.id}`\n**Username:** @{userbot.username}" + ) + # Check if assistant is banned or restricted + if ( + get.status == ChatMemberStatus.BANNED + or get.status == ChatMemberStatus.RESTRICTED + ): try: - await userbot.resolve_peer(chat_id) + await app.unban_chat_member(chat_id, userbot_id) except: - pass - - return await command( - client, - message, - _, - chat_id, - video, - channel, - playmode, - url, - fplay, - ) - - return wrapper - - -def CPlayWrapper(command): - async def wrapper(client, message): - i = await client.get_me() - language = await get_lang(message.chat.id) - _ = get_string(language) - if message.sender_chat: - upl = InlineKeyboardMarkup( - [ - [ - InlineKeyboardButton( - text="ʜᴏᴡ ᴛᴏ ғɪx ?", - callback_data="AnonymousAdmin", + return await message.reply_text( + text=f"**Assistant is banned in this group. Please unban the assistant to play songs!**\n\n**ID:** `{userbot.id}`\n**Username:** @{userbot.username}", + reply_markup=InlineKeyboardMarkup( + [ + [ + InlineKeyboardButton( + text="Unban Assistant", + callback_data=f"unban_userbot", + ) + ] + ] ), - ] - ] - ) - return await message.reply_text(_["general_3"], reply_markup=upl) - - if await is_maintenance() is False: - if message.from_user.id not in SUDOERS: - return await message.reply_text( - text=f"{i.mention} ɪs ᴜɴᴅᴇʀ ᴍᴀɪɴᴛᴇɴᴀɴᴄᴇ, ᴠɪsɪᴛ sᴜᴘᴘᴏʀᴛ ᴄʜᴀᴛ ғᴏʀ ᴋɴᴏᴡɪɴɢ ᴛʜᴇ ʀᴇᴀsᴏɴ.", - disable_web_page_preview=True, - ) - - try: - await message.delete() - except: - pass + ) - audio_telegram = ( - (message.reply_to_message.audio or message.reply_to_message.voice) - if message.reply_to_message - else None - ) - video_telegram = ( - (message.reply_to_message.video or message.reply_to_message.document) - if message.reply_to_message - else None - ) - url = await YouTube.url(message) - if audio_telegram is None and video_telegram is None and url is None: - if len(message.command) < 2: - if "stream" in message.command: - return await message.reply_text(_["str_1"]) - buttons = botplaylist_markup(_) - return await message.reply_photo( - photo=PLAYLIST_IMG_URL, - caption=_["play_18"], - reply_markup=InlineKeyboardMarkup(buttons), - ) - if message.command[0][0] == "c": - chat_id = await get_cmode(message.chat.id) - if chat_id is None: - return await message.reply_text(_["setting_7"]) - try: - chat = await client.get_chat(chat_id) - except: - return await message.reply_text(_["cplay_4"]) - channel = chat.title - else: - chat_id = message.chat.id - channel = None - playmode = await get_playmode(message.chat.id) - playty = await get_playtype(message.chat.id) - if playty != "Everyone": - if message.from_user.id not in SUDOERS: - admins = adminlist.get(message.chat.id) - if not admins: - return await message.reply_text(_["admin_13"]) - else: - if message.from_user.id not in admins: - return await message.reply_text(_["play_4"]) - if message.command[0][0] == "v": - video = True - else: - if "-v" in message.text: - video = True - else: - video = True if message.command[0][1] == "v" else None - if message.command[0][-1] == "e": - if not await is_active_chat(chat_id): - return await message.reply_text(_["play_16"]) - fplay = True - else: - fplay = None + # If group is public, try joining directly + if message.chat.username: + invitelink = message.chat.username - if not await is_active_chat(chat_id): - userbot = await get_assistant(chat_id) - try: try: - get = await client.get_chat_member(chat_id, userbot.username) - except ChatAdminRequired: - await message.reply_text(_["call_1"]) - if ( - get.status == ChatMemberStatus.BANNED - or get.status == ChatMemberStatus.RESTRICTED - ): - await message.reply_text( - _["call_2"].format( - i.mention, userbot.id, userbot.name, userbot.username - ) + await userbot.resolve_peer(invitelink) + await asyncio.sleep(1) + await userbot.join_chat(invitelink) + return await command( + client, + message, + _, + chat_id, + video, + channel, + playmode, + url, + fplay, ) - except UserNotParticipant: - if chat_id in clinks: - invitelink = clinks[chat_id] - else: - if message.chat.username: - invitelink = message.chat.username - try: - await userbot.resolve_peer(invitelink) - except: - pass - else: - try: - invitelink = await client.export_chat_invite_link(chat_id) - except ChatAdminRequired: - await message.reply_text(_["call_1"]) - except Exception as e: - await message.reply_text( - _["call_3"].format(i.mention, type(e).__name__) - ) + except InviteRequestSent: - if invitelink.startswith("https://t.me/+"): - invitelink = invitelink.replace( - "https://t.me/+", "https://t.me/joinchat/" + await app.approve_chat_join_request(chat_id, userbot.id) + await message.reply_text( + "**Assistant joined the group now playing...**" ) - myu = await message.reply_text(_["call_4"].format(i.mention)) + return await command( + client, + message, + _, + chat_id, + video, + channel, + playmode, + url, + fplay, + ) + except Exception as e: + return await message.reply_text( + f"**Failed to invite assistant. Please make the bot an admin to invite it.**\n\n**ID:** `{userbot.id}`\n**Username:** @{userbot.username}" + ) + else: + # If private, export invite link and try inviting try: + invitelink = await client.export_chat_invite_link(message.chat.id) await asyncio.sleep(1) await userbot.join_chat(invitelink) - except InviteRequestSent: - try: - await client.approve_chat_join_request(chat_id, userbot.id) - except Exception as e: - await message.reply_text( - _["call_3"].format(i.mention, type(e).__name__) - ) - await asyncio.sleep(3) - await myu.edit(_["call_5"].format(i.mention)) + return await command( + client, + message, + _, + chat_id, + video, + channel, + playmode, + url, + fplay, + ) + except ChatAdminRequired: + return await message.reply_text( + f"**Please make the bot admin to invite my assistant**\n\n**ID:** `{userbot.id}`\n**Username:** @{userbot.username}" + ) except UserAlreadyParticipant: pass except Exception as e: - await message.reply_text( - _["call_3"].format(i.mention, type(e).__name__) - ) - - clinks[chat_id] = invitelink - - try: - await userbot.resolve_peer(chat_id) - except: - pass + return await message.reply_text(f"Failed: {e}") return await command( - client, - message, - _, - chat_id, - video, - channel, - playmode, - url, - fplay, + client, message, _, chat_id, video, channel, playmode, url, fplay ) return wrapper +