__main__.py 8.6 KB

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