__main__.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. COMMANDS_COUNT = None
  17. async def start_bot():
  18. global COMMANDS_COUNT
  19. await app.start()
  20. await get_info(app)
  21. for module in ALL_MODULES:
  22. imported_module = importlib.import_module("wbb.modules." + module)
  23. if (
  24. hasattr(imported_module, "__MODULE__")
  25. and imported_module.__MODULE__
  26. ):
  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. COMMANDS_COUNT = HELPABLE
  36. bot_modules = ""
  37. j = 1
  38. for i in ALL_MODULES:
  39. if j == 4:
  40. bot_modules += "|{:<15}|\n".format(i)
  41. j = 0
  42. else:
  43. bot_modules += "|{:<15}".format(i)
  44. j += 1
  45. print("+===============================================================+")
  46. print("| WBB - Modules |")
  47. print("+===============+===============+===============+===============+")
  48. print(bot_modules)
  49. print("+===============+===============+===============+===============+")
  50. print("Bot Started Successfully as {}!".format(botinfo.BOT_NAME))
  51. await idle()
  52. @app.on_message(filters.command("start"))
  53. async def start(_, message):
  54. bot_uptime = int(time.time() - bot_start_time)
  55. await message.reply_text(
  56. f"Already Online Since {formatter.get_readable_time((bot_uptime))},"
  57. " Maybe Try /help")
  58. @app.on_message(filters.command("help"))
  59. async def help_command(_, message):
  60. if message.chat.type != "private":
  61. if len(message.command) >= 2 and message.command[1] == "help":
  62. text, keyboard = await help_parser(message)
  63. await message.reply(
  64. text, reply_markup=keyboard, disable_web_page_preview=True
  65. )
  66. return
  67. keyboard = InlineKeyboardMarkup(
  68. [
  69. [
  70. InlineKeyboardButton(
  71. text="Help",
  72. url=f"t.me/{botinfo.BOT_USERNAME}?start=help",
  73. ),
  74. InlineKeyboardButton(
  75. text="Repo",
  76. url="https://github.com/thehamkercat/WilliamButcherBot",
  77. )
  78. ],
  79. [
  80. InlineKeyboardButton(
  81. text="System Stats",
  82. callback_data="stats_callback"
  83. ),
  84. InlineKeyboardButton(
  85. text="Support",
  86. url="t.me/PatheticProgrammers"
  87. )
  88. ]
  89. ]
  90. )
  91. await message.reply("Pm Me For More Details.", reply_markup=keyboard)
  92. return
  93. keyboard = InlineKeyboardMarkup(
  94. [
  95. [
  96. InlineKeyboardButton(
  97. text="Commands",
  98. callback_data="bot_commands"
  99. ),
  100. InlineKeyboardButton(
  101. text="Repo",
  102. url="https://github.com/thehamkercat/WilliamButcherBot"
  103. )
  104. ],
  105. [
  106. InlineKeyboardButton(
  107. text="System Stats",
  108. callback_data="stats_callback"
  109. ),
  110. InlineKeyboardButton(
  111. text="Support",
  112. url="t.me/PatheticProgrammers"
  113. )
  114. ]
  115. ]
  116. )
  117. await message.reply("Hello, Choose An Option From Below.", reply_markup=keyboard)
  118. async def help_parser(name, keyboard=None):
  119. if not keyboard:
  120. keyboard = InlineKeyboardMarkup(paginate_modules(0, HELPABLE, "help"))
  121. return (
  122. "Hi {first_name}, I am {bot_name}, Here is the list of all possible commands:".format(
  123. first_name=name,
  124. bot_name=botinfo.BOT_NAME,
  125. ),
  126. keyboard,
  127. )
  128. @app.on_callback_query(filters.regex("bot_commands"))
  129. async def end_callbacc(_, CallbackQuery):
  130. text, keyboard = await help_parser(CallbackQuery.from_user.mention)
  131. await app.send_message(
  132. CallbackQuery.message.chat.id,
  133. text=text,
  134. reply_markup=keyboard
  135. )
  136. await CallbackQuery.message.delete()
  137. @app.on_callback_query(filters.regex("stats_callback"))
  138. async def end_callbacc(_, CallbackQuery):
  139. text = await bot_sys_stats()
  140. await app.answer_callback_query(CallbackQuery.id, text, show_alert=True)
  141. @app.on_callback_query(filters.regex(r"help_(.*?)"))
  142. async def help_button(client, query):
  143. mod_match = re.match(r"help_module\((.+?)\)", query.data)
  144. prev_match = re.match(r"help_prev\((.+?)\)", query.data)
  145. next_match = re.match(r"help_next\((.+?)\)", query.data)
  146. back_match = re.match(r"help_back", query.data)
  147. create_match = re.match(r"help_create", query.data)
  148. if mod_match:
  149. module = mod_match.group(1)
  150. text = (
  151. "{} **{}**:\n".format(
  152. "Here is the help for", HELPABLE[module].__MODULE__
  153. )
  154. + HELPABLE[module].__HELP__
  155. )
  156. await query.message.edit(
  157. text=text,
  158. reply_markup=InlineKeyboardMarkup(
  159. [[InlineKeyboardButton("back", callback_data="help_back")]]
  160. ),
  161. disable_web_page_preview=True,
  162. )
  163. elif prev_match:
  164. curr_page = int(prev_match.group(1))
  165. await query.message.edit(
  166. text="Hi {first_name}. I am {bot_name}".format(
  167. first_name=query.from_user.first_name,
  168. bot_name=botinfo.BOT_NAME,
  169. ),
  170. reply_markup=InlineKeyboardMarkup(
  171. paginate_modules(curr_page - 1, HELPABLE, "help")
  172. ),
  173. disable_web_page_preview=True,
  174. )
  175. elif next_match:
  176. next_page = int(next_match.group(1))
  177. await query.message.edit(
  178. text="Hi {first_name}. I am {bot_name}".format(
  179. first_name=query.from_user.first_name,
  180. bot_name=botinfo.BOT_NAME,
  181. ),
  182. reply_markup=InlineKeyboardMarkup(
  183. paginate_modules(next_page + 1, HELPABLE, "help")
  184. ),
  185. disable_web_page_preview=True,
  186. )
  187. elif back_match:
  188. await query.message.edit(
  189. text="Hi {first_name}. I am {bot_name}".format(
  190. first_name=query.from_user.first_name,
  191. bot_name=botinfo.BOT_NAME,
  192. ),
  193. reply_markup=InlineKeyboardMarkup(
  194. paginate_modules(0, HELPABLE, "help")
  195. ),
  196. disable_web_page_preview=True,
  197. )
  198. elif create_match:
  199. text, keyboard = await help_parser(query)
  200. await query.message.edit(
  201. text=text, reply_markup=keyboard, disable_web_page_preview=True
  202. )
  203. return await client.answer_callback_query(query.id)
  204. if __name__ == "__main__":
  205. uvloop.install()
  206. loop.run_until_complete(start_bot())