Skip to content

Commit 80dc0b9

Browse files
committed
Kick user if it has no username
- Debugging welcome.py - Improving username check - Improving username check by checking the existence of the `username` attribute. Refactorizing username check by moving the functionality to the main new_user loop - Fix kick member if username is not set
1 parent 05ff8f5 commit 80dc0b9

1 file changed

Lines changed: 106 additions & 95 deletions

File tree

tg_bot/modules/welcome.py

Lines changed: 106 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import html
2+
import time
23
from typing import Optional, List
34

45
import requests as req
@@ -18,6 +19,7 @@
1819
escape_invalid_curly_brackets,
1920
)
2021
from tg_bot.modules.log_channel import loggable
22+
from tg_bot import BAN_STICKER
2123

2224
VALID_WELCOME_FORMATTERS = [
2325
"first",
@@ -129,118 +131,127 @@ def cas_banned(userid):
129131
@run_async
130132
def new_member(bot: Bot, update: Update):
131133
chat = update.effective_chat # type: Optional[Chat]
134+
new_members = update.effective_message.new_chat_members
135+
message = update.effective_message
132136

133137
should_welc, cust_welcome, welc_type = sql.get_welc_pref(chat.id)
134138
if should_welc:
135139
sent = None
136-
new_members = update.effective_message.new_chat_members
137140
for new_mem in new_members:
138-
# Check if the user is cas-banned
139-
if not cas_banned(new_mem.id):
140-
# Give the owner a special welcome
141-
if new_mem.id == OWNER_ID:
142-
update.effective_message.reply_text(
143-
"Salve capo!"
144-
)
145-
continue
141+
# Check if the username is defined
142+
if new_mem.username:
143+
# Check if the user is cas-banned
144+
if not cas_banned(new_mem.id):
145+
# Give the owner a special welcome
146+
if new_mem.id == OWNER_ID:
147+
update.effective_message.reply_text(
148+
"Salve capo!"
149+
)
150+
continue
146151

147-
# Don't welcome yourself
148-
elif new_mem.id == bot.id:
149-
continue
152+
# Don't welcome yourself
153+
elif new_mem.id == bot.id:
154+
continue
150155

151-
else:
152-
# Muting new users
153-
bot.restrict_chat_member(
154-
chat.id, new_mem.id, can_send_messages=False
155-
)
156+
else:
157+
# Muting new users
158+
bot.restrict_chat_member(
159+
chat.id, new_mem.id, can_send_messages=False
160+
)
156161

157-
# If welcome message is media, send with appropriate function
158-
if (
159-
welc_type != sql.Types.TEXT
160-
and welc_type != sql.Types.BUTTON_TEXT
161-
):
162-
ENUM_FUNC_MAP[welc_type](chat.id, cust_welcome)
163-
return
164-
# else, move on
165-
first_name = (
166-
new_mem.first_name or "PersonWithNoName"
167-
) # edge case of empty name - occurs for some bugs.
168-
169-
if cust_welcome:
170-
if new_mem.last_name:
171-
fullname = "{} {}".format(first_name, new_mem.last_name)
162+
# If welcome message is media, send with appropriate function
163+
if (
164+
welc_type != sql.Types.TEXT
165+
and welc_type != sql.Types.BUTTON_TEXT
166+
):
167+
ENUM_FUNC_MAP[welc_type](chat.id, cust_welcome)
168+
return
169+
# else, move on
170+
first_name = (
171+
new_mem.first_name or "PersonWithNoName"
172+
) # edge case of empty name - occurs for some bugs.
173+
174+
if cust_welcome:
175+
if new_mem.last_name:
176+
fullname = "{} {}".format(first_name, new_mem.last_name)
177+
else:
178+
fullname = first_name
179+
count = chat.get_members_count()
180+
mention = mention_markdown(new_mem.id, first_name)
181+
if new_mem.username:
182+
username = "@" + escape_markdown(new_mem.username)
183+
else:
184+
username = mention
185+
186+
valid_format = escape_invalid_curly_brackets(
187+
cust_welcome, VALID_WELCOME_FORMATTERS
188+
)
189+
res = valid_format.format(
190+
first=escape_markdown(first_name),
191+
last=escape_markdown(new_mem.last_name or first_name),
192+
fullname=escape_markdown(fullname),
193+
username=username,
194+
mention=mention,
195+
count=count,
196+
chatname=escape_markdown(chat.title),
197+
id=new_mem.id,
198+
)
199+
buttons = sql.get_welc_buttons(chat.id)
200+
keyb = build_keyboard(buttons)
172201
else:
173-
fullname = first_name
174-
count = chat.get_members_count()
175-
mention = mention_markdown(new_mem.id, first_name)
176-
if new_mem.username:
177-
username = "@" + escape_markdown(new_mem.username)
178-
else:
179-
username = mention
202+
res = sql.DEFAULT_WELCOME.format(first=first_name)
203+
keyb = []
180204

181-
valid_format = escape_invalid_curly_brackets(
182-
cust_welcome, VALID_WELCOME_FORMATTERS
183-
)
184-
res = valid_format.format(
185-
first=escape_markdown(first_name),
186-
last=escape_markdown(new_mem.last_name or first_name),
187-
fullname=escape_markdown(fullname),
188-
username=username,
189-
mention=mention,
190-
count=count,
191-
chatname=escape_markdown(chat.title),
192-
id=new_mem.id,
193-
)
194-
buttons = sql.get_welc_buttons(chat.id)
195-
keyb = build_keyboard(buttons)
196-
else:
197-
res = sql.DEFAULT_WELCOME.format(first=first_name)
198-
keyb = []
205+
keyboard = InlineKeyboardMarkup(keyb)
199206

200-
keyboard = InlineKeyboardMarkup(keyb)
207+
sent = send(
208+
update,
209+
res,
210+
keyboard,
211+
sql.DEFAULT_WELCOME.format(first=first_name),
212+
) # type: Optional[Message]
201213

202-
sent = send(
203-
update,
204-
res,
205-
keyboard,
206-
sql.DEFAULT_WELCOME.format(first=first_name),
207-
) # type: Optional[Message]
214+
else:
215+
# BEGINNING THE BAN
216+
log = (
217+
"<b>CAS BAN:</b>" "\n#CASBAN" "\n<b>User:</b> {}".format(new_mem.id)
218+
)
208219

209-
else:
210-
# BEGINNING THE BAN
211-
log = (
212-
"<b>CAS BAN:</b>" "\n#CASBAN" "\n<b>User:</b> {}".format(new_mem.id)
213-
)
220+
reason = "Ban via CAS api query"
214221

215-
reason = "Ban via CAS api query"
222+
if reason:
223+
log += "\n<b>Motivo:</b> {}".format(reason)
216224

217-
if reason:
218-
log += "\n<b>Motivo:</b> {}".format(reason)
225+
user_id = new_mem.id
226+
try:
227+
chat.kick_member(user_id)
228+
bot.send_sticker(chat.id, BAN_STICKER) # banhammer electus sticker
229+
message.reply_text("BANNATO VIA SISTEMA AUTOMATICO CAS!")
230+
return log
219231

232+
except BadRequest as excp:
233+
if excp.message == "Reply message not found":
234+
# Do not reply
235+
message.reply_text("BANNATO!", quote=False)
236+
return log
237+
else:
238+
LOGGER.warning(update)
239+
LOGGER.exception(
240+
"ERROR in CAS banning user %s in chat %s (%s) due to %s",
241+
user_id,
242+
chat.title,
243+
chat.id,
244+
excp.message,
245+
)
246+
message.reply_text(
247+
"Diamine, non riesco a bannare questo utente."
248+
)
249+
else:
250+
# Kicking the user because of the username
220251
user_id = new_mem.id
221-
try:
222-
chat.kick_member(user_id)
223-
bot.send_sticker(chat.id, BAN_STICKER) # banhammer electus sticker
224-
message.reply_text("BANNATO VIA SISTEMA AUTOMATICO CAS!")
225-
return log
226-
227-
except BadRequest as excp:
228-
if excp.message == "Reply message not found":
229-
# Do not reply
230-
message.reply_text("BANNATO!", quote=False)
231-
return log
232-
else:
233-
LOGGER.warning(update)
234-
LOGGER.exception(
235-
"ERROR in CAS banning user %s in chat %s (%s) due to %s",
236-
user_id,
237-
chat.title,
238-
chat.id,
239-
excp.message,
240-
)
241-
message.reply_text(
242-
"Diamine, non riesco a bannare questo utente."
243-
)
252+
chat.kick_member(user_id, until_date=time.time() + 300)
253+
bot.send_sticker(chat.id, BAN_STICKER) # banhammer electus sticker
254+
message.reply_text("L'utente non ha uno username, quindi è stato rimosso.")
244255

245256
prev_welc = sql.get_clean_pref(chat.id)
246257
if prev_welc:

0 commit comments

Comments
 (0)