__main__.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. """
  2. MIT License
  3. Copyright (c) 2023 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 importlib
  22. import re
  23. from contextlib import closing, suppress
  24. from pyrogram import filters, idle
  25. from pyrogram.enums import ChatType, ParseMode
  26. from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
  27. from uvloop import install
  28. from wbb import (
  29. BOT_NAME,
  30. BOT_USERNAME,
  31. LOG_GROUP_ID,
  32. USERBOT_NAME,
  33. aiohttpsession,
  34. app,
  35. log,
  36. )
  37. from wbb.modules import ALL_MODULES
  38. from wbb.modules.sudoers import bot_sys_stats
  39. from wbb.utils import paginate_modules
  40. from wbb.utils.constants import MARKDOWN
  41. from wbb.utils.dbfunctions import clean_restart_stage
  42. loop = asyncio.get_event_loop()
  43. HELPABLE = {}
  44. async def start_bot():
  45. global HELPABLE
  46. for module in ALL_MODULES:
  47. imported_module = importlib.import_module("wbb.modules." + module)
  48. if (
  49. hasattr(imported_module, "__MODULE__")
  50. and imported_module.__MODULE__
  51. ):
  52. imported_module.__MODULE__ = imported_module.__MODULE__
  53. if (
  54. hasattr(imported_module, "__HELP__")
  55. and imported_module.__HELP__
  56. ):
  57. HELPABLE[
  58. imported_module.__MODULE__.replace(" ", "_").lower()
  59. ] = imported_module
  60. bot_modules = ""
  61. j = 1
  62. for i in ALL_MODULES:
  63. if j == 4:
  64. bot_modules += "|{:<15}|\n".format(i)
  65. j = 0
  66. else:
  67. bot_modules += "|{:<15}".format(i)
  68. j += 1
  69. print("+===============================================================+")
  70. print("| WBB |")
  71. print("+===============+===============+===============+===============+")
  72. print(bot_modules)
  73. print("+===============+===============+===============+===============+")
  74. log.info(f"BOT STARTED AS {BOT_NAME}!")
  75. log.info(f"USERBOT STARTED AS {USERBOT_NAME}!")
  76. restart_data = await clean_restart_stage()
  77. try:
  78. log.info("Sending online status")
  79. if restart_data:
  80. await app.edit_message_text(
  81. restart_data["chat_id"],
  82. restart_data["message_id"],
  83. "**Restarted Successfully**",
  84. )
  85. else:
  86. await app.send_message(LOG_GROUP_ID, "Bot started!")
  87. except Exception:
  88. pass
  89. await idle()
  90. await aiohttpsession.close()
  91. log.info("Stopping clients")
  92. await app.stop()
  93. log.info("Cancelling asyncio tasks")
  94. for task in asyncio.all_tasks():
  95. task.cancel()
  96. log.info("Dead!")
  97. home_keyboard_pm = InlineKeyboardMarkup(
  98. [
  99. [
  100. InlineKeyboardButton(
  101. text="Commands ❓", callback_data="bot_commands"
  102. ),
  103. InlineKeyboardButton(
  104. text="Repo 🛠",
  105. url="https://github.com/thehamkercat/WilliamButcherBot",
  106. ),
  107. ],
  108. [
  109. InlineKeyboardButton(
  110. text="System Stats 🖥",
  111. callback_data="stats_callback",
  112. ),
  113. InlineKeyboardButton(
  114. text="Support 👨", url="http://t.me/WBBSupport"
  115. ),
  116. ],
  117. [
  118. InlineKeyboardButton(
  119. text="Add Me To Your Group 🎉",
  120. url=f"http://t.me/{BOT_USERNAME}?startgroup=new",
  121. )
  122. ],
  123. ]
  124. )
  125. home_text_pm = (
  126. f"Hey there! My name is {BOT_NAME}. I can manage your "
  127. + "group with lots of useful features, feel free to "
  128. + "add me to your group."
  129. )
  130. keyboard = InlineKeyboardMarkup(
  131. [
  132. [
  133. InlineKeyboardButton(
  134. text="Help ❓",
  135. url=f"t.me/{BOT_USERNAME}?start=help",
  136. ),
  137. InlineKeyboardButton(
  138. text="Repo 🛠",
  139. url="https://github.com/thehamkercat/WilliamButcherBot",
  140. ),
  141. ],
  142. [
  143. InlineKeyboardButton(
  144. text="System Stats 💻",
  145. callback_data="stats_callback",
  146. ),
  147. InlineKeyboardButton(text="Support 👨", url="t.me/WBBSupport"),
  148. ],
  149. ]
  150. )
  151. @app.on_message(filters.command("start"))
  152. async def start(_, message):
  153. if message.chat.type != ChatType.PRIVATE:
  154. return await message.reply(
  155. "Pm Me For More Details.", reply_markup=keyboard
  156. )
  157. if len(message.text.split()) > 1:
  158. name = (message.text.split(None, 1)[1]).lower()
  159. if name == "mkdwn_help":
  160. await message.reply(
  161. MARKDOWN,
  162. parse_mode=ParseMode.HTML,
  163. disable_web_page_preview=True,
  164. )
  165. elif "_" in name:
  166. module = name.split("_", 1)[1]
  167. text = (
  168. f"Here is the help for **{HELPABLE[module].__MODULE__}**:\n"
  169. + HELPABLE[module].__HELP__
  170. )
  171. await message.reply(text, disable_web_page_preview=True)
  172. elif name == "help":
  173. text, keyb = await help_parser(message.from_user.first_name)
  174. await message.reply(
  175. text,
  176. reply_markup=keyb,
  177. )
  178. else:
  179. await message.reply(
  180. home_text_pm,
  181. reply_markup=home_keyboard_pm,
  182. )
  183. return
  184. @app.on_message(filters.command("help"))
  185. async def help_command(_, message):
  186. if message.chat.type != ChatType.PRIVATE:
  187. if len(message.command) >= 2:
  188. name = (message.text.split(None, 1)[1]).replace(" ", "_").lower()
  189. if str(name) in HELPABLE:
  190. key = InlineKeyboardMarkup(
  191. [
  192. [
  193. InlineKeyboardButton(
  194. text="Click here",
  195. url=f"t.me/{BOT_USERNAME}?start=help_{name}",
  196. )
  197. ],
  198. ]
  199. )
  200. await message.reply(
  201. f"Click on the below button to get help about {name}",
  202. reply_markup=key,
  203. )
  204. else:
  205. await message.reply(
  206. "PM Me For More Details.", reply_markup=keyboard
  207. )
  208. else:
  209. await message.reply(
  210. "Pm Me For More Details.", reply_markup=keyboard
  211. )
  212. else:
  213. if len(message.command) >= 2:
  214. name = (message.text.split(None, 1)[1]).replace(" ", "_").lower()
  215. if str(name) in HELPABLE:
  216. text = (
  217. f"Here is the help for **{HELPABLE[name].__MODULE__}**:\n"
  218. + HELPABLE[name].__HELP__
  219. )
  220. await message.reply(text, disable_web_page_preview=True)
  221. else:
  222. text, help_keyboard = await help_parser(
  223. message.from_user.first_name
  224. )
  225. await message.reply(
  226. text,
  227. reply_markup=help_keyboard,
  228. disable_web_page_preview=True,
  229. )
  230. else:
  231. text, help_keyboard = await help_parser(
  232. message.from_user.first_name
  233. )
  234. await message.reply(
  235. text, reply_markup=help_keyboard, disable_web_page_preview=True
  236. )
  237. return
  238. async def help_parser(name, keyboard=None):
  239. if not keyboard:
  240. keyboard = InlineKeyboardMarkup(paginate_modules(0, HELPABLE, "help"))
  241. return (
  242. """Hello {first_name}, My name is {bot_name}.
  243. I'm a group management bot with some useful features.
  244. You can choose an option below, by clicking a button.
  245. Also you can ask anything in Support Group.
  246. """.format(
  247. first_name=name,
  248. bot_name=BOT_NAME,
  249. ),
  250. keyboard,
  251. )
  252. @app.on_callback_query(filters.regex("bot_commands"))
  253. async def commands_callbacc(_, CallbackQuery):
  254. text, keyboard = await help_parser(CallbackQuery.from_user.mention)
  255. await app.send_message(
  256. CallbackQuery.message.chat.id,
  257. text=text,
  258. reply_markup=keyboard,
  259. )
  260. await CallbackQuery.message.delete()
  261. @app.on_callback_query(filters.regex("stats_callback"))
  262. async def stats_callbacc(_, CallbackQuery):
  263. text = await bot_sys_stats()
  264. await app.answer_callback_query(CallbackQuery.id, text, show_alert=True)
  265. @app.on_callback_query(filters.regex(r"help_(.*?)"))
  266. async def help_button(client, query):
  267. home_match = re.match(r"help_home\((.+?)\)", query.data)
  268. mod_match = re.match(r"help_module\((.+?)\)", query.data)
  269. prev_match = re.match(r"help_prev\((.+?)\)", query.data)
  270. next_match = re.match(r"help_next\((.+?)\)", query.data)
  271. back_match = re.match(r"help_back", query.data)
  272. create_match = re.match(r"help_create", query.data)
  273. top_text = f"""
  274. Hello {query.from_user.first_name}, My name is {BOT_NAME}.
  275. I'm a group management bot with some useful features.
  276. You can choose an option below, by clicking a button.
  277. Also you can ask anything in Support Group.
  278. General command are:
  279. - /start: Start the bot
  280. - /help: Give this message
  281. """
  282. if mod_match:
  283. module = (mod_match.group(1)).replace(" ", "_")
  284. text = (
  285. "{} **{}**:\n".format(
  286. "Here is the help for", HELPABLE[module].__MODULE__
  287. )
  288. + HELPABLE[module].__HELP__
  289. )
  290. await query.message.edit(
  291. text=text,
  292. reply_markup=InlineKeyboardMarkup(
  293. [[InlineKeyboardButton("back", callback_data="help_back")]]
  294. ),
  295. disable_web_page_preview=True,
  296. )
  297. elif home_match:
  298. await app.send_message(
  299. query.from_user.id,
  300. text=home_text_pm,
  301. reply_markup=home_keyboard_pm,
  302. )
  303. await query.message.delete()
  304. elif prev_match:
  305. curr_page = int(prev_match.group(1))
  306. await query.message.edit(
  307. text=top_text,
  308. reply_markup=InlineKeyboardMarkup(
  309. paginate_modules(curr_page - 1, HELPABLE, "help")
  310. ),
  311. disable_web_page_preview=True,
  312. )
  313. elif next_match:
  314. next_page = int(next_match.group(1))
  315. await query.message.edit(
  316. text=top_text,
  317. reply_markup=InlineKeyboardMarkup(
  318. paginate_modules(next_page + 1, HELPABLE, "help")
  319. ),
  320. disable_web_page_preview=True,
  321. )
  322. elif back_match:
  323. await query.message.edit(
  324. text=top_text,
  325. reply_markup=InlineKeyboardMarkup(
  326. paginate_modules(0, HELPABLE, "help")
  327. ),
  328. disable_web_page_preview=True,
  329. )
  330. elif create_match:
  331. text, keyboard = await help_parser(query)
  332. await query.message.edit(
  333. text=text,
  334. reply_markup=keyboard,
  335. disable_web_page_preview=True,
  336. )
  337. return await client.answer_callback_query(query.id)
  338. if __name__ == "__main__":
  339. install()
  340. with closing(loop):
  341. with suppress(asyncio.exceptions.CancelledError):
  342. loop.run_until_complete(start_bot())
  343. loop.run_until_complete(asyncio.sleep(3.0)) # task cancel wait