2121from .. import api
2222from .base import BaseObject , multiple
2323from . import mixins
24+ from datetime import datetime as dt
25+ from time import mktime
26+
2427
2528from .media import Photo
2629
@@ -38,6 +41,7 @@ class User(BaseObject, mixins.ChatMixin):
3841 optional = {
3942 "last_name" : str ,
4043 "username" : str ,
44+ "is_bot" : bool
4145 }
4246 _check_equality_ = "id"
4347
@@ -102,6 +106,13 @@ class Chat(BaseObject, mixins.ChatMixin):
102106 "username" : str ,
103107 "first_name" : str ,
104108 "last_name" : str ,
109+ "all_members_are_administrators" : bool ,
110+ "description" : str ,
111+ "invite_link" : str ,
112+ # This is added at the bottom of messages.py due to circular imports
113+ # "pinned_message" = Message
114+ "sticker_set_name" : str ,
115+ "can_set_sticker_set" : bool
105116 }
106117 _check_equality_ = "id"
107118
@@ -260,6 +271,115 @@ def unban(self, user):
260271 "user_id" : user ,
261272 })
262273
274+ def kick (self , user , time = None ):
275+ if self .type == "private" :
276+ raise RuntimeError ("This chat is a private!" )
277+ if isinstance (user , User ):
278+ user = user .id
279+
280+ if time is None :
281+ self ._api .call ("unbanChatMember" , {
282+ "chat_id" : self .id ,
283+ "user_id" : user
284+ })
285+ else :
286+ if isinstance (time , dt ):
287+ time = mktime (time .timetuple ())
288+ self ._api .call ("kickChatMember" , {
289+ "chat_id" : self .id ,
290+ "user_id" : user ,
291+ "until_date" : time
292+ })
293+
294+ def permissions (self , user ):
295+ return Permissions (user , self )
296+
297+
298+ class Permissions :
299+ def __init__ (self , user , chat ):
300+ if chat .type not in ("group" , "supergroup" ):
301+ raise RuntimeError ("This chat is not a group or a supergroup!" )
302+ # Accept also an instance of `User`
303+ self ._chatid = chat .id
304+ self ._api = chat ._api
305+ if isinstance (user , User ):
306+ self ._user = user .id
307+ else :
308+ self ._user = user
309+ infouser = self ._api .call ("getChatMember" , {
310+ "chat_id" : self ._chatid ,
311+ "user_id" : self ._user },
312+ expect = ChatMember )
313+
314+ try :
315+ self ._until_date = infouser .until_date
316+ except AttributeError :
317+ self ._until_date = 0
318+ self .until_date = self ._until_date
319+
320+ try :
321+ self ._send_messages = infouser .can_send_messages
322+ except AttributeError :
323+ self ._send_messages = True
324+ self .send_messages = self ._send_messages
325+
326+ try :
327+ self ._send_media_messages = infouser .can_media_messages
328+ except AttributeError :
329+ self ._send_media_messages = True
330+ self .send_media_messages = self ._send_media_messages
331+
332+ try :
333+ self ._send_other_messages = infouser .can_send_other_messages
334+ except AttributeError :
335+ self ._send_other_messages = True
336+ self .send_other_messages = self ._send_other_messages
337+
338+ try :
339+ self ._add_web_page_previews = infouser .can_add_web_page_previews
340+ except AttributeError :
341+ self ._add_web_page_previews = True
342+ self .add_web_page_previews = self ._add_web_page_previews
343+
344+ def __enter__ (self ):
345+ return self
346+
347+ def __exit__ (self , exc_type , exc_val , exc_tb ):
348+ if exc_type is None :
349+ self .save ()
350+
351+ def save (self ):
352+ arguments = {
353+ "chat_id" : self .chatid ,
354+ "user_id" : self .user
355+ }
356+ modify = False
357+
358+ if isinstance (self .until_date , dt ):
359+ self .until_date = mktime (self .until_date .timetuple ())
360+ modify = True
361+ if self ._until_date != self .until_date :
362+ arguments .update ({"until_date" : self .until_date })
363+ modify = True
364+ if self ._send_messages != self .send_messages :
365+ arguments .update ({"can_send_messages" : self .send_messages })
366+ modify = True
367+ if self ._send_media_messages != self .send_media_messages :
368+ arguments .update ({"can_send_media_messages" :
369+ self .send_media_messages })
370+ modify = True
371+ if self ._send_other_messages != self .send_other_messages :
372+ arguments .update ({"can_send_other_messages" :
373+ self .send_other_messages })
374+ modify = True
375+ if self ._add_web_page_previews != self .add_web_page_previews :
376+ arguments .update ({"can_add_web_page_previews" :
377+ self .add_web_page_previews })
378+ modify = True
379+
380+ if modify :
381+ self ._api .call ("restrictChatMember" , arguments )
382+
263383
264384class ChatMember (BaseObject ):
265385 """Telegram API representation of a chat member
@@ -271,6 +391,22 @@ class ChatMember(BaseObject):
271391 "user" : User ,
272392 "status" : str ,
273393 }
394+ optional = {
395+ "until_date" : int ,
396+ "can_be_edited" : bool ,
397+ "can_change_info" : bool ,
398+ "can_post_messages" : bool ,
399+ "can_edit_messages" : bool ,
400+ "can_delete_messages" : bool ,
401+ "can_invite_users" : bool ,
402+ "can_restrict_members" : bool ,
403+ "can_pin_messages" : bool ,
404+ "can_promote_members" : bool ,
405+ "can_send_messages" : bool ,
406+ "can_send_media_messages" : bool ,
407+ "can_send_other_messages" : bool ,
408+ "can_add_web_page_previews" : bool
409+ }
274410 _check_equality_ = "user"
275411
276412
@@ -284,3 +420,9 @@ class UserProfilePhotos(BaseObject):
284420 "total_count" : int ,
285421 "photos" : multiple (Photo ),
286422 }
423+
424+
425+ # add this code on the button to avoid import loop
426+ # flake8: noqa
427+ from .messages import Message
428+ Chat .optional ["pinned_message" ] = Message
0 commit comments