🐛 性能优化 + block
, unblock
指令优化 (#144)
* [upd] `block` and `unblock` command improvement * [fix] Bug fixing * [upd] Use `ensure_future` for `save_id`
This commit is contained in:
parent
68df2db9bc
commit
3fb0578995
@ -1,5 +1,6 @@
|
|||||||
""" PagerMaid initialization. """
|
""" PagerMaid initialization. """
|
||||||
|
|
||||||
|
import asyncio
|
||||||
from concurrent.futures import CancelledError
|
from concurrent.futures import CancelledError
|
||||||
|
|
||||||
# Analytics
|
# Analytics
|
||||||
@ -282,7 +283,7 @@ async def save_id():
|
|||||||
|
|
||||||
|
|
||||||
with bot:
|
with bot:
|
||||||
bot.loop.run_until_complete(save_id())
|
asyncio.ensure_future(save_id())
|
||||||
|
|
||||||
|
|
||||||
def before_send(event, hint):
|
def before_send(event, hint):
|
||||||
|
@ -8,7 +8,7 @@ from telethon.tl.functions.account import UpdateProfileRequest, UpdateUsernameRe
|
|||||||
from telethon.tl.functions.photos import DeletePhotosRequest, GetUserPhotosRequest, UploadProfilePhotoRequest
|
from telethon.tl.functions.photos import DeletePhotosRequest, GetUserPhotosRequest, UploadProfilePhotoRequest
|
||||||
from telethon.tl.functions.users import GetFullUserRequest
|
from telethon.tl.functions.users import GetFullUserRequest
|
||||||
from telethon.tl.functions.contacts import BlockRequest, UnblockRequest
|
from telethon.tl.functions.contacts import BlockRequest, UnblockRequest
|
||||||
from telethon.tl.types import InputPhoto, MessageMediaPhoto, MessageEntityMentionName, MessageEntityPhone
|
from telethon.tl.types import InputPhoto, MessageMediaPhoto, MessageEntityMentionName, MessageEntityPhone, User
|
||||||
from struct import error as StructError
|
from struct import error as StructError
|
||||||
from pagermaid import bot, log
|
from pagermaid import bot, log
|
||||||
from pagermaid.utils import lang, alias_command
|
from pagermaid.utils import lang, alias_command
|
||||||
@ -277,41 +277,41 @@ async def profile(context):
|
|||||||
|
|
||||||
@listener(is_plugin=False, outgoing=True, command=alias_command('block'),
|
@listener(is_plugin=False, outgoing=True, command=alias_command('block'),
|
||||||
description=lang('block_des'),
|
description=lang('block_des'),
|
||||||
parameters="<username/uid/reply>")
|
parameters="(username/uid/reply)")
|
||||||
async def block_user(context):
|
async def block_user(context):
|
||||||
""" Block an user. """
|
""" Block an user. """
|
||||||
|
current_chat = await context.get_chat()
|
||||||
if len(context.parameter) > 1:
|
if len(context.parameter) > 1:
|
||||||
await context.edit(f"{lang('error_prefix')}{lang('arg_error')}")
|
await context.edit(f"{lang('error_prefix')}{lang('arg_error')}")
|
||||||
return
|
return
|
||||||
|
|
||||||
await context.edit(lang('block_process'))
|
await context.edit(lang('block_process'))
|
||||||
user = None
|
user = None
|
||||||
if context.reply_to_msg_id:
|
# Priority: reply > argument > current_chat
|
||||||
|
if context.reply_to_msg_id: # Reply to a user
|
||||||
reply_message = await context.get_reply_message()
|
reply_message = await context.get_reply_message()
|
||||||
user = reply_message.sender_id
|
if reply_message and reply_message.sender_id is not None:
|
||||||
else:
|
user = reply_message.sender_id
|
||||||
if len(context.parameter) == 1:
|
if not user and len(context.parameter) == 1: # Argument provided
|
||||||
[user] = context.parameter
|
(raw_user,) = context.parameter
|
||||||
if user.isnumeric():
|
if raw_user.isnumeric():
|
||||||
user = int(user)
|
user = int(raw_user)
|
||||||
elif context.message.entities is not None:
|
elif context.message.entities is not None:
|
||||||
if isinstance(context.message.entities[0], MessageEntityMentionName):
|
if isinstance(context.message.entities[0], MessageEntityMentionName):
|
||||||
user = context.message.entities[0].user_id
|
user = context.message.entities[0].user_id
|
||||||
else:
|
if not user and isinstance(current_chat, User):
|
||||||
user_object = await context.client.get_me()
|
user = current_chat.id
|
||||||
user = user_object.id
|
|
||||||
result = None
|
|
||||||
if not user:
|
if not user:
|
||||||
await context.edit(f"{lang('error_prefix')}{lang('arg_error')}")
|
await context.edit(f"{lang('error_prefix')}{lang('arg_error')}")
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
result = await context.client(BlockRequest(id=user))
|
if await context.client(BlockRequest(id=user)):
|
||||||
except Exception:
|
await context.edit(f"{lang('block_success')} `{user}`")
|
||||||
|
except Exception: # noqa
|
||||||
pass
|
pass
|
||||||
if result:
|
await context.edit(f"`{user}` {lang('block_exist')}")
|
||||||
await context.edit(f"{lang('block_success')} `{user}`")
|
if isinstance(current_chat, User) and current_chat.id == user:
|
||||||
else:
|
await context.delete()
|
||||||
await context.edit(f"`{user}` {lang('block_exist')}")
|
|
||||||
|
|
||||||
|
|
||||||
@listener(is_plugin=False, outgoing=True, command=alias_command('unblock'),
|
@listener(is_plugin=False, outgoing=True, command=alias_command('unblock'),
|
||||||
@ -327,27 +327,21 @@ async def unblock_user(context):
|
|||||||
user = None
|
user = None
|
||||||
if context.reply_to_msg_id:
|
if context.reply_to_msg_id:
|
||||||
reply_message = await context.get_reply_message()
|
reply_message = await context.get_reply_message()
|
||||||
user = reply_message.from_id.user_id
|
if reply_message and reply_message.sender_id is not None:
|
||||||
else:
|
user = reply_message.sender_id
|
||||||
if len(context.parameter) == 1:
|
if not user and len(context.parameter) == 1:
|
||||||
[user] = context.parameter
|
(raw_user,) = context.parameter
|
||||||
if user.isnumeric():
|
if raw_user.isnumeric():
|
||||||
user = int(user)
|
user = int(raw_user)
|
||||||
elif context.message.entities is not None:
|
elif context.message.entities is not None:
|
||||||
if isinstance(context.message.entities[0], MessageEntityMentionName):
|
if isinstance(context.message.entities[0], MessageEntityMentionName):
|
||||||
user = context.message.entities[0].user_id
|
user = context.message.entities[0].user_id
|
||||||
else:
|
|
||||||
user_object = await context.client.get_me()
|
|
||||||
user = user_object.id
|
|
||||||
result = None
|
|
||||||
if not user:
|
if not user:
|
||||||
await context.edit(f"{lang('error_prefix')}{lang('arg_error')}")
|
await context.edit(f"{lang('error_prefix')}{lang('arg_error')}")
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
result = await context.client(UnblockRequest(id=user))
|
if await context.client(UnblockRequest(id=user)):
|
||||||
|
await context.edit(f"{lang('unblock_success')} `{user}`")
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
if result:
|
await context.edit(f"`{user}` {lang('unblock_exist')}")
|
||||||
await context.edit(f"{lang('unblock_success')} `{user}`")
|
|
||||||
else:
|
|
||||||
await context.edit(f"`{user}` {lang('unblock_exist')}")
|
|
||||||
|
Loading…
Reference in New Issue
Block a user