From cbfb4d6d325bdbef22199365dd31bef87df0b7c8 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 31 May 2019 01:18:03 +0300 Subject: [PATCH] Add command to change displayname --- mautrix_telegram/commands/telegram/account.py | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/mautrix_telegram/commands/telegram/account.py b/mautrix_telegram/commands/telegram/account.py index dd2a17ed..15add619 100644 --- a/mautrix_telegram/commands/telegram/account.py +++ b/mautrix_telegram/commands/telegram/account.py @@ -17,10 +17,10 @@ from typing import Dict, Optional from telethon.errors import (UsernameInvalidError, UsernameNotModifiedError, UsernameOccupiedError, - HashInvalidError, AuthKeyError) + HashInvalidError, AuthKeyError, FirstNameInvalidError) from telethon.tl.types import Authorization from telethon.tl.functions.account import (UpdateUsernameRequest, GetAuthorizationsRequest, - ResetAuthorizationRequest) + ResetAuthorizationRequest, UpdateProfileRequest) from .. import command_handler, CommandEvent, SECTION_AUTH @@ -53,6 +53,25 @@ async def username(evt: CommandEvent) -> Optional[Dict]: await evt.reply(f"Username changed to {evt.sender.username}") +@command_handler(needs_auth=True, help_section=SECTION_AUTH, help_args="<_new displayname_>", + help_text="Change your Telegram displayname.") +async def displayname(evt: CommandEvent) -> Optional[Dict]: + if len(evt.args) == 0: + return await evt.reply("**Usage:** `$cmdprefix+sp displayname `") + if evt.sender.is_bot: + return await evt.reply("Bots can't set their own displayname.") + + first_name, last_name = ((evt.args[0], "") + if len(evt.args) == 1 + else (" ".join(evt.args[:-1]), evt.args[-1])) + try: + await evt.sender.client(UpdateProfileRequest(first_name=first_name, last_name=last_name)) + except FirstNameInvalidError: + return await evt.reply("Invalid first name") + await evt.sender.update_info() + await evt.reply("Displayname updated") + + def _format_session(sess: Authorization) -> str: return (f"**{sess.app_name} {sess.app_version}** \n" f" **Platform:** {sess.device_model} {sess.platform} {sess.system_version} \n"