Use username for invite link and fix other things
Fixes #50 Other things: * Display flood wait errors from commands * (Maybe) Fix removing room aliases when removing channel username
This commit is contained in:
@@ -32,6 +32,24 @@ def command_handler(func):
|
|||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
||||||
|
def format_duration(seconds):
|
||||||
|
def pluralize(count, singular): return singular if count == 1 else singular + "s"
|
||||||
|
|
||||||
|
def include(count, word): return f"{count} {pluralize(count, word)}" if count > 0 else ""
|
||||||
|
|
||||||
|
minutes, seconds = divmod(seconds, 60)
|
||||||
|
hours, minutes = divmod(minutes, 60)
|
||||||
|
days, hours = divmod(hours, 24)
|
||||||
|
parts = [a for a in [
|
||||||
|
include(days, "day"),
|
||||||
|
include(hours, "hour"),
|
||||||
|
include(minutes, "minute"),
|
||||||
|
include(seconds, "second")] if a]
|
||||||
|
if len(parts) > 2:
|
||||||
|
return "{} and {}".format(", ".join(parts[:-1]), parts[-1])
|
||||||
|
return " and ".join(parts)
|
||||||
|
|
||||||
|
|
||||||
class CommandHandler:
|
class CommandHandler:
|
||||||
def __init__(self, context):
|
def __init__(self, context):
|
||||||
self.az, self.db, log, self.config = context
|
self.az, self.db, log, self.config = context
|
||||||
@@ -47,6 +65,8 @@ class CommandHandler:
|
|||||||
with self.handler(sender, room, command, args, is_management, is_portal) as handle_command:
|
with self.handler(sender, room, command, args, is_management, is_portal) as handle_command:
|
||||||
try:
|
try:
|
||||||
handle_command(self, sender, args)
|
handle_command(self, sender, args)
|
||||||
|
except FloodWaitError as e:
|
||||||
|
self.reply(f"Flood error: Please wait {format_duration(e.seconds)}")
|
||||||
except Exception:
|
except Exception:
|
||||||
self.reply("Fatal error while handling command. Check logs for more details.")
|
self.reply("Fatal error while handling command. Check logs for more details.")
|
||||||
self.log.exception(f"Fatal error handling command "
|
self.log.exception(f"Fatal error handling command "
|
||||||
@@ -420,7 +440,6 @@ class CommandHandler:
|
|||||||
except UsernameInvalidError:
|
except UsernameInvalidError:
|
||||||
return self.reply("Invalid username")
|
return self.reply("Invalid username")
|
||||||
|
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
# region Command-related commands
|
# region Command-related commands
|
||||||
@command_handler
|
@command_handler
|
||||||
|
|||||||
@@ -223,7 +223,7 @@ class Portal:
|
|||||||
if self.username != username:
|
if self.username != username:
|
||||||
if self.username:
|
if self.username:
|
||||||
self.main_intent.remove_room_alias(self._get_room_alias())
|
self.main_intent.remove_room_alias(self._get_room_alias())
|
||||||
self.username = username
|
self.username = username or None
|
||||||
if self.username:
|
if self.username:
|
||||||
self.main_intent.add_room_alias(self.mxid, self._get_room_alias())
|
self.main_intent.add_room_alias(self.mxid, self._get_room_alias())
|
||||||
return True
|
return True
|
||||||
@@ -282,6 +282,8 @@ class Portal:
|
|||||||
elif self.peer_type == "chat":
|
elif self.peer_type == "chat":
|
||||||
link = user.client(ExportChatInviteRequest(chat_id=self.tgid))
|
link = user.client(ExportChatInviteRequest(chat_id=self.tgid))
|
||||||
elif self.peer_type == "channel":
|
elif self.peer_type == "channel":
|
||||||
|
if self.username:
|
||||||
|
return f"https://t.me/{self.username}"
|
||||||
link = user.client(
|
link = user.client(
|
||||||
ExportInviteRequest(channel=self.get_input_entity(user)))
|
ExportInviteRequest(channel=self.get_input_entity(user)))
|
||||||
else:
|
else:
|
||||||
@@ -484,9 +486,8 @@ class Portal:
|
|||||||
if self.peer_type != "channel":
|
if self.peer_type != "channel":
|
||||||
raise ValueError("Only channels and supergroups have usernames.")
|
raise ValueError("Only channels and supergroups have usernames.")
|
||||||
success = source.client(UpdateUsernameRequest(self.get_input_entity(source), username))
|
success = source.client(UpdateUsernameRequest(self.get_input_entity(source), username))
|
||||||
if success:
|
if self.update_username(username):
|
||||||
if self.update_username(username):
|
self.save()
|
||||||
self.save()
|
|
||||||
|
|
||||||
def create_telegram_chat(self, source, supergroup=False):
|
def create_telegram_chat(self, source, supergroup=False):
|
||||||
if not self.mxid:
|
if not self.mxid:
|
||||||
|
|||||||
Reference in New Issue
Block a user