1616 escape_invalid_curly_brackets
1717from tg_bot .modules .log_channel import loggable
1818
19+ import requests as req
20+
1921VALID_WELCOME_FORMATTERS = ['first' , 'last' , 'fullname' , 'username' , 'id' , 'count' , 'chatname' , 'mention' ]
2022
2123ENUM_FUNC_MAP = {
@@ -75,6 +77,21 @@ def send(update, message, keyboard, backup_message):
7577
7678 return msg
7779
80+ def CasBanned (userid ):
81+ # Query cas
82+ try : # Just in case the CAS api endpoint goes down
83+ response = req .get ("https://combot.org/api/cas/check?user_id=" + char (userid ))
84+ response = response .json ()
85+ # if true the user should be banned
86+ if response ["ok" ] == True :
87+ return True # The user should be banned
88+ else :
89+ return False # The user should not be banned
90+ except Exception as e :
91+ LOGGER .warning (e )
92+ return False
93+
94+
7895
7996@run_async
8097def new_member (bot : Bot , update : Update ):
@@ -85,53 +102,84 @@ def new_member(bot: Bot, update: Update):
85102 sent = None
86103 new_members = update .effective_message .new_chat_members
87104 for new_mem in new_members :
88- # Give the owner a special welcome
89- if new_mem .id == OWNER_ID :
90- update .effective_message .reply_text ("È arrivato il capooooo, diamo il via alla festa!" )
91- continue
105+ # Check if the user is cas-banned
106+ if not CasBanned (new_mem .id ):
107+ # Give the owner a special welcome
108+ if new_mem .id == OWNER_ID :
109+ update .effective_message .reply_text ("È arrivato il capooooo, diamo il via alla festa!" )
110+ continue
92111
93- # Don't welcome yourself
94- elif new_mem .id == bot .id :
95- continue
112+ # Don't welcome yourself
113+ elif new_mem .id == bot .id :
114+ continue
96115
97- else :
98- # Muting new users
99- bot .restrict_chat_member (chat .id , new_mem .id , can_send_messages = False )
100-
101- # If welcome message is media, send with appropriate function
102- if welc_type != sql .Types .TEXT and welc_type != sql .Types .BUTTON_TEXT :
103- ENUM_FUNC_MAP [welc_type ](chat .id , cust_welcome )
104- return
105- # else, move on
106- first_name = new_mem .first_name or "PersonWithNoName" # edge case of empty name - occurs for some bugs.
107-
108- if cust_welcome :
109- if new_mem .last_name :
110- fullname = "{} {}" .format (first_name , new_mem .last_name )
111- else :
112- fullname = first_name
113- count = chat .get_members_count ()
114- mention = mention_markdown (new_mem .id , first_name )
115- if new_mem .username :
116- username = "@" + escape_markdown (new_mem .username )
117- else :
118- username = mention
119-
120- valid_format = escape_invalid_curly_brackets (cust_welcome , VALID_WELCOME_FORMATTERS )
121- res = valid_format .format (first = escape_markdown (first_name ),
122- last = escape_markdown (new_mem .last_name or first_name ),
123- fullname = escape_markdown (fullname ), username = username , mention = mention ,
124- count = count , chatname = escape_markdown (chat .title ), id = new_mem .id )
125- buttons = sql .get_welc_buttons (chat .id )
126- keyb = build_keyboard (buttons )
127116 else :
128- res = sql .DEFAULT_WELCOME .format (first = first_name )
129- keyb = []
117+ # Muting new users
118+ bot .restrict_chat_member (chat .id , new_mem .id , can_send_messages = False )
119+
120+ # If welcome message is media, send with appropriate function
121+ if welc_type != sql .Types .TEXT and welc_type != sql .Types .BUTTON_TEXT :
122+ ENUM_FUNC_MAP [welc_type ](chat .id , cust_welcome )
123+ return
124+ # else, move on
125+ first_name = new_mem .first_name or "PersonWithNoName" # edge case of empty name - occurs for some bugs.
126+
127+ if cust_welcome :
128+ if new_mem .last_name :
129+ fullname = "{} {}" .format (first_name , new_mem .last_name )
130+ else :
131+ fullname = first_name
132+ count = chat .get_members_count ()
133+ mention = mention_markdown (new_mem .id , first_name )
134+ if new_mem .username :
135+ username = "@" + escape_markdown (new_mem .username )
136+ else :
137+ username = mention
138+
139+ valid_format = escape_invalid_curly_brackets (cust_welcome , VALID_WELCOME_FORMATTERS )
140+ res = valid_format .format (first = escape_markdown (first_name ),
141+ last = escape_markdown (new_mem .last_name or first_name ),
142+ fullname = escape_markdown (fullname ), username = username , mention = mention ,
143+ count = count , chatname = escape_markdown (chat .title ), id = new_mem .id )
144+ buttons = sql .get_welc_buttons (chat .id )
145+ keyb = build_keyboard (buttons )
146+ else :
147+ res = sql .DEFAULT_WELCOME .format (first = first_name )
148+ keyb = []
130149
131- keyboard = InlineKeyboardMarkup (keyb )
150+ keyboard = InlineKeyboardMarkup (keyb )
151+
152+ sent = send (update , res , keyboard ,
153+ sql .DEFAULT_WELCOME .format (first = first_name )) # type: Optional[Message]
132154
133- sent = send (update , res , keyboard ,
134- sql .DEFAULT_WELCOME .format (first = first_name )) # type: Optional[Message]
155+ else :
156+ # BEGINNING THE BAN
157+ log = "<b>CAS BAN:</b>" \
158+ "\n #CASBAN" \
159+ "\n <b>User:</b> {}" .format (new_mem .id )
160+
161+ reason = "Ban via CAS api query"
162+
163+ if reason :
164+ log += "\n <b>Motivo:</b> {}" .format (reason )
165+
166+ user_id = new_mem .id
167+ try :
168+ chat .kick_member (user_id )
169+ bot .send_sticker (chat .id , BAN_STICKER ) # banhammer marie sticker
170+ message .reply_text ("BANNATO VIA SISTEMA AUTOMATICO CAS!" )
171+ return log
172+
173+ except BadRequest as excp :
174+ if excp .message == "Reply message not found" :
175+ # Do not reply
176+ message .reply_text ('BANNATO!' , quote = False )
177+ return log
178+ else :
179+ LOGGER .warning (update )
180+ LOGGER .exception ("ERROR in CAS banning user %s in chat %s (%s) due to %s" , user_id , chat .title , chat .id ,
181+ excp .message )
182+ message .reply_text ("Diamine, non riesco a bannare questo utente." )
135183
136184 prev_welc = sql .get_clean_pref (chat .id )
137185 if prev_welc :
0 commit comments