|
1 | 1 | import html |
| 2 | +import time |
2 | 3 | from typing import Optional, List |
3 | 4 |
|
4 | 5 | import requests as req |
|
18 | 19 | escape_invalid_curly_brackets, |
19 | 20 | ) |
20 | 21 | from tg_bot.modules.log_channel import loggable |
| 22 | +from tg_bot import BAN_STICKER |
21 | 23 |
|
22 | 24 | VALID_WELCOME_FORMATTERS = [ |
23 | 25 | "first", |
@@ -129,118 +131,127 @@ def cas_banned(userid): |
129 | 131 | @run_async |
130 | 132 | def new_member(bot: Bot, update: Update): |
131 | 133 | chat = update.effective_chat # type: Optional[Chat] |
| 134 | + new_members = update.effective_message.new_chat_members |
| 135 | + message = update.effective_message |
132 | 136 |
|
133 | 137 | should_welc, cust_welcome, welc_type = sql.get_welc_pref(chat.id) |
134 | 138 | if should_welc: |
135 | 139 | sent = None |
136 | | - new_members = update.effective_message.new_chat_members |
137 | 140 | 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 |
146 | 151 |
|
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 |
150 | 155 |
|
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 | + ) |
156 | 161 |
|
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) |
172 | 201 | 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 = [] |
180 | 204 |
|
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) |
199 | 206 |
|
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] |
201 | 213 |
|
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 | + ) |
208 | 219 |
|
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" |
214 | 221 |
|
215 | | - reason = "Ban via CAS api query" |
| 222 | + if reason: |
| 223 | + log += "\n<b>Motivo:</b> {}".format(reason) |
216 | 224 |
|
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 |
219 | 231 |
|
| 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 |
220 | 251 | 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.") |
244 | 255 |
|
245 | 256 | prev_welc = sql.get_clean_pref(chat.id) |
246 | 257 | if prev_welc: |
|
0 commit comments