Strict ID match replaced with Username search

This commit is contained in:
Profitroll 2022-12-06 11:10:44 +01:00
parent 27e63111e5
commit 4363ef9d78

View File

@ -10,16 +10,33 @@ async def cmd_message(app, msg):
if msg.chat.id == configGet("admin_group") or await isAnAdmin(msg.from_user.id):
try:
destination = int(msg.command[1])
try:
destination = await app.get_users(int(msg.command[1]))
if destination == [] or destination == None:
raise TypeError
except TypeError:
try:
destination = await app.get_users(msg.command[1])
except bad_request_400.UsernameNotOccupied:
await msg.reply_text(locale("message_no_user", "message"), quote=should_quote(msg))
logWrite(f"Admin {msg.from_user.id} tried to send message '{' '.join(msg.command[2:])}' to '{msg.command[1]}' but 'UsernameNotOccupied'")
return
except ValueError:
try:
destination = await app.get_users(msg.command[1])
except bad_request_400.UsernameNotOccupied:
await msg.reply_text(locale("message_no_user", "message"), quote=should_quote(msg))
logWrite(f"Admin {msg.from_user.id} tried to send message '{' '.join(msg.command[2:])}' to '{msg.command[1]}' but 'UsernameNotOccupied'")
return
void = msg.command[2]
message = " ".join(msg.command[2:])
try:
await app.send_message(destination, message)
await app.send_message(destination.id, message)
await msg.reply_text(locale("message_sent", "message"), quote=should_quote(msg))
logWrite(f"Admin {msg.from_user.id} sent message '{' '.join(msg.command[2:])}' to {destination}")
logWrite(f"Admin {msg.from_user.id} sent message '{' '.join(msg.command[2:])}' to {destination.id}")
except bad_request_400.PeerIdInvalid:
await msg.reply_text(locale("message_no_user", "message"), quote=should_quote(msg))
logWrite(f"Admin {msg.from_user.id} tried to send message '{' '.join(msg.command[2:])}' to {destination} but 'PeerIdInvalid'")
logWrite(f"Admin {msg.from_user.id} tried to send message '{' '.join(msg.command[2:])}' to {destination.id} but 'PeerIdInvalid'")
except IndexError:
await msg.reply_text(locale("message_invalid_syntax", "message"), quote=should_quote(msg))
logWrite(f"Admin {msg.from_user.id} tried to send message but 'IndexError'")