misc.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 secrets
  21. import string
  22. import aiohttp
  23. from pyrogram import filters
  24. from googletrans import Translator
  25. from cryptography.fernet import Fernet
  26. from wbb import app, FERNET_ENCRYPTION_KEY
  27. from wbb.utils import random_line
  28. from wbb.utils.json_prettify import json_prettify
  29. from wbb.utils.fetch import fetch
  30. from wbb.utils.pastebin import paste
  31. from wbb.core.decorators.errors import capture_err
  32. __MODULE__ = "Misc"
  33. __HELP__ = '''/commit - Generate Funny Commit Messages
  34. /runs - Idk Test Yourself
  35. /id - Get Chat_ID or User_ID
  36. /random [Length] - Generate Random Complex Passwords
  37. /encrypt - Encrypt Text [Can Only Be Decrypted By This Bot]
  38. /decrypt - Decrypt Text
  39. /cheat [Language] [Query] - Get Programming Related Help
  40. /weather [City] - To Get Weather Info
  41. /tr [en] - Translate A Message
  42. /json [URL] - Get Response From An API or Something.
  43. #RTFM - Check it lol'''
  44. @app.on_message(filters.command("commit") & ~filters.edited)
  45. async def commit(_, message):
  46. await message.reply_text((await random_line('wbb/utils/commit.txt')))
  47. @app.on_message(filters.command("RTFM", "#"))
  48. async def rtfm(_, message):
  49. await message.delete()
  50. if not message.reply_to_message:
  51. await message.reply_text("Reply To A Message lol")
  52. return
  53. await message.reply_to_message.reply_text("Are You Lost? READ THE FUCKING DOCS!")
  54. @app.on_message(filters.command("runs") & ~filters.edited)
  55. async def runs(_, message):
  56. await message.reply_text((await random_line('wbb/utils/runs.txt')))
  57. @app.on_message(filters.command("id"))
  58. async def getid(_, message):
  59. if len(message.command) == 2:
  60. id = (await app.get_users(message.text.split(None, 1)[1])).id
  61. text = f"<b>ID:</b> {id}"
  62. return
  63. text_unping = "<b>Chat ID:</b>"
  64. if message.chat.username:
  65. text_unping = (
  66. f'<a href="https://t.me/{message.chat.username}">{text_unping}</a>'
  67. )
  68. text_unping += f" <code>{message.chat.id}</code>\n"
  69. text = "<b>Message ID:</b>"
  70. if message.link:
  71. text = f'<a href="{message.link}">{text}</a>'
  72. text += f" <code>{message.message_id}</code>\n"
  73. text_unping += text
  74. if message.from_user:
  75. text_unping += f'<b><a href="tg://user?id={message.from_user.id}">User ID:</a></b> <code>{message.from_user.id}</code>\n'
  76. text_ping = text_unping
  77. reply = message.reply_to_message
  78. if not getattr(reply, "empty", True):
  79. text_unping += "\n"
  80. text = "<b>Replied Message ID:</b>"
  81. if reply.link:
  82. text = f'<a href="{reply.link}">{text}</a>'
  83. text += f" <code>{reply.message_id}</code>\n"
  84. text_unping += text
  85. text_ping = text_unping
  86. if reply.from_user:
  87. text = "<b>Replied User ID:</b>"
  88. if reply.from_user.username:
  89. text = f'<a href="https://t.me/{reply.from_user.username}">{text}</a>'
  90. text += f" <code>{reply.from_user.id}</code>\n"
  91. text_unping += text
  92. text_ping += f'<b><a href="tg://user?id={reply.from_user.id}">Replied User ID:</a></b> <code>{reply.from_user.id}</code>\n'
  93. if reply.forward_from:
  94. text_unping += "\n"
  95. text = "<b>Forwarded User ID:</b>"
  96. if reply.forward_from.username:
  97. text = (
  98. f'<a href="https://t.me/{reply.forward_from.username}">{text}</a>'
  99. )
  100. text += f" <code>{reply.forward_from.id}</code>\n"
  101. text_unping += text
  102. text_ping += f'\n<b><a href="tg://user?id={reply.forward_from.id}">Forwarded User ID:</a></b> <code>{reply.forward_from.id}</code>\n'
  103. reply = await message.reply_text(text_unping, disable_web_page_preview=True)
  104. if text_unping != text_ping:
  105. await reply.edit_text(text_ping, disable_web_page_preview=True)
  106. # Random
  107. @app.on_message(filters.command('random') & ~filters.edited)
  108. @capture_err
  109. async def random(_, message):
  110. if len(message.command) != 2:
  111. await message.reply_text('"/random" Needs An Argurment.'
  112. ' Ex: `/random 5`')
  113. return
  114. length = message.text.split(None, 1)[1]
  115. try:
  116. if 1 < int(length) < 1000:
  117. alphabet = string.ascii_letters + string.digits
  118. password = ''.join(secrets.choice(alphabet) for i in
  119. range(int(length)))
  120. await message.reply_text(f"`{password}`")
  121. else:
  122. await message.reply_text('Specify A Length Between 1-1000')
  123. except ValueError:
  124. await message.reply_text("Strings Won't Work!, Pass A"
  125. + " Positive Integer Between 1-1000")
  126. # Encrypt
  127. @app.on_message(filters.command('encrypt') & ~filters.edited)
  128. @capture_err
  129. async def encrypt(_, message):
  130. if not message.reply_to_message:
  131. await message.reply_text('Reply To A Message To Encrypt It.')
  132. return
  133. text = message.reply_to_message.text
  134. text_in_bytes = bytes(text, 'utf-8')
  135. cipher_suite = Fernet(FERNET_ENCRYPTION_KEY)
  136. encrypted_text = cipher_suite.encrypt(text_in_bytes)
  137. bytes_in_text = encrypted_text.decode("utf-8")
  138. await message.reply_text(bytes_in_text)
  139. # Decrypt
  140. @app.on_message(filters.command('decrypt') & ~filters.edited)
  141. @capture_err
  142. async def decrypt(_, message):
  143. if not message.reply_to_message:
  144. await message.reply_text('Reply To A Message To Decrypt It.')
  145. return
  146. text = message.reply_to_message.text
  147. text_in_bytes = bytes(text, 'utf-8')
  148. cipher_suite = Fernet(FERNET_ENCRYPTION_KEY)
  149. decoded_text = cipher_suite.decrypt(text_in_bytes)
  150. bytes_in_text = decoded_text.decode("utf-8")
  151. await message.reply_text(bytes_in_text)
  152. async def fetch_text(url):
  153. async with aiohttp.ClientSession(headers={"user-agent": "curl"}) as session:
  154. async with session.get(url) as resp:
  155. data = await resp.text()
  156. return data
  157. # Cheat.sh
  158. @app.on_message(filters.command("cheat") & ~filters.edited)
  159. @capture_err
  160. async def cheat(_, message):
  161. if len(message.command) < 3:
  162. await message.reply_text("/cheat [language] [query]")
  163. return
  164. text = message.text.split(None, 1)[1]
  165. m = await message.reply_text("Searching")
  166. try:
  167. ftext = text.split()
  168. language = ftext[0]
  169. query = ftext[1]
  170. data = await fetch_text(f"http://cht.sh/{language}/{query}?QT")
  171. if not data:
  172. await m.edit("Found Literally Nothing!")
  173. return
  174. await m.edit(f"`{data}`")
  175. except Exception as e:
  176. await m.edit(str(e))
  177. print(str(e))
  178. # Weather
  179. @app.on_message(filters.command("weather") & ~filters.edited)
  180. @capture_err
  181. async def weather(_, message):
  182. if len(message.command) != 2:
  183. await message.reply_text("/weather [city]")
  184. return
  185. city = message.text.split(None, 1)[1]
  186. m = await message.reply_text("Fetching Data")
  187. data = await fetch_text(f"https://wttr.in/{city}?mnTC0")
  188. await m.edit(f"`{data}`")
  189. # Translate
  190. @app.on_message(filters.command("tr") & ~filters.edited)
  191. @capture_err
  192. async def tr(_, message):
  193. if len(message.command) != 2:
  194. await message.reply_text("/tr [LANGUAGE_CODE]")
  195. return
  196. lang = message.text.split(None, 1)[1]
  197. if not message.reply_to_message or not lang:
  198. await message.reply_text(
  199. "Reply to a message with /tr [language code]"
  200. + "\nGet supported language list from here -"
  201. + " https://py-googletrans.readthedocs.io/en"
  202. + "/latest/#googletrans-languages"
  203. )
  204. return
  205. if message.reply_to_message.text:
  206. text = message.reply_to_message.text
  207. try:
  208. i = Translator().translate(text, dest=lang)
  209. except ValueError:
  210. await message.reply_text("[ERROR]: LANGUAGE NOT FOUND")
  211. return
  212. await message.reply_text(i.text)
  213. elif message.reply_to_message.caption:
  214. text = message.reply_to_message.caption
  215. i = Translator().translate(text, dest=lang)
  216. await message.reply_text(i.text)
  217. fetch_limit = 0
  218. @app.on_message(filters.command("json") & ~filters.edited)
  219. @capture_err
  220. async def json_fetch(_, message):
  221. global fetch_limit
  222. if len(message.command) != 2:
  223. await message.reply_text("/json [URL]")
  224. return
  225. elif fetch_limit > 500:
  226. await message.reply_text("Today's Quota Exceeded!, lol")
  227. return
  228. url = message.text.split(None, 1)[1]
  229. try:
  230. data = await fetch(url)
  231. data = await json_prettify(data)
  232. fetch_limit += 1
  233. if len(data) < 4090:
  234. await message.reply_text(data)
  235. else:
  236. link = await paste(data)
  237. await message.reply_text(f"[OUTPUT_TOO_LONG]({link})", disable_web_page_preview=True)
  238. except Exception as e:
  239. await message.reply_text(str(e))
  240. print(str(e))
  241. @app.on_message(filters.command('bun'))
  242. async def bunn(_, message):
  243. if message.reply_to_message:
  244. await message.reply_to_message.reply_sticker('CAACAgUAAx0CWIlO9AABARyRYBhyjKXFATVhu7AGQwip3TzSFiMAAuMBAAJ7usBUIu2xBtXTmuweBA')
  245. await app.send_message(message.chat.id, text="Eat Bun")
  246. return
  247. if not message.reply_to_message:
  248. await message.reply_sticker('CAACAgUAAx0CWIlO9AABARyRYBhyjKXFATVhu7AGQwip3TzSFiMAAuMBAAJ7usBUIu2xBtXTmuweBA')
  249. await app.send_message(message.chat.id, text="Eat Bun")