Skip to content
This repository was archived by the owner on Sep 29, 2021. It is now read-only.

Commit 359798d

Browse files
KenHVMr.Miss
authored andcommitted
afk: don't crash if an anonymous message mentions user
squash change from: 1. afk: don't crash if an anonymous message mentions user [KenHV/KensurBot@7dc089c] get_sender() returns None if telegram didn't send the sender. this leads to the bot crashing whenever an anonymous message mentions the user, as the afk event handler doesn't account for this case. let's add a check to handle this case. 2. afk: fully handle anonymous admin [MrMissx/UserButt@1c29d68] follow up for commit MrMissx/UserButt@46a239d now the anonymous admin info saved and notified on BOTLOG as a group name. Co-authored-by: KenHV <[email protected]> Co-authored-by: Mr.Miss <[email protected]> Signed-off-by: MoveAngel <[email protected]>
1 parent 251c941 commit 359798d

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

userbot/modules/afk.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,20 @@ async def type_afk_is_not_true(notafk):
121121
+ " chats while you were away",
122122
)
123123
for i in USERS:
124-
name = await notafk.client.get_entity(i)
125-
name0 = str(name.first_name)
126-
await notafk.client.send_message(
127-
BOTLOG_CHATID,
128-
"["
129-
+ name0
130-
+ "](tg://user?id="
131-
+ str(i)
132-
+ ")"
133-
+ " sent you "
134-
+ "`"
135-
+ str(USERS[i])
136-
+ " messages`",
137-
)
124+
if str(i).isnumeric():
125+
name = await notafk.client.get_entity(i)
126+
name0 = str(name.first_name)
127+
await notafk.client.send_message(
128+
BOTLOG_CHATID,
129+
"[" + name0 + "](tg://user?id=" + str(i) + ")" +
130+
" sent you " + "`" + str(USERS[i]) + " message(s)`",
131+
)
132+
else: # anon admin
133+
await notafk.client.send_message(
134+
BOTLOG_CHATID,
135+
"Anonymous admin in `" + i + "` sent you " + "`" +
136+
str(USERS[i]) + " message(s)`",
137+
)
138138
COUNT_MSG = 0
139139
USERS = {}
140140
AFKREASON = None
@@ -153,8 +153,7 @@ async def mention_afk(mention):
153153
back_alivee = datetime.now()
154154
afk_end = back_alivee.replace(microsecond=0)
155155
afk_since = "a while ago"
156-
if mention.message.mentioned and not (await mention.get_sender()).bot:
157-
if ISAFK:
156+
if ISAFK and mention.message.mentioned:
158157
now = datetime.now()
159158
datime_since_afk = now - afk_time # pylint:disable=E0602
160159
time = float(datime_since_afk.seconds)
@@ -182,17 +181,28 @@ async def mention_afk(mention):
182181
afk_since = f"`{int(minutes)}m{int(seconds)}s` ago"
183182
else:
184183
afk_since = f"`{int(seconds)}s` ago"
185-
if mention.sender_id not in USERS:
184+
185+
is_bot = False
186+
if (sender := await mention.get_sender()):
187+
is_bot = sender.bot
188+
if is_bot: return # ignore bot
189+
190+
chat_obj = await mention.client.get_entity(mention.chat_id)
191+
chat_title = chat_obj.title
192+
193+
if mention.sender_id not in USERS or chat_title not in USERS:
186194
if AFKREASON:
187195
await mention.reply(
188196
f"I'm AFK since {afk_since}.\
189197
\nReason: `{AFKREASON}`"
190198
)
191199
else:
192200
await mention.reply(str(choice(AFKSTR)))
193-
USERS.update({mention.sender_id: 1})
194-
COUNT_MSG = COUNT_MSG + 1
195-
elif mention.sender_id in USERS:
201+
if mention.sender_id is not None:
202+
USERS.update({mention.sender_id: 1})
203+
else:
204+
USERS.update({chat_title: 1})
205+
else:
196206
if USERS[mention.sender_id] % randint(2, 4) == 0:
197207
if AFKREASON:
198208
await mention.reply(
@@ -201,11 +211,11 @@ async def mention_afk(mention):
201211
)
202212
else:
203213
await mention.reply(str(choice(AFKSTR)))
204-
USERS[mention.sender_id] = USERS[mention.sender_id] + 1
205-
COUNT_MSG = COUNT_MSG + 1
206-
else:
207-
USERS[mention.sender_id] = USERS[mention.sender_id] + 1
208-
COUNT_MSG = COUNT_MSG + 1
214+
if mention.sender_id is not None:
215+
USERS[mention.sender_id] += 1
216+
else:
217+
USERS[chat_title] += 1
218+
COUNT_MSG += 1
209219

210220

211221
@register(incoming=True, disable_errors=True)

0 commit comments

Comments
 (0)