MTPyroger/pyrogram/methods/chats/set_administrator_title.py

117 lines
4.0 KiB
Python
Raw Normal View History

2020-03-21 14:43:32 +00:00
# Pyrogram - Telegram MTProto API Client Library for Python
2021-01-01 21:58:48 +00:00
# Copyright (C) 2017-2021 Dan <https://github.com/delivrance>
2019-09-07 15:08:30 +00:00
#
2020-03-21 14:43:32 +00:00
# This file is part of Pyrogram.
2019-09-07 15:08:30 +00:00
#
2020-03-21 14:43:32 +00:00
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
2019-09-07 15:08:30 +00:00
#
2020-03-21 14:43:32 +00:00
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
2019-09-07 15:08:30 +00:00
#
2020-03-21 14:43:32 +00:00
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
2019-09-07 15:08:30 +00:00
from typing import Union
from pyrogram import raw
from pyrogram.scaffold import Scaffold
2019-09-07 15:08:30 +00:00
class SetAdministratorTitle(Scaffold):
async def set_administrator_title(
2019-09-07 15:08:30 +00:00
self,
chat_id: Union[int, str],
user_id: Union[int, str],
title: str,
) -> bool:
"""Set a custom title (rank) to an administrator of a supergroup.
If you are an administrator of a supergroup (i.e. not the owner), you can only set the title of other
administrators who have been promoted by you. If you are the owner, you can change every administrator's title.
2019-09-07 15:08:30 +00:00
Parameters:
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target chat.
user_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target user.
For a contact that exists in your Telegram address book you can use his phone number (str).
title (``str``, *optional*):
A custom title that will be shown to all members instead of "Owner" or "Admin".
Pass None or "" (empty string) to remove the custom title.
Returns:
``bool``: True on success.
Example:
.. code-block:: python
2022-01-07 09:18:51 +00:00
app.set_administrator_title(chat_id, user_id, "Admin Title")
2019-09-07 15:08:30 +00:00
"""
chat_id = await self.resolve_peer(chat_id)
user_id = await self.resolve_peer(user_id)
2019-09-07 15:08:30 +00:00
r = (await self.send(
raw.functions.channels.GetParticipant(
2019-09-07 15:08:30 +00:00
channel=chat_id,
participant=user_id
2019-09-07 15:08:30 +00:00
)
)).participant
2019-09-07 15:08:30 +00:00
if isinstance(r, raw.types.ChannelParticipantCreator):
admin_rights = raw.types.ChatAdminRights(
2019-09-07 15:08:30 +00:00
change_info=True,
post_messages=True,
edit_messages=True,
delete_messages=True,
ban_users=True,
invite_users=True,
pin_messages=True,
add_admins=True,
)
elif isinstance(r, raw.types.ChannelParticipantAdmin):
2019-09-07 15:08:30 +00:00
admin_rights = r.admin_rights
else:
raise ValueError("Custom titles can only be applied to owners or administrators of supergroups")
if not admin_rights.change_info:
admin_rights.change_info = None
if not admin_rights.post_messages:
admin_rights.post_messages = None
if not admin_rights.edit_messages:
admin_rights.edit_messages = None
if not admin_rights.delete_messages:
admin_rights.delete_messages = None
if not admin_rights.ban_users:
admin_rights.ban_users = None
if not admin_rights.invite_users:
admin_rights.invite_users = None
if not admin_rights.pin_messages:
admin_rights.pin_messages = None
if not admin_rights.add_admins:
admin_rights.add_admins = None
await self.send(
raw.functions.channels.EditAdmin(
2019-09-07 15:08:30 +00:00
channel=chat_id,
user_id=user_id,
admin_rights=admin_rights,
rank=title
)
)
return True