__main__.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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, USERBOT_NAME, app
  27. from wbb.modules import ALL_MODULES
  28. from wbb.modules.sudoers import bot_sys_stats
  29. from wbb.utils import paginate_modules
  30. loop = asyncio.get_event_loop()
  31. HELPABLE = {}
  32. async def start_bot():
  33. global COMMANDS_COUNT
  34. for module in ALL_MODULES:
  35. imported_module = importlib.import_module("wbb.modules." + module)
  36. if hasattr(imported_module, "__MODULE__") and imported_module.__MODULE__:
  37. imported_module.__MODULE__ = imported_module.__MODULE__
  38. if hasattr(imported_module, "__HELP__") and imported_module.__HELP__:
  39. HELPABLE[imported_module.__MODULE__.lower()] = imported_module
  40. bot_modules = ""
  41. j = 1
  42. for i in ALL_MODULES:
  43. if j == 4:
  44. bot_modules += "|{:<15}|\n".format(i)
  45. j = 0
  46. else:
  47. bot_modules += "|{:<15}".format(i)
  48. j += 1
  49. print("+===============================================================+")
  50. print("| WBB |")
  51. print("+===============+===============+===============+===============+")
  52. print(bot_modules)
  53. print("+===============+===============+===============+===============+")
  54. print(f"[INFO]: BOT STARTED AS {BOT_NAME}!")
  55. print(f"[INFO]: USERBOT STARTED AS {USERBOT_NAME}!")
  56. await idle()
  57. @app.on_message(filters.command(["help", "start"]))
  58. async def help_command(_, message):
  59. if message.chat.type != "private":
  60. keyboard = InlineKeyboardMarkup(
  61. [
  62. [
  63. InlineKeyboardButton(
  64. text="Help ❓",
  65. url=f"t.me/{BOT_USERNAME}?start=help",
  66. ),
  67. InlineKeyboardButton(
  68. text="Repo 🛠",
  69. url="https://github.com/thehamkercat/WilliamButcherBot",
  70. ),
  71. ],
  72. [
  73. InlineKeyboardButton(
  74. text="System Stats 💻", callback_data="stats_callback"
  75. ),
  76. InlineKeyboardButton(text="Support 👨", url="t.me/WBBSupport"),
  77. ],
  78. ]
  79. )
  80. await message.reply("Pm Me For More Details.", reply_markup=keyboard)
  81. return
  82. keyboard = InlineKeyboardMarkup(
  83. [
  84. [
  85. InlineKeyboardButton(text="Commands ❓", callback_data="bot_commands"),
  86. InlineKeyboardButton(
  87. text="Repo 🛠",
  88. url="https://github.com/thehamkercat/WilliamButcherBot",
  89. ),
  90. ],
  91. [
  92. InlineKeyboardButton(
  93. text="System Stats 🖥", callback_data="stats_callback"
  94. ),
  95. InlineKeyboardButton(text="Support 👨", url="t.me/WBBSupport"),
  96. ],
  97. [
  98. InlineKeyboardButton(
  99. text="Add Me To Your Group 🎉",
  100. url=f"http://t.me/{BOT_USERNAME}?startgroup=new",
  101. )
  102. ],
  103. ]
  104. )
  105. await message.reply(
  106. 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.",
  107. reply_markup=keyboard,
  108. )
  109. async def help_parser(name, keyboard=None):
  110. if not keyboard:
  111. keyboard = InlineKeyboardMarkup(paginate_modules(0, HELPABLE, "help"))
  112. return (
  113. """Hello {first_name}! My name is {bot_name}!
  114. I'm a group management bot with some usefule features.
  115. You can choose an option below, by clicking a button.
  116. Also you can ask anything in Support Group.
  117. General command are:
  118. - /start: Start the bot
  119. - /help: Give this message""".format(
  120. first_name=name,
  121. bot_name=BOT_NAME,
  122. ),
  123. keyboard,
  124. )
  125. @app.on_callback_query(filters.regex("bot_commands"))
  126. async def commands_callbacc(_, CallbackQuery):
  127. text, keyboard = await help_parser(CallbackQuery.from_user.mention)
  128. await app.send_message(
  129. CallbackQuery.message.chat.id, text=text, reply_markup=keyboard
  130. )
  131. await CallbackQuery.message.delete()
  132. @app.on_callback_query(filters.regex("stats_callback"))
  133. async def stats_callbacc(_, CallbackQuery):
  134. text = await bot_sys_stats()
  135. await app.answer_callback_query(CallbackQuery.id, text, show_alert=True)
  136. @app.on_callback_query(filters.regex(r"help_(.*?)"))
  137. async def help_button(client, query):
  138. mod_match = re.match(r"help_module\((.+?)\)", query.data)
  139. prev_match = re.match(r"help_prev\((.+?)\)", query.data)
  140. next_match = re.match(r"help_next\((.+?)\)", query.data)
  141. back_match = re.match(r"help_back", query.data)
  142. create_match = re.match(r"help_create", query.data)
  143. top_text = f"""
  144. Hello {query.from_user.first_name}! My name is {BOT_NAME}!
  145. I'm a group management bot with some usefule features.
  146. You can choose an option below, by clicking a button.
  147. Also you can ask anything in Support Group.
  148. General command are:
  149. - /start: Start the bot
  150. - /help: Give this message
  151. """
  152. if mod_match:
  153. module = mod_match.group(1)
  154. text = (
  155. "{} **{}**:\n".format("Here is the help for", HELPABLE[module].__MODULE__)
  156. + HELPABLE[module].__HELP__
  157. )
  158. await query.message.edit(
  159. text=text,
  160. reply_markup=InlineKeyboardMarkup(
  161. [[InlineKeyboardButton("back", callback_data="help_back")]]
  162. ),
  163. disable_web_page_preview=True,
  164. )
  165. elif prev_match:
  166. curr_page = int(prev_match.group(1))
  167. await query.message.edit(
  168. text=top_text,
  169. reply_markup=InlineKeyboardMarkup(
  170. paginate_modules(curr_page - 1, HELPABLE, "help")
  171. ),
  172. disable_web_page_preview=True,
  173. )
  174. elif next_match:
  175. next_page = int(next_match.group(1))
  176. await query.message.edit(
  177. text=top_text,
  178. reply_markup=InlineKeyboardMarkup(
  179. paginate_modules(next_page + 1, HELPABLE, "help")
  180. ),
  181. disable_web_page_preview=True,
  182. )
  183. elif back_match:
  184. await query.message.edit(
  185. text=top_text,
  186. reply_markup=InlineKeyboardMarkup(paginate_modules(0, HELPABLE, "help")),
  187. disable_web_page_preview=True,
  188. )
  189. elif create_match:
  190. text, keyboard = await help_parser(query)
  191. await query.message.edit(
  192. text=text, reply_markup=keyboard, disable_web_page_preview=True
  193. )
  194. return await client.answer_callback_query(query.id)
  195. if __name__ == "__main__":
  196. uvloop.install()
  197. loop.run_until_complete(start_bot())