Karma
commited on
Commit
·
9eb0bbf
1
Parent(s):
7c2a6d7
AFK added
Browse files- Mikobot/plugins/afk.py +215 -0
Mikobot/plugins/afk.py
ADDED
@@ -0,0 +1,215 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# <============================================== IMPORTS =========================================================>
|
2 |
+
import html
|
3 |
+
import random
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
import humanize
|
7 |
+
from telegram import MessageEntity, Update
|
8 |
+
from telegram.error import BadRequest
|
9 |
+
from telegram.ext import ContextTypes, MessageHandler, filters
|
10 |
+
|
11 |
+
from Database.sql import afk_sql as sql
|
12 |
+
from Mikobot import LOGGER, function
|
13 |
+
from Mikobot.plugins.disable import DisableAbleCommandHandler, DisableAbleMessageHandler
|
14 |
+
from Mikobot.plugins.users import get_user_id
|
15 |
+
|
16 |
+
# <=======================================================================================================>
|
17 |
+
|
18 |
+
AFK_GROUP = 7
|
19 |
+
AFK_REPLY_GROUP = 8
|
20 |
+
|
21 |
+
|
22 |
+
# <================================================ FUNCTION =======================================================>
|
23 |
+
async def afk(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
24 |
+
if update.effective_message.text:
|
25 |
+
args = update.effective_message.text.split(None, 1)
|
26 |
+
else:
|
27 |
+
return
|
28 |
+
user = update.effective_user
|
29 |
+
|
30 |
+
if not user: # ignore channels
|
31 |
+
return
|
32 |
+
|
33 |
+
notice = ""
|
34 |
+
if len(args) >= 2:
|
35 |
+
reason = args[1]
|
36 |
+
if len(reason) > 100:
|
37 |
+
reason = reason[:100]
|
38 |
+
notice = "\nYour afk reason was shortened to 100 characters."
|
39 |
+
else:
|
40 |
+
reason = ""
|
41 |
+
|
42 |
+
sql.set_afk(update.effective_user.id, reason)
|
43 |
+
fname = update.effective_user.first_name
|
44 |
+
try:
|
45 |
+
if reason:
|
46 |
+
await update.effective_message.reply_text(
|
47 |
+
f"➲ {fname} is now away! \n\n➦ Reason: <code>{reason}</code> \n {notice}",
|
48 |
+
parse_mode="html",
|
49 |
+
)
|
50 |
+
else:
|
51 |
+
await update.effective_message.reply_text(
|
52 |
+
"➲ {} is now away!{}".format(fname, notice),
|
53 |
+
)
|
54 |
+
except BadRequest:
|
55 |
+
pass
|
56 |
+
|
57 |
+
|
58 |
+
async def no_longer_afk(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
59 |
+
user = update.effective_user
|
60 |
+
message = update.effective_message
|
61 |
+
|
62 |
+
if not user: # ignore channels
|
63 |
+
return
|
64 |
+
|
65 |
+
if sql.is_afk(user.id):
|
66 |
+
afk_user = sql.check_afk_status(user.id)
|
67 |
+
|
68 |
+
time = humanize.naturaldelta(datetime.now() - afk_user.time)
|
69 |
+
|
70 |
+
res = sql.rm_afk(user.id)
|
71 |
+
if res:
|
72 |
+
if message.new_chat_members: # dont say msg
|
73 |
+
return
|
74 |
+
firstname = update.effective_user.first_name
|
75 |
+
try:
|
76 |
+
options = [
|
77 |
+
"➲ {} is here!",
|
78 |
+
"➲ {} is back!",
|
79 |
+
"➲ {} is now in the chat!",
|
80 |
+
"➲ {} is awake!",
|
81 |
+
"➲ {} is back online!",
|
82 |
+
"➲ {} is finally here!",
|
83 |
+
"➲ Welcome back! {}",
|
84 |
+
]
|
85 |
+
chosen_option = random.choice(options)
|
86 |
+
await update.effective_message.reply_text(
|
87 |
+
chosen_option.format(firstname)
|
88 |
+
+ f"\n\nYou were AFK for: <code>{time}</code>",
|
89 |
+
parse_mode="html",
|
90 |
+
)
|
91 |
+
except:
|
92 |
+
return
|
93 |
+
|
94 |
+
|
95 |
+
async def reply_afk(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
96 |
+
bot = context.bot
|
97 |
+
message = update.effective_message
|
98 |
+
userc = update.effective_user
|
99 |
+
userc_id = userc.id
|
100 |
+
if message.entities and message.parse_entities(
|
101 |
+
[MessageEntity.TEXT_MENTION, MessageEntity.MENTION],
|
102 |
+
):
|
103 |
+
entities = message.parse_entities(
|
104 |
+
[MessageEntity.TEXT_MENTION, MessageEntity.MENTION],
|
105 |
+
)
|
106 |
+
|
107 |
+
chk_users = []
|
108 |
+
for ent in entities:
|
109 |
+
if ent.type == MessageEntity.TEXT_MENTION:
|
110 |
+
user_id = ent.user.id
|
111 |
+
fst_name = ent.user.first_name
|
112 |
+
|
113 |
+
if user_id in chk_users:
|
114 |
+
return
|
115 |
+
chk_users.append(user_id)
|
116 |
+
|
117 |
+
if ent.type != MessageEntity.MENTION:
|
118 |
+
return
|
119 |
+
|
120 |
+
user_id = await get_user_id(
|
121 |
+
message.text[ent.offset : ent.offset + ent.length],
|
122 |
+
)
|
123 |
+
if not user_id:
|
124 |
+
return
|
125 |
+
|
126 |
+
if user_id in chk_users:
|
127 |
+
return
|
128 |
+
chk_users.append(user_id)
|
129 |
+
|
130 |
+
try:
|
131 |
+
chat = await bot.get_chat(user_id)
|
132 |
+
except BadRequest:
|
133 |
+
LOGGER.error(
|
134 |
+
"Error: Could not fetch userid {} for AFK module".format(user_id)
|
135 |
+
)
|
136 |
+
return
|
137 |
+
fst_name = chat.first_name
|
138 |
+
|
139 |
+
await check_afk(update, context, user_id, fst_name, userc_id)
|
140 |
+
|
141 |
+
elif message.reply_to_message:
|
142 |
+
user_id = message.reply_to_message.from_user.id
|
143 |
+
fst_name = message.reply_to_message.from_user.first_name
|
144 |
+
await check_afk(update, context, user_id, fst_name, userc_id)
|
145 |
+
|
146 |
+
|
147 |
+
async def check_afk(
|
148 |
+
update: Update,
|
149 |
+
context: ContextTypes.DEFAULT_TYPE,
|
150 |
+
user_id: int,
|
151 |
+
fst_name: str,
|
152 |
+
userc_id: int,
|
153 |
+
):
|
154 |
+
if sql.is_afk(user_id):
|
155 |
+
user = sql.check_afk_status(user_id)
|
156 |
+
|
157 |
+
if int(userc_id) == int(user_id):
|
158 |
+
return
|
159 |
+
|
160 |
+
time = humanize.naturaldelta(datetime.now() - user.time)
|
161 |
+
|
162 |
+
if not user.reason:
|
163 |
+
res = "➲ {} is afk.\n\n➦ Last seen {} ago.".format(
|
164 |
+
fst_name,
|
165 |
+
time,
|
166 |
+
)
|
167 |
+
await update.effective_message.reply_text(res)
|
168 |
+
else:
|
169 |
+
res = (
|
170 |
+
"➲ {} is afk.\n\n➦ Reason: <code>{}</code>\n➦ Last seen {} ago.".format(
|
171 |
+
html.escape(fst_name),
|
172 |
+
html.escape(user.reason),
|
173 |
+
time,
|
174 |
+
)
|
175 |
+
)
|
176 |
+
await update.effective_message.reply_text(res, parse_mode="html")
|
177 |
+
|
178 |
+
|
179 |
+
# <=================================================== HELP ====================================================>
|
180 |
+
|
181 |
+
|
182 |
+
__help__ = """
|
183 |
+
» /afk <reason>*:* mark yourself as AFK (away from keyboard).
|
184 |
+
|
185 |
+
» brb , !afk <reason>*:* same as the afk command - but not a command.
|
186 |
+
|
187 |
+
➠ *When marked as AFK, any mentions will be replied to with a message to say you're not available!*
|
188 |
+
"""
|
189 |
+
|
190 |
+
# <================================================ HANDLER =======================================================>
|
191 |
+
AFK_HANDLER = DisableAbleCommandHandler("afk", afk, block=False)
|
192 |
+
AFK_REGEX_HANDLER = DisableAbleMessageHandler(
|
193 |
+
filters.Regex(r"^(?i:(brb|!afk))( .*)?$"), afk, friendly="afk", block=False
|
194 |
+
)
|
195 |
+
NO_AFK_HANDLER = MessageHandler(
|
196 |
+
filters.ALL & filters.ChatType.GROUPS, no_longer_afk, block=False
|
197 |
+
)
|
198 |
+
AFK_REPLY_HANDLER = MessageHandler(
|
199 |
+
filters.ALL & filters.ChatType.GROUPS, reply_afk, block=False
|
200 |
+
)
|
201 |
+
|
202 |
+
function(AFK_HANDLER, AFK_GROUP)
|
203 |
+
function(AFK_REGEX_HANDLER, AFK_GROUP)
|
204 |
+
function(NO_AFK_HANDLER, AFK_GROUP)
|
205 |
+
function(AFK_REPLY_HANDLER, AFK_REPLY_GROUP)
|
206 |
+
|
207 |
+
__mod_name__ = "AFK"
|
208 |
+
__command_list__ = ["afk"]
|
209 |
+
__handlers__ = [
|
210 |
+
(AFK_HANDLER, AFK_GROUP),
|
211 |
+
(AFK_REGEX_HANDLER, AFK_GROUP),
|
212 |
+
(NO_AFK_HANDLER, AFK_GROUP),
|
213 |
+
(AFK_REPLY_HANDLER, AFK_REPLY_GROUP),
|
214 |
+
]
|
215 |
+
# <================================================ END =======================================================>
|