__main__.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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, app2)
  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.dbfunctions import clean_restart_stage
  32. loop = asyncio.get_event_loop()
  33. HELPABLE = {}
  34. async def start_bot():
  35. for module in ALL_MODULES:
  36. imported_module = importlib.import_module(
  37. "wbb.modules." + module
  38. )
  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[
  49. imported_module.__MODULE__.lower()
  50. ] = imported_module
  51. bot_modules = ""
  52. j = 1
  53. for i in ALL_MODULES:
  54. if j == 4:
  55. bot_modules += "|{:<15}|\n".format(i)
  56. j = 0
  57. else:
  58. bot_modules += "|{:<15}".format(i)
  59. j += 1
  60. print(
  61. "+===============================================================+"
  62. )
  63. print(
  64. "| WBB |"
  65. )
  66. print(
  67. "+===============+===============+===============+===============+"
  68. )
  69. print(bot_modules)
  70. print(
  71. "+===============+===============+===============+===============+"
  72. )
  73. print(f"[INFO]: BOT STARTED AS {BOT_NAME}!")
  74. print(f"[INFO]: USERBOT STARTED AS {USERBOT_NAME}!")
  75. restart_data = await clean_restart_stage()
  76. try:
  77. print("[INFO]: SENDING ONLINE STATUS")
  78. if restart_data:
  79. await app.edit_message_text(
  80. restart_data["chat_id"],
  81. restart_data["message_id"],
  82. "**Restarted Successfully**",
  83. )
  84. else:
  85. await app.send_message(LOG_GROUP_ID, "Bot started!")
  86. except Exception:
  87. pass
  88. await idle()
  89. print("[INFO]: STOPPING BOT AND CLOSING AIOHTTP SESSION")
  90. await aiohttpsession.close()
  91. home_keyboard_pm = InlineKeyboardMarkup(
  92. [
  93. [
  94. InlineKeyboardButton(
  95. text="Commands ❓", callback_data="bot_commands"
  96. ),
  97. InlineKeyboardButton(
  98. text="Repo 🛠",
  99. url="https://github.com/thehamkercat/WilliamButcherBot",
  100. ),
  101. ],
  102. [
  103. InlineKeyboardButton(
  104. text="System Stats 🖥",
  105. callback_data="stats_callback",
  106. ),
  107. InlineKeyboardButton(
  108. text="Support 👨", url="http://t.me/WBBSupport"
  109. ),
  110. ],
  111. [
  112. InlineKeyboardButton(
  113. text="Add Me To Your Group 🎉",
  114. url=f"http://t.me/{BOT_USERNAME}?startgroup=new",
  115. )
  116. ],
  117. ]
  118. )
  119. home_text_pm = (
  120. f"Hey there! My name is {BOT_NAME}. I can manage your "
  121. + "group with lots of useful features, feel free to "
  122. + "add me to your group."
  123. )
  124. @app.on_message(filters.command(["help", "start"]))
  125. async def help_command(_, message):
  126. if message.chat.type != "private":
  127. keyboard = InlineKeyboardMarkup(
  128. [
  129. [
  130. InlineKeyboardButton(
  131. text="Help ❓",
  132. url=f"t.me/{BOT_USERNAME}?start=help",
  133. ),
  134. InlineKeyboardButton(
  135. text="Repo 🛠",
  136. url="https://github.com/thehamkercat/WilliamButcherBot",
  137. ),
  138. ],
  139. [
  140. InlineKeyboardButton(
  141. text="System Stats 💻",
  142. callback_data="stats_callback",
  143. ),
  144. InlineKeyboardButton(
  145. text="Support 👨", url="t.me/WBBSupport"
  146. ),
  147. ],
  148. ]
  149. )
  150. return await message.reply(
  151. "Pm Me For More Details.", reply_markup=keyboard
  152. )
  153. await message.reply(
  154. home_text_pm,
  155. reply_markup=home_keyboard_pm,
  156. )
  157. async def help_parser(name, keyboard=None):
  158. if not keyboard:
  159. keyboard = InlineKeyboardMarkup(
  160. paginate_modules(0, HELPABLE, "help")
  161. )
  162. return (
  163. """Hello {first_name}, My name is {bot_name}.
  164. I'm a group management bot with some useful features.
  165. You can choose an option below, by clicking a button.
  166. Also you can ask anything in Support Group.
  167. """.format(
  168. first_name=name,
  169. bot_name=BOT_NAME,
  170. ),
  171. keyboard,
  172. )
  173. @app.on_callback_query(filters.regex("bot_commands"))
  174. async def commands_callbacc(_, CallbackQuery):
  175. text, keyboard = await help_parser(
  176. CallbackQuery.from_user.mention
  177. )
  178. await app.send_message(
  179. CallbackQuery.message.chat.id,
  180. text=text,
  181. reply_markup=keyboard,
  182. )
  183. await CallbackQuery.message.delete()
  184. @app.on_callback_query(filters.regex("stats_callback"))
  185. async def stats_callbacc(_, CallbackQuery):
  186. text = await bot_sys_stats()
  187. await app.answer_callback_query(
  188. CallbackQuery.id, text, show_alert=True
  189. )
  190. @app.on_callback_query(filters.regex(r"help_(.*?)"))
  191. async def help_button(client, query):
  192. home_match = re.match(r"help_home\((.+?)\)", query.data)
  193. mod_match = re.match(r"help_module\((.+?)\)", query.data)
  194. prev_match = re.match(r"help_prev\((.+?)\)", query.data)
  195. next_match = re.match(r"help_next\((.+?)\)", query.data)
  196. back_match = re.match(r"help_back", query.data)
  197. create_match = re.match(r"help_create", query.data)
  198. top_text = f"""
  199. Hello {query.from_user.first_name}, My name is {BOT_NAME}.
  200. I'm a group management bot with some usefule features.
  201. You can choose an option below, by clicking a button.
  202. Also you can ask anything in Support Group.
  203. General command are:
  204. - /start: Start the bot
  205. - /help: Give this message
  206. """
  207. if mod_match:
  208. module = mod_match.group(1)
  209. text = (
  210. "{} **{}**:\n".format(
  211. "Here is the help for", HELPABLE[module].__MODULE__
  212. )
  213. + HELPABLE[module].__HELP__
  214. )
  215. await query.message.edit(
  216. text=text,
  217. reply_markup=InlineKeyboardMarkup(
  218. [
  219. [
  220. InlineKeyboardButton(
  221. "back", callback_data="help_back"
  222. )
  223. ]
  224. ]
  225. ),
  226. disable_web_page_preview=True,
  227. )
  228. elif home_match:
  229. await app.send_message(
  230. query.from_user.id,
  231. text=home_text_pm,
  232. reply_markup=home_keyboard_pm,
  233. )
  234. await query.message.delete()
  235. elif prev_match:
  236. curr_page = int(prev_match.group(1))
  237. await query.message.edit(
  238. text=top_text,
  239. reply_markup=InlineKeyboardMarkup(
  240. paginate_modules(curr_page - 1, HELPABLE, "help")
  241. ),
  242. disable_web_page_preview=True,
  243. )
  244. elif next_match:
  245. next_page = int(next_match.group(1))
  246. await query.message.edit(
  247. text=top_text,
  248. reply_markup=InlineKeyboardMarkup(
  249. paginate_modules(next_page + 1, HELPABLE, "help")
  250. ),
  251. disable_web_page_preview=True,
  252. )
  253. elif back_match:
  254. await query.message.edit(
  255. text=top_text,
  256. reply_markup=InlineKeyboardMarkup(
  257. paginate_modules(0, HELPABLE, "help")
  258. ),
  259. disable_web_page_preview=True,
  260. )
  261. elif create_match:
  262. text, keyboard = await help_parser(query)
  263. await query.message.edit(
  264. text=text,
  265. reply_markup=keyboard,
  266. disable_web_page_preview=True,
  267. )
  268. return await client.answer_callback_query(query.id)
  269. if __name__ == "__main__":
  270. uvloop.install()
  271. loop.run_until_complete(start_bot())