__main__.py 9.1 KB

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