Karma
commited on
Commit
·
bd72f54
1
Parent(s):
51b00d5
Create speedtest.py
Browse files- Mikobot/plugins/speedtest.py +74 -0
Mikobot/plugins/speedtest.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# <============================================== IMPORTS =========================================================>
|
2 |
+
import speedtest
|
3 |
+
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
|
4 |
+
from telegram.constants import ParseMode
|
5 |
+
from telegram.ext import CallbackQueryHandler, ContextTypes
|
6 |
+
|
7 |
+
from Mikobot import DEV_USERS, function
|
8 |
+
from Mikobot.plugins.disable import DisableAbleCommandHandler
|
9 |
+
from Mikobot.plugins.helper_funcs.chat_status import check_admin
|
10 |
+
|
11 |
+
# <=======================================================================================================>
|
12 |
+
|
13 |
+
|
14 |
+
# <================================================ FUNCTION =======================================================>
|
15 |
+
def convert(speed):
|
16 |
+
return round(int(speed) / 1048576, 2)
|
17 |
+
|
18 |
+
|
19 |
+
@check_admin(only_dev=True)
|
20 |
+
async def speedtestxyz(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
21 |
+
buttons = [
|
22 |
+
[
|
23 |
+
InlineKeyboardButton("Image", callback_data="speedtest_image"),
|
24 |
+
InlineKeyboardButton("Text", callback_data="speedtest_text"),
|
25 |
+
],
|
26 |
+
]
|
27 |
+
await update.effective_message.reply_text(
|
28 |
+
"Select SpeedTest Mode",
|
29 |
+
reply_markup=InlineKeyboardMarkup(buttons),
|
30 |
+
)
|
31 |
+
|
32 |
+
|
33 |
+
async def speedtestxyz_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
34 |
+
query = update.callback_query
|
35 |
+
|
36 |
+
if query.from_user.id in DEV_USERS:
|
37 |
+
msg = await update.effective_message.edit_text("Running a speedtest....")
|
38 |
+
speed = speedtest.Speedtest()
|
39 |
+
speed.get_best_server()
|
40 |
+
speed.download()
|
41 |
+
speed.upload()
|
42 |
+
replymsg = "SpeedTest Results:"
|
43 |
+
|
44 |
+
if query.data == "speedtest_image":
|
45 |
+
speedtest_image = speed.results.share()
|
46 |
+
await update.effective_message.reply_photo(
|
47 |
+
photo=speedtest_image,
|
48 |
+
caption=replymsg,
|
49 |
+
)
|
50 |
+
await msg.delete()
|
51 |
+
|
52 |
+
elif query.data == "speedtest_text":
|
53 |
+
result = speed.results.dict()
|
54 |
+
replymsg += f"\nDownload: `{convert(result['download'])}Mb/s`\nUpload: `{convert(result['upload'])}Mb/s`\nPing: `{result['ping']}`"
|
55 |
+
await update.effective_message.edit_text(
|
56 |
+
replymsg, parse_mode=ParseMode.MARKDOWN
|
57 |
+
)
|
58 |
+
else:
|
59 |
+
await query.answer("You are required to join Black Bulls to use this command.")
|
60 |
+
|
61 |
+
|
62 |
+
# <================================================ HANDLER =======================================================>
|
63 |
+
SPEED_TEST_HANDLER = DisableAbleCommandHandler("speedtest", speedtestxyz, block=False)
|
64 |
+
SPEED_TEST_CALLBACKHANDLER = CallbackQueryHandler(
|
65 |
+
speedtestxyz_callback, pattern="speedtest_.*", block=False
|
66 |
+
)
|
67 |
+
|
68 |
+
function(SPEED_TEST_HANDLER)
|
69 |
+
function(SPEED_TEST_CALLBACKHANDLER)
|
70 |
+
|
71 |
+
__mod_name__ = "SpeedTest"
|
72 |
+
__command_list__ = ["speedtest"]
|
73 |
+
__handlers__ = [SPEED_TEST_HANDLER, SPEED_TEST_CALLBACKHANDLER]
|
74 |
+
# <================================================ END =======================================================>
|