forked from friendly-telegram/modules-repo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
afk.py
executable file
·90 lines (76 loc) · 3.58 KB
/
afk.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
86
87
88
89
90
# Friendly Telegram (telegram userbot)
# Copyright (C) 2018-2019 The Authors
# 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/>.
from .. import loader, utils
import logging
import datetime
import time
from telethon import types
logger = logging.getLogger(__name__)
@loader.tds
class AFKMod(loader.Module):
"""Provides a message saying that you are unavailable"""
strings = {"name": "AFK",
"gone": "<b>I'm goin' AFK</b>",
"back": "<b>I'm no longer AFK</b>",
"afk": "<b>I'm AFK right now (since {} ago).</b>",
"afk_reason": "<b>I'm AFK right now (since {} ago).\nReason:</b> <i>{}</i>"}
async def client_ready(self, client, db):
self._db = db
self._me = await client.get_me()
async def afkcmd(self, message):
""".afk [message]"""
if utils.get_args_raw(message):
self._db.set(__name__, "afk", utils.get_args_raw(message))
else:
self._db.set(__name__, "afk", True)
self._db.set(__name__, "gone", time.time())
self._db.set(__name__, "ratelimit", [])
await self.allmodules.log("afk", data=utils.get_args_raw(message) or None)
await utils.answer(message, self.strings("gone", message))
async def unafkcmd(self, message):
"""Remove the AFK status"""
self._db.set(__name__, "afk", False)
self._db.set(__name__, "gone", None)
self._db.set(__name__, "ratelimit", [])
await self.allmodules.log("unafk")
await utils.answer(message, self.strings("back", message))
async def watcher(self, message):
if not isinstance(message, types.Message):
return
if message.mentioned or getattr(message.to_id, "user_id", None) == self._me.id:
afk_state = self.get_afk()
if not afk_state:
return
logger.debug("tagged!")
ratelimit = self._db.get(__name__, "ratelimit", [])
if utils.get_chat_id(message) in ratelimit:
return
else:
self._db.setdefault(__name__, {}).setdefault("ratelimit", []).append(utils.get_chat_id(message))
self._db.save()
user = await utils.get_user(message)
if user.is_self or user.bot or user.verified:
logger.debug("User is self, bot or verified.")
return
if self.get_afk() is False:
return
now = datetime.datetime.now().replace(microsecond=0)
gone = datetime.datetime.fromtimestamp(self._db.get(__name__, "gone")).replace(microsecond=0)
diff = now - gone
if afk_state is True:
ret = self.strings("afk", message).format(diff)
elif afk_state is not False:
ret = self.strings("afk_reason", message).format(diff, afk_state)
await utils.answer(message, ret, reply_to=message)
def get_afk(self):
return self._db.get(__name__, "afk", False)