Skip to content

Commit

Permalink
to delete has been optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeN220 committed Mar 25, 2022
1 parent 522e033 commit 346037a
Showing 1 changed file with 41 additions and 14 deletions.
55 changes: 41 additions & 14 deletions commands/to_delete.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import asyncio
from time import time
from typing import List
import re

from vkwave.api import APIOptionsRequestContext
from vkwave.api.methods._error import APIError
from vkwave.bots import (
SimpleUserEvent,
FromMeFilter,
Expand All @@ -18,6 +22,39 @@
)


async def get_messages(
count: int,
api_ctx: APIOptionsRequestContext,
peer_id: int
) -> List[int]:
message_ids = []
history = (await api_ctx.messages.get_history(peer_id=peer_id, count=50 + count)).response.items

for message in history:
if len(message_ids) > count:
break
if message.from_id == config['VK']['user_id']:
message_ids.append(message.id)

return message_ids


async def edit_messages(message_ids: List[int], event: SimpleUserEvent) -> None:
not_edtied_messages = message_ids.copy()
for message in message_ids:
try:
start_time = time()
await event.api_ctx.messages.edit(
peer_id=event.peer_id, message='
', message_id=message
)
not_edtied_messages.pop()

except APIError as error:
if error.code == 6:
await asyncio.sleep(0.3)
await edit_messages(not_edtied_messages, event)


@to_delete_router.message_handler(
TextStartswithFilter(config['to_delete_trigger']),
FromMeFilter(True)
Expand All @@ -31,22 +68,12 @@ async def to_delete(event: SimpleUserEvent):

is_editing = (len(text) > 3) and (text[2] == config['to_delete_argument'])

messages = (await event.api_ctx.messages.get_history(
peer_id=peer_id,
count=50 + to_delete_count
)).response.items
for message in messages:
if message.from_id == config['VK']['user_id'] and len(to_delete_list) <= to_delete_count:
to_delete_list.append(message.id)
if is_editing:
await event.api_ctx.messages.edit(
peer_id=peer_id,
message='&#13;',
message_id=message.id
)
messages = await get_messages(to_delete_count, event.api_ctx, peer_id)
if is_editing:
await edit_messages(messages, event)

await event.api_ctx.messages.delete(
message_ids=to_delete_list,
message_ids=messages,
delete_for_all=1
)

Expand Down

0 comments on commit 346037a

Please sign in to comment.