__main__.py 12 KB

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