auto-renamer / plugins /admin /speedtest.py
dragxd's picture
Fix: UserNotParticipant crash & Update: Public Stats + New Premium Emoji IDs
20fc8f6
import speedtest
import asyncio
from pyrogram import Client, filters
from config import ADMIN
@Client.on_message(filters.user(ADMIN) & filters.command("speedtest"))
async def speedtest_runner(client, message):
msg = await message.reply_text("<code>Running Speedtest... This might take a minute.</code>")
try:
loop = asyncio.get_event_loop()
st = speedtest.Speedtest()
# Select best server
await loop.run_in_executor(None, st.get_best_server)
# Run download test
await msg.edit("<code>Running Download Test...</code>")
await loop.run_in_executor(None, st.download)
# Run upload test
await msg.edit("<code>Running Upload Test...</code>")
await loop.run_in_executor(None, st.upload)
# Get share image
await msg.edit("<code>Finalizing Results...</code>")
results = await loop.run_in_executor(None, st.results.dict)
share_image = await loop.run_in_executor(None, st.results.share)
# Format results
download = results['download'] / 10**6 # bps to Mbps
upload = results['upload'] / 10**6 # bps to Mbps
ping = results['ping']
server = results['server']
text = f"""<b><emoji id=5042290883949495533>πŸš€</emoji> Speedtest Results</b>
<b><emoji id=5456140674028019486>πŸ“₯</emoji> Download:</b> <code>{download:.2f} Mbps</code>
<b><emoji id=5433653135799228968>πŸ“€</emoji> Upload:</b> <code>{upload:.2f} Mbps</code>
<b><emoji id=5787432469598835099>πŸ“‘</emoji> Ping:</b> <code>{ping:.1f} ms</code>
<b>🌍 Server:</b> <code>{server['name']} ({server['country']})</code>
<b>🏒 ISP:</b> <code>{results['client']['isp']}</code>
<b>Bot Managed By @dragbotsupport</b>"""
await message.reply_photo(photo=share_image, caption=text)
await msg.delete()
except Exception as e:
await msg.edit(f"<b>Speedtest Error:</b>\n<code>{str(e)}</code>")