__init__.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. """
  2. MIT License
  3. Copyright (c) 2024 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import asyncio
  21. import time
  22. from inspect import getfullargspec
  23. from os import path
  24. from pathlib import Path
  25. from aiohttp import ClientSession
  26. from motor.motor_asyncio import AsyncIOMotorClient as MongoClient
  27. from pyrogram import Client, filters
  28. from pyrogram.types import Message
  29. from pyromod import listen
  30. from Python_ARQ import ARQ
  31. from telegraph import Telegraph
  32. is_config = path.exists("config.py")
  33. if is_config:
  34. from config import *
  35. else:
  36. from sample_config import *
  37. Path("sessions").mkdir(exist_ok=True)
  38. USERBOT_PREFIX = USERBOT_PREFIX
  39. GBAN_LOG_GROUP_ID = GBAN_LOG_GROUP_ID
  40. WELCOME_DELAY_KICK_SEC = WELCOME_DELAY_KICK_SEC
  41. LOG_GROUP_ID = LOG_GROUP_ID
  42. MESSAGE_DUMP_CHAT = MESSAGE_DUMP_CHAT
  43. MOD_LOAD = []
  44. MOD_NOLOAD = []
  45. SUDOERS = filters.user()
  46. bot_start_time = time.time()
  47. class Log:
  48. def __init__(self, save_to_file=False, file_name="wbb.log"):
  49. self.save_to_file = save_to_file
  50. self.file_name = file_name
  51. def info(self, msg):
  52. print(f"[+]: {msg}")
  53. if self.save_to_file:
  54. with open(self.file_name, "a") as f:
  55. f.write(f"[INFO]({time.ctime(time.time())}): {msg}\n")
  56. def error(self, msg):
  57. print(f"[-]: {msg}")
  58. if self.save_to_file:
  59. with open(self.file_name, "a") as f:
  60. f.write(f"[ERROR]({time.ctime(time.time())}): {msg}\n")
  61. log = Log(True, "bot.log")
  62. # MongoDB client
  63. log.info("Initializing MongoDB client")
  64. mongo_client = MongoClient(MONGO_URL)
  65. db = mongo_client.wbb
  66. aiohttpsession = None
  67. async def initialize():
  68. global aiohttpsession, SUDOERS
  69. aiohttpsession = ClientSession()
  70. log.info("Loading sudoers")
  71. sudoersdb = db.sudoers
  72. sudoers = await sudoersdb.find_one({"sudo": "sudo"})
  73. sudoers = [] if not sudoers else sudoers["sudoers"]
  74. for user_id in SUDO_USERS_ID:
  75. SUDOERS.add(user_id)
  76. if user_id not in sudoers:
  77. sudoers.append(user_id)
  78. await sudoersdb.update_one(
  79. {"sudo": "sudo"},
  80. {"$set": {"sudoers": sudoers}},
  81. upsert=True,
  82. )
  83. if sudoers:
  84. for user_id in sudoers:
  85. SUDOERS.add(user_id)
  86. loop = asyncio.get_event_loop()
  87. loop.run_until_complete(initialize())
  88. if not SESSION_STRING:
  89. app2 = Client(
  90. name="sessions/userbot",
  91. api_id=API_ID,
  92. api_hash=API_HASH,
  93. phone_number=PHONE_NUMBER,
  94. )
  95. else:
  96. app2 = Client(
  97. name="sessions/userbot", api_id=API_ID, api_hash=API_HASH, session_string=SESSION_STRING
  98. )
  99. arq = ARQ(ARQ_API_URL, ARQ_API_KEY, aiohttpsession)
  100. app = Client("sessions/wbb", bot_token=BOT_TOKEN, api_id=API_ID, api_hash=API_HASH)
  101. log.info("Starting bot client")
  102. app.start()
  103. log.info("Starting userbot client")
  104. app2.start()
  105. log.info("Gathering profile info")
  106. x = app.get_me()
  107. y = app2.get_me()
  108. BOT_ID = x.id
  109. BOT_NAME = x.first_name + (x.last_name or "")
  110. BOT_USERNAME = x.username
  111. BOT_MENTION = x.mention
  112. BOT_DC_ID = x.dc_id
  113. USERBOT_ID = y.id
  114. USERBOT_NAME = y.first_name + (y.last_name or "")
  115. USERBOT_USERNAME = y.username
  116. USERBOT_MENTION = y.mention
  117. USERBOT_DC_ID = y.dc_id
  118. if USERBOT_ID not in SUDOERS:
  119. SUDOERS.add(USERBOT_ID)
  120. log.info("Initializing Telegraph client")
  121. telegraph = Telegraph(domain="graph.org")
  122. telegraph.create_account(short_name=BOT_USERNAME)
  123. async def eor(msg: Message, **kwargs):
  124. func = (
  125. (msg.edit_text if msg.from_user.is_self else msg.reply)
  126. if msg.from_user
  127. else msg.reply
  128. )
  129. spec = getfullargspec(func.__wrapped__).args
  130. return await func(**{k: v for k, v in kwargs.items() if k in spec})