forked from callsmusic/tgvc-userbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.py
71 lines (63 loc) · 2.05 KB
/
ping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""!ping reply with pong
!uptime check uptime
"""
from time import time
from datetime import datetime
from pyrogram import Client, filters, emoji
from pyrogram.types import Message
# DELAY_DELETE = 60
START_TIME = datetime.utcnow()
START_TIME_ISO = START_TIME.replace(microsecond=0).isoformat()
TIME_DURATION_UNITS = (
('week', 60 * 60 * 24 * 7),
('day', 60 * 60 * 24),
('hour', 60 * 60),
('min', 60),
('sec', 1)
)
self_or_contact_filter = filters.create(
lambda
_,
__,
message:
(message.from_user and message.from_user.is_contact) or message.outgoing
)
# https://gist.github.com/borgstrom/936ca741e885a1438c374824efb038b3
async def _human_time_duration(seconds):
if seconds == 0:
return 'inf'
parts = []
for unit, div in TIME_DURATION_UNITS:
amount, seconds = divmod(int(seconds), div)
if amount > 0:
parts.append('{} {}{}'
.format(amount, unit, "" if amount == 1 else "s"))
return ', '.join(parts)
@Client.on_message(filters.text
& self_or_contact_filter
& ~filters.edited
& ~filters.via_bot
& filters.regex("^!ping$"))
async def ping_pong(_, m: Message):
"""reply ping with pong and delete both messages"""
start = time()
m_reply = await m.reply_text("...")
delta_ping = time() - start
await m_reply.edit_text(
f"{emoji.ROBOT} ping: `{delta_ping * 1000:.3f} ms`"
)
@Client.on_message(filters.text
& self_or_contact_filter
& ~filters.edited
& ~filters.via_bot
& filters.regex("^!uptime$"))
async def get_uptime(_, m: Message):
"""/uptime Reply with readable uptime and ISO 8601 start time"""
current_time = datetime.utcnow()
uptime_sec = (current_time - START_TIME).total_seconds()
uptime = await _human_time_duration(int(uptime_sec))
await m.reply_text(
f"{emoji.ROBOT}\n"
f"- uptime: `{uptime}`\n"
f"- start time: `{START_TIME_ISO}`"
)