__main__.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import asyncio
  2. import re
  3. import importlib
  4. import time
  5. import uvloop
  6. from pyrogram import filters, idle
  7. from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
  8. from wbb import app, MOD_NOLOAD
  9. from wbb.utils import get_info, paginate_modules
  10. from wbb.utils import botinfo, formatter
  11. from wbb.modules.sudoers import bot_sys_stats
  12. from wbb.modules import ALL_MODULES
  13. from wbb import bot_start_time
  14. loop = asyncio.get_event_loop()
  15. HELPABLE = {}
  16. async def start_bot():
  17. await app.start()
  18. await get_info(app)
  19. for module in ALL_MODULES:
  20. imported_module = importlib.import_module("wbb.modules." + module)
  21. if (
  22. hasattr(imported_module, "__MODULE__")
  23. and imported_module.__MODULE__
  24. ):
  25. if imported_module.__MODULE__.lower() in MOD_NOLOAD:
  26. continue
  27. imported_module.__MODULE__ = imported_module.__MODULE__
  28. if (
  29. hasattr(imported_module, "__HELP__")
  30. and imported_module.__HELP__
  31. ):
  32. HELPABLE[
  33. imported_module.__MODULE__.lower()
  34. ] = imported_module
  35. bot_modules = ""
  36. j = 1
  37. for i in ALL_MODULES:
  38. if j == 4:
  39. bot_modules += "|{:<15}|\n".format(i)
  40. j = 0
  41. else:
  42. bot_modules += "|{:<15}".format(i)
  43. j += 1
  44. print("+===============================================================+")
  45. print("| WBB - Modules |")
  46. print("+===============+===============+===============+===============+")
  47. print(bot_modules)
  48. print("+===============+===============+===============+===============+")
  49. print("Bot Started Successfully as {}!".format(botinfo.BOT_NAME))
  50. await idle()
  51. @app.on_message(filters.command("start"))
  52. async def start(_, message):
  53. bot_uptime = int(time.time() - bot_start_time)
  54. await message.reply_text(
  55. f"Already Online Since {formatter.get_readable_time((bot_uptime))},"
  56. " Maybe Try /help")
  57. @app.on_message(filters.command("help"))
  58. async def help_command(_, message):
  59. if message.chat.type != "private":
  60. if len(message.command) >= 2 and message.command[1] == "help":
  61. text, keyboard = await help_parser(message)
  62. await message.reply(
  63. text, reply_markup=keyboard, disable_web_page_preview=True
  64. )
  65. return
  66. keyboard = InlineKeyboardMarkup(
  67. [
  68. [
  69. InlineKeyboardButton(
  70. text="Help",
  71. url=f"t.me/{botinfo.BOT_USERNAME}?start=help",
  72. ),
  73. InlineKeyboardButton(
  74. text="Repo",
  75. url="https://github.com/thehamkercat/WilliamButcherBot",
  76. )
  77. ],
  78. [
  79. InlineKeyboardButton(
  80. text="System Stats",
  81. callback_data="stats_callback"
  82. ),
  83. InlineKeyboardButton(
  84. text="Support",
  85. url="t.me/PatheticProgrammers"
  86. )
  87. ]
  88. ]
  89. )
  90. await message.reply("Pm Me For More Details.", reply_markup=keyboard)
  91. return
  92. text, keyboard = await help_parser(message)
  93. await message.reply_photo(
  94. caption=text,
  95. photo="https://static2.aniimg.com/upload/20170515/414/c/d/7/cd7EEF.jpg",
  96. reply_markup=keyboard
  97. )
  98. async def help_parser(message, keyboard=None):
  99. if not keyboard:
  100. keyboard = InlineKeyboardMarkup(paginate_modules(0, HELPABLE, "help"))
  101. return (
  102. "Hi {first_name}, I am {bot_name}, A well written telegram group manager bot, Invite me to your chat.".format(
  103. first_name=message.from_user.first_name,
  104. bot_name=botinfo.BOT_NAME,
  105. ),
  106. keyboard,
  107. )
  108. @app.on_callback_query(filters.regex("stats_callback"))
  109. async def end_callbacc(_, CallbackQuery):
  110. text = await bot_sys_stats()
  111. await app.answer_callback_query(CallbackQuery.id, text, show_alert=True)
  112. @app.on_callback_query(filters.regex(r"help_(.*?)"))
  113. async def help_button(client, query):
  114. mod_match = re.match(r"help_module\((.+?)\)", query.data)
  115. prev_match = re.match(r"help_prev\((.+?)\)", query.data)
  116. next_match = re.match(r"help_next\((.+?)\)", query.data)
  117. back_match = re.match(r"help_back", query.data)
  118. create_match = re.match(r"help_create", query.data)
  119. if mod_match:
  120. module = mod_match.group(1)
  121. text = (
  122. "{} **{}**:\n".format(
  123. "Here is the help for", HELPABLE[module].__MODULE__
  124. )
  125. + HELPABLE[module].__HELP__
  126. )
  127. await query.message.edit(
  128. text=text,
  129. reply_markup=InlineKeyboardMarkup(
  130. [[InlineKeyboardButton("back", callback_data="help_back")]]
  131. ),
  132. disable_web_page_preview=True,
  133. )
  134. elif prev_match:
  135. curr_page = int(prev_match.group(1))
  136. await query.message.edit(
  137. text="Hi {first_name}. I am {bot_name}".format(
  138. first_name=query.from_user.first_name,
  139. bot_name=botinfo.BOT_NAME,
  140. ),
  141. reply_markup=InlineKeyboardMarkup(
  142. paginate_modules(curr_page - 1, HELPABLE, "help")
  143. ),
  144. disable_web_page_preview=True,
  145. )
  146. elif next_match:
  147. next_page = int(next_match.group(1))
  148. await query.message.edit(
  149. text="Hi {first_name}. I am {bot_name}".format(
  150. first_name=query.from_user.first_name,
  151. bot_name=botinfo.BOT_NAME,
  152. ),
  153. reply_markup=InlineKeyboardMarkup(
  154. paginate_modules(next_page + 1, HELPABLE, "help")
  155. ),
  156. disable_web_page_preview=True,
  157. )
  158. elif back_match:
  159. await query.message.edit(
  160. text="Hi {first_name}. I am {bot_name}".format(
  161. first_name=query.from_user.first_name,
  162. bot_name=botinfo.BOT_NAME,
  163. ),
  164. reply_markup=InlineKeyboardMarkup(
  165. paginate_modules(0, HELPABLE, "help")
  166. ),
  167. disable_web_page_preview=True,
  168. )
  169. elif create_match:
  170. text, keyboard = await help_parser(query)
  171. await query.message.edit(
  172. text=text, reply_markup=keyboard, disable_web_page_preview=True
  173. )
  174. return await client.answer_callback_query(query.id)
  175. if __name__ == "__main__":
  176. uvloop.install()
  177. loop.run_until_complete(start_bot())