From fb511e7837afead73c874c31a6113fa47886e07f Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 15 Feb 2022 12:44:58 +0800 Subject: [PATCH] [fix] Just run once get_me --- driver/core.py | 22 +++++++++++++++++----- driver/decorators.py | 4 ++-- program/callback.py | 14 +++++++------- program/extra.py | 5 +++-- program/music.py | 4 ++-- program/punishment.py | 11 ++++++----- program/start.py | 10 +++++----- program/userbot_tools.py | 4 ++-- program/video.py | 6 +++--- 9 files changed, 47 insertions(+), 33 deletions(-) diff --git a/driver/core.py b/driver/core.py index 3486794..6686a4b 100644 --- a/driver/core.py +++ b/driver/core.py @@ -1,5 +1,6 @@ -from requests import get +import asyncio from pyrogram import Client +from pyrogram.types import User from pytgcalls import PyTgCalls from config import API_HASH, API_ID, BOT_TOKEN, SESSION_NAME @@ -8,11 +9,9 @@ bot = Client( ":veez:", API_ID, API_HASH, - bot_token=BOT_TOKEN, - plugins={"root": "program"}, + bot_token=BOT_TOKEN ) -# A bad way to do it, but it works -me = get(f"https://api.telegram.org/bot{BOT_TOKEN}/getme").json()["result"] + user = Client( SESSION_NAME, api_id=API_ID, @@ -20,3 +19,16 @@ user = Client( ) calls = PyTgCalls(user, overload_quiet_mode=True) + +with bot as app: + me_bot = app.get_me() +with user as app: + me_user = app.get_me() + +bot = Client( # type: ignore + ":veez:", + API_ID, + API_HASH, + bot_token=BOT_TOKEN, + plugins={"root": "program"}, +) diff --git a/driver/decorators.py b/driver/decorators.py index b95ad4f..3a5f5bd 100644 --- a/driver/decorators.py +++ b/driver/decorators.py @@ -4,7 +4,7 @@ from typing import Callable, Union, Optional from pyrogram import Client from pyrogram.types import Message, CallbackQuery from config import SUDO_USERS, OWNER_ID -from driver.core import bot, me +from driver.core import bot, me_bot from driver.admins import get_administrators from driver.database.dblockchat import blacklisted_chats from driver.database.dbpunish import is_gbanned_user @@ -129,7 +129,7 @@ def require_admin( async def wrapper( client: Client, message: Union[CallbackQuery, Message], *args, **kwargs ): - has_perms = await check_perms(message, permissions, notice, me["id"] if self else None) + has_perms = await check_perms(message, permissions, notice, me_bot.id if self else None) if has_perms: return await func(client, message, *args, *kwargs) diff --git a/program/callback.py b/program/callback.py index d39679e..a2f4d03 100644 --- a/program/callback.py +++ b/program/callback.py @@ -1,6 +1,6 @@ # Copyright (C) 2021 By VeezMusicProject -from driver.core import me +from driver.core import me_bot from driver.decorators import check_blacklist from driver.queues import QUEUE from driver.database.dbpunish import is_gbanned_user @@ -21,7 +21,7 @@ from config import ( @Client.on_callback_query(filters.regex("home_start")) @check_blacklist() async def start_set(_, query: CallbackQuery): - BOT_NAME = me["first_name"] + BOT_NAME = me_bot.first_name await query.answer("home start") await query.edit_message_text( f"""✨ **Welcome [{query.message.chat.first_name}](tg://user?id={query.message.chat.id}) !**\n @@ -86,7 +86,7 @@ async def quick_set(_, query: CallbackQuery): @Client.on_callback_query(filters.regex("user_guide")) @check_blacklist() async def guide_set(_, query: CallbackQuery): - ass_uname = me["username"] + ass_uname = me_bot.first_name await query.answer("user guide") await query.edit_message_text( f"""❓ How to use this Bot ?, read the Guide below ! @@ -145,7 +145,7 @@ All commands can be used with (`! / .`) handler""", @Client.on_callback_query(filters.regex("user_command")) @check_blacklist() async def user_set(_, query: CallbackQuery): - BOT_NAME = me["first_name"] + BOT_NAME = me_bot.first_name await query.answer("basic commands") await query.edit_message_text( f"""✏️ Command list for all user. @@ -172,7 +172,7 @@ async def user_set(_, query: CallbackQuery): @Client.on_callback_query(filters.regex("admin_command")) @check_blacklist() async def admin_set(_, query: CallbackQuery): - BOT_NAME = me["first_name"] + BOT_NAME = me_bot.first_name await query.answer("admin commands") await query.edit_message_text( f"""✏️ Command list for group admin. @@ -199,7 +199,7 @@ async def admin_set(_, query: CallbackQuery): @check_blacklist() async def sudo_set(_, query: CallbackQuery): user_id = query.from_user.id - BOT_NAME = me["first_name"] + BOT_NAME = me_bot.first_name if user_id not in SUDO_USERS: await query.answer("⚠️ You don't have permissions to click this button\n\n» This button is reserved for sudo members of this bot.", show_alert=True) return @@ -228,7 +228,7 @@ async def sudo_set(_, query: CallbackQuery): @check_blacklist() async def owner_set(_, query: CallbackQuery): user_id = query.from_user.id - BOT_NAME = me["first_name"] + BOT_NAME = me_bot.first_name if user_id not in OWNER_ID: await query.answer("⚠️ You don't have permissions to click this button\n\n» This button is reserved for owner of this bot.", show_alert=True) return diff --git a/program/extra.py b/program/extra.py index 68884f1..c491f08 100644 --- a/program/extra.py +++ b/program/extra.py @@ -8,6 +8,8 @@ from pyrogram.types import Message from pyrogram import Client, filters, __version__ as pyrover from pytgcalls import (__version__ as pytgver) + +from driver.core import me_bot from program import __version__ as ver from program.start import __python_version__ as pyver @@ -126,9 +128,8 @@ async def broadcast_message_pin(c: Client, message: Message): @Client.on_message(command(["stats", f"stats@{uname}"]) & ~filters.edited) @sudo_users_only async def bot_statistic(c: Client, message: Message): - name = (await c.get_me()).first_name + name = me_bot.first_name chat_id = message.chat.id - user_id = message.from_user.id msg = await c.send_message( chat_id, "❖ Collecting Stats..." ) diff --git a/program/music.py b/program/music.py index 5647c4a..e8184bb 100644 --- a/program/music.py +++ b/program/music.py @@ -20,7 +20,7 @@ from driver.design.thumbnail import thumb 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.core import calls, user, bot, me_user from driver.utils import bash, remove_if_exists from driver.database.dbpunish import is_gbanned_user from driver.database.dblockchat import blacklisted_chats @@ -74,7 +74,7 @@ async def play(c: Client, m: Message): "you're an __Anonymous__ user !\n\n» revert back to your real user account to use this bot." ) try: - ubot = (await user.get_me()).id + ubot = me_user.id b = await c.get_chat_member(chat_id, ubot) if b.status == "kicked": await c.unban_chat_member(chat_id, ubot) diff --git a/program/punishment.py b/program/punishment.py index d8d964f..346e429 100644 --- a/program/punishment.py +++ b/program/punishment.py @@ -6,6 +6,7 @@ import asyncio from pyrogram import Client, filters from pyrogram.types import Message from pyrogram.errors import FloodWait +from driver.core import me_bot from driver.filters import command, other_filters from driver.decorators import bot_creator from driver.database.dbchat import get_served_chats @@ -17,7 +18,7 @@ from config import SUDO_USERS, BOT_USERNAME as bn @Client.on_message(command(["gban", f"gban@{bn}"]) & other_filters) @bot_creator async def global_banned(c: Client, message: Message): - BOT_NAME = (await c.get_me()).first_name + BOT_NAME = me_bot.first_name if not message.reply_to_message: if len(message.command) < 2: await message.reply_text("**usage:**\n\n/gban [username | user_id]") @@ -27,7 +28,7 @@ async def global_banned(c: Client, message: Message): user = user.replace("@", "") user = await c.get_users(user) from_user = message.from_user - BOT_ID = await c.get_me() + BOT_ID = me_bot.id if user.id == from_user.id: return await message.reply_text( "You can't gban yourself !" @@ -76,7 +77,7 @@ async def global_banned(c: Client, message: Message): from_user_mention = message.from_user.mention user_id = message.reply_to_message.from_user.id mention = message.reply_to_message.from_user.mention - BOT_ID = await c.get_me() + BOT_ID = me_bot.id if user_id == from_user_id: await message.reply_text("You can't gban yourself !") elif user_id == BOT_ID: @@ -140,7 +141,7 @@ async def ungban_global(c: Client, message: Message): user = user.replace("@", "") user = await c.get_users(user) from_user = message.from_user - BOT_ID = await c.get_me() + BOT_ID = me_bot.id if user.id == from_user.id: await message.reply_text("You can't ungban yourself because you can't be gbanned !") elif user.id == BOT_ID: @@ -159,7 +160,7 @@ async def ungban_global(c: Client, message: Message): from_user_id = message.from_user.id user_id = message.reply_to_message.from_user.id mention = message.reply_to_message.from_user.mention - BOT_ID = await c.get_me() + BOT_ID = me_bot.id if user_id == from_user_id: await message.reply_text("You can't ungban yourself because you can't be gbanned !") elif user_id == BOT_ID: diff --git a/program/start.py b/program/start.py index a8e5c49..59c37f7 100644 --- a/program/start.py +++ b/program/start.py @@ -14,7 +14,7 @@ from config import ( ) from driver.decorators import check_blacklist from program import __version__ -from driver.core import user, bot, me +from driver.core import bot, me_bot, me_user from driver.filters import command, other_filters from driver.database.dbchat import add_served_chat, is_served_chat from driver.database.dbpunish import is_gbanned_user @@ -59,7 +59,7 @@ async def _human_time_duration(seconds): ) @check_blacklist() async def start_(c: Client, message: Message): - BOT_NAME = me["first_name"] + BOT_NAME = me_bot.first_name await message.reply_text( f"""✨ **Welcome {message.from_user.mention()} !**\n 💭 [{BOT_NAME}](https://t.me/{BOT_USERNAME}) **Is a bot to play music and video in groups, through the Telegram Group video chat!** @@ -109,7 +109,7 @@ async def alive(c: Client, message: Message): current_time = datetime.utcnow() uptime_sec = (current_time - START_TIME).total_seconds() uptime = await _human_time_duration(int(uptime_sec)) - BOT_NAME = (await c.get_me()).first_name + BOT_NAME = me_bot.first_name keyboard = InlineKeyboardMarkup( [ @@ -172,8 +172,8 @@ async def new_chat(c: Client, m: Message): pass else: await add_served_chat(chat_id) - ass_uname = (await user.get_me()).username - bot_id = (await c.get_me()).id + ass_uname = me_user.username + bot_id = me_bot.id for member in m.new_chat_members: if chat_id in await blacklisted_chats(): await m.reply( diff --git a/program/userbot_tools.py b/program/userbot_tools.py index 4d263ca..baa265c 100644 --- a/program/userbot_tools.py +++ b/program/userbot_tools.py @@ -2,7 +2,7 @@ import asyncio from config import BOT_USERNAME, SUDO_USERS -from driver.core import user +from driver.core import user, me_bot from driver.filters import command, other_filters from driver.database.dbchat import remove_served_chat from driver.database.dbqueue import remove_active_chat @@ -91,7 +91,7 @@ async def leave_all(client, message): @Client.on_message(filters.left_chat_member) async def bot_kicked(c: Client, m: Message): - bot_id = (await c.get_me()).id + bot_id = me_bot.id chat_id = m.chat.id left_member = m.left_chat_member if left_member.id == bot_id: diff --git a/program/video.py b/program/video.py index 924b4bf..46e8b97 100644 --- a/program/video.py +++ b/program/video.py @@ -14,7 +14,7 @@ from driver.design.thumbnail import thumb 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.core import calls, user, bot, me_user 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 @@ -89,7 +89,7 @@ async def vplay(c: Client, m: Message): "you're an __Anonymous__ user !\n\n» revert back to your real user account to use this bot." ) try: - ubot = (await user.get_me()).id + ubot = me_user.id b = await c.get_chat_member(chat_id, ubot) if b.status == "kicked": await c.unban_chat_member(chat_id, ubot) @@ -357,7 +357,7 @@ async def vstream(c: Client, m: Message): "you're an __Anonymous__ user !\n\n» revert back to your real user account to use this bot." ) try: - ubot = (await user.get_me()).id + ubot = me_user.id b = await c.get_chat_member(chat_id, ubot) if b.status == "kicked": await c.unban_chat_member(chat_id, ubot)