__init__.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # flake8: noqa F405
  2. """
  3. MIT License
  4. Copyright (c) 2021 TheHamkerCat
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. """
  21. print("[INFO]: INITIALIZING")
  22. import asyncio
  23. import logging
  24. import time
  25. from inspect import getfullargspec
  26. from os import path
  27. from aiohttp import ClientSession
  28. from motor.motor_asyncio import AsyncIOMotorClient as MongoClient
  29. from pyrogram import Client
  30. from pyrogram.types import Message
  31. from pyromod import listen
  32. from Python_ARQ import ARQ
  33. # Setup logging
  34. log_file = "error.log"
  35. with open(log_file, "w") as f:
  36. f.write("PEAK OF LOG FILE")
  37. logging.basicConfig(
  38. level=logging.ERROR,
  39. format="[%(asctime)s.%(msecs)03d] %(filename)s:%(lineno)s %(levelname)s: %(message)s",
  40. datefmt="%m-%d %H:%M",
  41. filename=log_file,
  42. filemode="w",
  43. )
  44. console = logging.StreamHandler()
  45. logging.getLogger("").addHandler(console)
  46. log = logging.getLogger()
  47. is_config = path.exists("config.py")
  48. if is_config:
  49. from config import *
  50. else:
  51. from sample_config import *
  52. USERBOT_PREFIX = USERBOT_PREFIX
  53. GBAN_LOG_GROUP_ID = GBAN_LOG_GROUP_ID
  54. SUDOERS = SUDO_USERS_ID
  55. WELCOME_DELAY_KICK_SEC = WELCOME_DELAY_KICK_SEC
  56. LOG_GROUP_ID = LOG_GROUP_ID
  57. MESSAGE_DUMP_CHAT = MESSAGE_DUMP_CHAT
  58. MOD_LOAD = []
  59. MOD_NOLOAD = []
  60. bot_start_time = time.time()
  61. # MongoDB client
  62. print("[INFO]: INITIALIZING DATABASE")
  63. mongo_client = MongoClient(MONGO_DB_URI)
  64. db = mongo_client.wbb
  65. async def load_sudoers():
  66. global SUDOERS
  67. print("[INFO]: LOADING SUDOERS")
  68. sudoersdb = db.sudoers
  69. sudoers = await sudoersdb.find_one({"sudo": "sudo"})
  70. sudoers = [] if not sudoers else sudoers["sudoers"]
  71. for user_id in SUDOERS:
  72. if user_id not in sudoers:
  73. sudoers.append(user_id)
  74. await sudoersdb.update_one(
  75. {"sudo": "sudo"},
  76. {"$set": {"sudoers": sudoers}},
  77. upsert=True,
  78. )
  79. SUDOERS = (SUDOERS + sudoers) if sudoers else SUDOERS
  80. print("[INFO]: LOADED SUDOERS")
  81. loop = asyncio.get_event_loop()
  82. loop.run_until_complete(load_sudoers())
  83. if not HEROKU:
  84. print("[INFO]: INITIALIZING USERBOT CLIENT")
  85. app2 = Client(
  86. "userbot",
  87. phone_number=PHONE_NUMBER,
  88. api_id=API_ID,
  89. api_hash=API_HASH,
  90. )
  91. else:
  92. print("[INFO]: INITIALIZING USERBOT CLIENT")
  93. app2 = Client(SESSION_STRING, api_id=API_ID, api_hash=API_HASH)
  94. # Aiohttp Client
  95. print("[INFO]: INITIALZING AIOHTTP SESSION")
  96. aiohttpsession = ClientSession()
  97. # ARQ Client
  98. print("[INFO]: INITIALIZING ARQ CLIENT")
  99. arq = ARQ(ARQ_API_URL, ARQ_API_KEY, aiohttpsession)
  100. # Bot client
  101. print("[INFO]: INITIALIZING BOT CLIENT")
  102. app = Client(
  103. "wbb", bot_token=BOT_TOKEN, api_id=API_ID, api_hash=API_HASH
  104. )
  105. BOT_ID = 0
  106. BOT_NAME = ""
  107. BOT_USERNAME = ""
  108. BOT_MENTION = ""
  109. BOT_DC_ID = 0
  110. USERBOT_ID = 0
  111. USERBOT_NAME = ""
  112. USERBOT_USERNAME = ""
  113. USERBOT_DC_ID = 0
  114. USERBOT_MENTION = ""
  115. USERBOT_BOT_CHAT_COMMON = []
  116. def get_info(app, app2):
  117. global BOT_ID, BOT_NAME, BOT_USERNAME, BOT_DC_ID, BOT_MENTION
  118. global USERBOT_ID, USERBOT_NAME, USERBOT_USERNAME, USERBOT_DC_ID, USERBOT_MENTION
  119. global USERBOT_BOT_CHAT_COMMON
  120. getme = app.get_me()
  121. getme2 = app2.get_me()
  122. BOT_ID = getme.id
  123. USERBOT_ID = getme2.id
  124. BOT_NAME = (
  125. f"{getme.first_name} {getme.last_name}"
  126. if getme.last_name
  127. else getme.first_name
  128. )
  129. BOT_USERNAME = getme.username
  130. BOT_MENTION = getme.mention
  131. BOT_DC_ID = getme.dc_id
  132. USERBOT_NAME = (
  133. f"{getme2.first_name} {getme2.last_name}"
  134. if getme2.last_name
  135. else getme2.first_name
  136. )
  137. USERBOT_USERNAME = getme2.username
  138. USERBOT_MENTION = getme2.mention
  139. USERBOT_DC_ID = getme2.dc_id
  140. print("[INFO]: STARTING BOT CLIENT TEMPORARILY")
  141. app.start()
  142. print("[INFO]: STARTING USERBOT CLIENT TEMPORARILY")
  143. app2.start()
  144. print("[INFO]: LOADING UB/BOT PROFILE INFO")
  145. get_info(app, app2)
  146. print("[INFO]: LOADED UB/BOT PROFILE INFO")
  147. if USERBOT_ID not in SUDOERS:
  148. SUDOERS.append(USERBOT_ID)
  149. app.stop()
  150. app2.stop()
  151. async def eor(msg: Message, **kwargs):
  152. func = msg.edit_text if msg.from_user.is_self else msg.reply
  153. spec = getfullargspec(func.__wrapped__).args
  154. return await func(
  155. **{k: v for k, v in kwargs.items() if k in spec}
  156. )