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,8 @@ class User(BaseObject, mixins.ChatMixin):
3841 optional = {
3942 "last_name" : str ,
4043 "username" : str ,
44+ "language_code" : str ,
45+ "is_bot" : bool
4146 }
4247 _check_equality_ = "id"
4348
@@ -102,6 +107,13 @@ class Chat(BaseObject, mixins.ChatMixin):
102107 "username" : str ,
103108 "first_name" : str ,
104109 "last_name" : str ,
110+ "all_members_are_administrators" : bool ,
111+ "description" : str ,
112+ "invite_link" : str ,
113+ # This is added at the bottom of messages.py due to circular imports
114+ # "pinned_message" = Message
115+ "sticker_set_name" : str ,
116+ "can_set_sticker_set" : bool
105117 }
106118 _check_equality_ = "id"
107119
@@ -260,6 +272,112 @@ def unban(self, user):
260272 "user_id" : user ,
261273 })
262274
275+ def kick (self , user , time = None ):
276+ if self .type == "private" :
277+ raise RuntimeError ("This chat is a private!" )
278+ if isinstance (user , User ):
279+ user = user .id
280+
281+ if time is None :
282+ self ._api .call ("unbanChatMember" , {
283+ "chat_id" : self .id ,
284+ "user_id" : user
285+ })
286+ else :
287+ if isinstance (time , dt ):
288+ time = mktime (time .timetuple ())
289+ self ._api .call ("kickChatMember" , {
290+ "chat_id" : self .id ,
291+ "user_id" : user ,
292+ "until_date" : time
293+ })
294+
295+ def permissions (self , user ):
296+ return Permissions (user , self )
297+
298+
299+ class Permissions :
300+ def __init__ (self , user , chat ):
301+ if chat .type not in ("group" , "supergroup" ):
302+ raise RuntimeError ("This chat is not a group or a supergroup!" )
303+ # Accept also an instance of `User`
304+ self ._chatid = chat .id
305+ self ._api = chat ._api
306+ if isinstance (user , User ):
307+ self ._user = user .id
308+ else :
309+ self ._user = user
310+ infouser = self ._api .call ("getChatMember" , {
311+ "chat_id" : self ._chatid ,
312+ "user_id" : self ._user },
313+ expect = ChatMember )
314+
315+ try :
316+ self ._until_date = infouser .until_date
317+ except AttributeError :
318+ self ._until_date = 0
319+ self .until_date = self ._until_date
320+
321+ try :
322+ self ._send_messages = infouser .can_send_messages
323+ except AttributeError :
324+ self ._send_messages = True
325+ self .send_messages = self ._send_messages
326+
327+ try :
328+ self ._send_media_messages = infouser .can_media_messages
329+ except AttributeError :
330+ self ._send_media_messages = True
331+ self .send_media_messages = self ._send_media_messages
332+
333+ try :
334+ self ._send_other_messages = infouser .can_send_other_messages
335+ except AttributeError :
336+ self ._send_other_messages = True
337+ self .send_other_messages = self ._send_other_messages
338+
339+ try :
340+ self ._add_web_page_previews = infouser .can_add_web_page_previews
341+ except AttributeError :
342+ self ._add_web_page_previews = True
343+ self .add_web_page_previews = self ._add_web_page_previews
344+
345+ def __enter__ (self ):
346+ return self
347+
348+ def __exit__ (self , exc_type , exc_val , exc_tb ):
349+ if exc_type is None :
350+ self .save ()
351+
352+ def save (self ):
353+ arguments = {
354+ "chat_id" : self .chatid ,
355+ "user_id" : self .user
356+ }
357+ modify = False
358+
359+ if isinstance (self .until_date , dt ):
360+ self .until_date = mktime (self .until_date .timetuple ())
361+ modify = True
362+ if self ._until_date != self .until_date :
363+ arguments .update ({"until_date" : self .until_date })
364+ modify = True
365+ if self ._send_messages != self .send_messages :
366+ arguments .update ({"can_send_messages" : self .send_messages })
367+ modify = True
368+ if self ._send_media_messages != self .send_media_messages :
369+ arguments .update ({"can_send_media_messages" : self .send_media_messages })
370+ modify = True
371+ if self ._send_other_messages != self .send_other_messages :
372+ arguments .update ({"can_send_other_messages" : self .send_other_messages })
373+ modify = True
374+ if self ._add_web_page_previews != self .add_web_page_previews :
375+ arguments .update ({"can_add_web_page_previews" : self .add_web_page_previews })
376+ modify = True
377+
378+ if modify :
379+ self ._api .call ("restrictChatMember" , arguments )
380+
263381
264382class ChatMember (BaseObject ):
265383 """Telegram API representation of a chat member
@@ -271,6 +389,22 @@ class ChatMember(BaseObject):
271389 "user" : User ,
272390 "status" : str ,
273391 }
392+ optional = {
393+ "until_date" : int ,
394+ "can_be_edited" : bool ,
395+ "can_change_info" : bool ,
396+ "can_post_messages" : bool ,
397+ "can_edit_messages" : bool ,
398+ "can_delete_messages" : bool ,
399+ "can_invite_users" : bool ,
400+ "can_restrict_members" : bool ,
401+ "can_pin_messages" : bool ,
402+ "can_promote_members" : bool ,
403+ "can_send_messages" : bool ,
404+ "can_send_media_messages" : bool ,
405+ "can_send_other_messages" : bool ,
406+ "can_add_web_page_previews" : bool
407+ }
274408 _check_equality_ = "user"
275409
276410
@@ -284,3 +418,9 @@ class UserProfilePhotos(BaseObject):
284418 "total_count" : int ,
285419 "photos" : multiple (Photo ),
286420 }
421+
422+
423+ # add this code on the button to avoid import loop
424+ # flake8: noqa
425+ from .messages import Message
426+ Chat .optional ["pinned_message" ] = Message
0 commit comments