Implement log ring

This commit is contained in:
blank X 2021-03-10 15:27:11 +07:00
parent 12bc8afa43
commit aabc6d142f
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
6 changed files with 27 additions and 29 deletions

View File

@ -16,3 +16,4 @@ config:
log_user_adds: true
log_reports: true
log_forwards: true
log_ring_maxlen: 69420

View File

@ -8,6 +8,7 @@ import functools
import mimetypes
import yaml
import aiohttp
from collections import deque
from datetime import timedelta
from pyrogram import Client, StopPropagation, ContinuePropagation
from pyrogram.types import Chat, User
@ -19,6 +20,7 @@ with open('config.yaml') as config:
config = yaml.safe_load(config)
loop = asyncio.get_event_loop()
help_dict = dict()
log_ring = deque(maxlen=config['config'].get('log_ring_maxlen', 69420))
apps = []
app_user_ids = dict()

View File

@ -1,6 +1,7 @@
import asyncio
from pyrogram import idle
from . import loop, apps, slave, app_user_ids, session
from pyrogram.errors.exceptions.flood_420 import FloodWait
from . import loop, apps, slave, app_user_ids, session, log_ring, config
async def main():
async def _start_app(app):
@ -11,9 +12,24 @@ async def main():
try:
me = await app.get_me()
app_user_ids[me.id] = me
except:
except BaseException:
pass
await asyncio.sleep(60)
async def log_ring_worker():
while True:
await asyncio.sleep(1)
try:
text = log_ring.popleft()
except IndexError:
pass
while True:
try:
await slave.send_message(config['config']['log_chat'], text, disable_web_page_preview=True)
except FloodWait as ex:
await asyncio.sleep(ex.x + 1)
else:
break
asyncio.create_task(log_ring_worker())
await asyncio.gather(*(_start_app(app) for app in apps), slave.start())
await idle()
await asyncio.gather(*(app.stop() for app in apps), slave.stop())

View File

@ -2,8 +2,7 @@ import html
import asyncio
from collections import defaultdict
from pyrogram import Client, filters
from pyrogram.errors.exceptions.flood_420 import FloodWait
from .. import config, slave, log_errors, app_user_ids
from .. import config, log_errors, app_user_ids, log_ring
logged = defaultdict(set)
lock = asyncio.Lock()
@ -87,11 +86,5 @@ async def log_forwards(client, message):
if forwardee.is_scam:
user_text += ' <code>[SCAM]</code>'
text += f'{user_text} [<code>{forwardee.id}</code>]'
while True:
try:
await slave.send_message(config['config']['log_chat'], text, disable_web_page_preview=True)
except FloodWait as ex:
await asyncio.sleep(ex.x + 1)
else:
break
log_ring.append(text)
logged[message.chat.id].add(message.message_id)

View File

@ -2,8 +2,7 @@ import html
import asyncio
from collections import defaultdict
from pyrogram import Client, filters
from pyrogram.errors.exceptions.flood_420 import FloodWait
from .. import config, slave, log_errors
from .. import config, log_errors, log_ring
reported = defaultdict(set)
lock = asyncio.Lock()
@ -92,12 +91,6 @@ async def log_reports(client, message):
if mtext.strip():
text += ':'
text += f'</a></b> {html.escape(mtext.strip()[:1000])}'
while True:
try:
reply = await slave.send_message(config['config']['log_chat'], text, disable_web_page_preview=True)
except FloodWait as ex:
await asyncio.sleep(ex.x + 1)
else:
break
log_ring.append(text)
reported[message.chat.id].add(message.message_id)
reported[reply.chat.id].add(reply.message_id)

View File

@ -2,9 +2,8 @@ import html
import asyncio
from collections import defaultdict
from pyrogram import Client, ContinuePropagation
from pyrogram.errors.exceptions.flood_420 import FloodWait
from pyrogram.raw.types import UpdateNewChannelMessage, UpdateNewMessage, MessageService, PeerChat, PeerChannel, MessageActionChatAddUser, MessageActionChatJoinedByLink, PeerUser
from .. import config, log_errors, slave
from .. import config, log_errors, log_ring
def sexy_user_name(user):
text = user.first_name
@ -59,13 +58,7 @@ async def log_user_joins(client, update, users, chats):
text += f'- <b>Adder:</b> {adder}\n- <b>Added Users:</b>\n'
for user in action.users:
text += f'--- {sexy_user_name(users[user])}\n'
while True:
try:
await slave.send_message(config['config']['log_chat'], text, disable_web_page_preview=True)
except FloodWait as ex:
await asyncio.sleep(ex.x + 1)
else:
break
log_ring.append(text)
handled[sexy_chat_id].add(message.id)
return
raise ContinuePropagation