__main__.py 12 KB

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