This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
afk.py
92 lines (75 loc) · 3.5 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
91
92
# -*- coding: future_fstrings -*-
# 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
logger = logging.getLogger(__name__)
def register(cb):
cb(AFKMod())
@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>"}
def config_complete(self):
self.name = self.strings["name"]
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"])
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"])
async def watcher(self, message):
if message.mentioned or getattr(message.to_id, "user_id", None) == self._me.id:
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 self.get_afk() is True:
ret = self.strings["afk"].format(diff)
elif self.get_afk() is not False:
ret = self.strings["afk_reason"].format(diff, self.get_afk())
await utils.answer(message, ret)
def get_afk(self):
return self._db.get(__name__, "afk", False)