forked from callsmusic/tgvc-userbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.py
85 lines (72 loc) · 2.71 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
tgvc-userbot, Telegram Voice Chat Userbot
Copyright (C) 2021 Dash Eclipse
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
!ping reply with pong
!uptime check uptime
"""
from datetime import datetime
from time import time
from pyrogram import Client, filters, emoji
from pyrogram.types import Message
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}`"
)