File size: 4,556 Bytes
67305b5
 
 
 
 
 
 
a685e1d
67305b5
 
 
 
 
a685e1d
67305b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a685e1d
 
67305b5
 
a685e1d
67305b5
 
a685e1d
 
 
 
 
67305b5
 
 
 
a685e1d
67305b5
 
 
 
a685e1d
67305b5
a685e1d
67305b5
a685e1d
 
 
67305b5
a685e1d
67305b5
 
a685e1d
67305b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a685e1d
67305b5
 
 
a685e1d
67305b5
 
 
 
 
 
 
a685e1d
 
 
 
 
 
 
 
 
 
67305b5
 
 
 
 
 
a685e1d
67305b5
 
 
 
 
 
 
 
 
 
 
a685e1d
 
 
 
 
67305b5
a685e1d
67305b5
 
 
 
 
 
 
a685e1d
 
 
 
 
 
67305b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from datetime import datetime
from random import choice

from pyrogram import filters
from pyrogram.enums import ParseMode as PM
from pyrogram.types import Message

from Powers import LOGGER, PREFIX_HANDLER
from Powers.bot_class import Gojo
from Powers.database.afk_db import AFK
from Powers.plugins import till_date
from Powers.utils.cmd_senders import send_cmd
from Powers.utils.custom_filters import command
from Powers.utils.msg_types import Types, get_afk_type
from Powers.vars import Config

# from traceback import format_exc

res = [
    "{first} is resting for a while...",
    "{first} living his real life, go and live yours.",
    "{first} is quite busy now-a-days.",
    "I am looking for {first} too...tell me if you see him/her around",
    "{first} ran away from the chat...",
    "{first} is busy in his/her work ||simping||",
    "{first} is busy saving the world",
    "{first} is now tired fighting all the curses"
]

back = [
    "{first} is finally back to life",
    "{first} welcome back",
    "{first} the spy is back watch what you talk about"
]

@Gojo.on_message(command(["afk","brb"]) & ~filters.private)
async def going_afk(c: Gojo, m: Message):
    user = m.from_user.id
    chat = m.chat.id
    afk = AFK()
    text, data_type, content = await get_afk_type(m)
    
    time = str(datetime.now()).rsplit(".",1)[0]

    if len(m.command) == 1:
        text = choice(res)

    elif len(m.command) > 1:
        text = m.text.markdown.split(None,1)[1]

    if not data_type:
        data_type = Types.TEXT

    afk.insert_afk(chat,user,str(time),text,data_type,content)

    await m.reply_text(f"{m.from_user.mention} is now AFK")

    return

async def get_hours(hour:str):
    tim = hour.strip().split(":")
    txt = ""
    if int(tim[0]):
        txt += tim[0] + " hours "
    if int(tim[1]):
        txt += tim[1] + " minutes "
    if int(round(float(tim[2]))):
        txt += str(round(float(tim[2]))) + " seconds"
    
    return txt


@Gojo.on_message(filters.group,group=-18)
async def afk_checker(c: Gojo, m: Message):
    if not m.from_user:
        return

    afk = AFK()
    back_ = choice(back)
    user = m.from_user.id
    chat = m.chat.id
    repl = m.reply_to_message

    if repl and repl.from_user:
        rep_user = repl.from_user.id
    else:
        rep_user = False

    is_afk = afk.check_afk(chat,user)
    is_rep_afk = False
    if rep_user:
        is_rep_afk = afk.check_afk(chat,rep_user)

    if is_rep_afk and rep_user != user:
        con = afk.get_afk(chat,rep_user)
        time = till_date(con["time"])
        media = con["media"]
        media_type = con["media_type"]
        tim_ = datetime.now() - time
        tim_ = str(tim_).split(",")
        tim = await get_hours(tim_[-1])
        if len(tim_) == 1:
            tims = tim
        elif len(tim_) == 2:
            tims = tim_[0] + " " + tim
        reason = f"{repl.from_user.first_name} is afk since {tims}\n"
        if con['reason'] not in res:
            reason += f"\nDue to: {con['reason'].format(first=repl.from_user.first_name)}"
        else:
            reason += f"\n{con['reason'].format(first=repl.from_user.first_name)}"
        txt = reason

        if media_type == Types.TEXT:        
            await (await send_cmd(c,media_type))(
                chat,
                txt,
                parse_mode=PM.MARKDOWN,
                reply_to_message_id=m.id,
            )
        else:
            await (await send_cmd(c,media_type))(
                chat,
                media,
                txt,
                parse_mode=PM.MARKDOWN,
                reply_to_message_id=repl.id
            )
    
    if is_afk:
        txt = False
        try:
            txt = m.command[0]
        except Exception:
            pass

        if txt and txt in ["afk","brb"]:
            return
        else:
            con = afk.get_afk(chat,user)
            time = till_date(con["time"])
            tim_ = datetime.now() - time
            tim_ = str(tim_).split(",")
            tim = await get_hours(tim_[-1])
            if len(tim_) == 1:
                tims = tim
            elif len(tim_) == 2:
                tims = tim_[0] + " " + tim
            txt = back_.format(first=m.from_user.mention) + f"\n\nAfk for: {tims}"
            await m.reply_text(txt)
        afk.delete_afk(chat,user)
    return

__PLUGIN__ = "afk"

_DISABLE_CMDS_ = ["afk","brb"]

__alt_name__ = ["brb"]

__HELP__ = """
**AFK**
• /afk (/brb) [reason | reply to a message]

`reply to a message` can be any media or text
"""