Implement log ring
This commit is contained in:
		
							parent
							
								
									12bc8afa43
								
							
						
					
					
						commit
						aabc6d142f
					
				| 
						 | 
					@ -16,3 +16,4 @@ config:
 | 
				
			||||||
    log_user_adds: true
 | 
					    log_user_adds: true
 | 
				
			||||||
    log_reports: true
 | 
					    log_reports: true
 | 
				
			||||||
    log_forwards: true
 | 
					    log_forwards: true
 | 
				
			||||||
 | 
					    log_ring_maxlen: 69420
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,6 +8,7 @@ import functools
 | 
				
			||||||
import mimetypes
 | 
					import mimetypes
 | 
				
			||||||
import yaml
 | 
					import yaml
 | 
				
			||||||
import aiohttp
 | 
					import aiohttp
 | 
				
			||||||
 | 
					from collections import deque
 | 
				
			||||||
from datetime import timedelta
 | 
					from datetime import timedelta
 | 
				
			||||||
from pyrogram import Client, StopPropagation, ContinuePropagation
 | 
					from pyrogram import Client, StopPropagation, ContinuePropagation
 | 
				
			||||||
from pyrogram.types import Chat, User
 | 
					from pyrogram.types import Chat, User
 | 
				
			||||||
| 
						 | 
					@ -19,6 +20,7 @@ with open('config.yaml') as config:
 | 
				
			||||||
    config = yaml.safe_load(config)
 | 
					    config = yaml.safe_load(config)
 | 
				
			||||||
loop = asyncio.get_event_loop()
 | 
					loop = asyncio.get_event_loop()
 | 
				
			||||||
help_dict = dict()
 | 
					help_dict = dict()
 | 
				
			||||||
 | 
					log_ring = deque(maxlen=config['config'].get('log_ring_maxlen', 69420))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
apps = []
 | 
					apps = []
 | 
				
			||||||
app_user_ids = dict()
 | 
					app_user_ids = dict()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
import asyncio
 | 
					import asyncio
 | 
				
			||||||
from pyrogram import idle
 | 
					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 main():
 | 
				
			||||||
    async def _start_app(app):
 | 
					    async def _start_app(app):
 | 
				
			||||||
| 
						 | 
					@ -11,9 +12,24 @@ async def main():
 | 
				
			||||||
            try:
 | 
					            try:
 | 
				
			||||||
                me = await app.get_me()
 | 
					                me = await app.get_me()
 | 
				
			||||||
                app_user_ids[me.id] = me
 | 
					                app_user_ids[me.id] = me
 | 
				
			||||||
            except:
 | 
					            except BaseException:
 | 
				
			||||||
                pass
 | 
					                pass
 | 
				
			||||||
            await asyncio.sleep(60)
 | 
					            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 asyncio.gather(*(_start_app(app) for app in apps), slave.start())
 | 
				
			||||||
    await idle()
 | 
					    await idle()
 | 
				
			||||||
    await asyncio.gather(*(app.stop() for app in apps), slave.stop())
 | 
					    await asyncio.gather(*(app.stop() for app in apps), slave.stop())
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,8 +2,7 @@ import html
 | 
				
			||||||
import asyncio
 | 
					import asyncio
 | 
				
			||||||
from collections import defaultdict
 | 
					from collections import defaultdict
 | 
				
			||||||
from pyrogram import Client, filters
 | 
					from pyrogram import Client, filters
 | 
				
			||||||
from pyrogram.errors.exceptions.flood_420 import FloodWait
 | 
					from .. import config, log_errors, app_user_ids, log_ring
 | 
				
			||||||
from .. import config, slave, log_errors, app_user_ids
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
logged = defaultdict(set)
 | 
					logged = defaultdict(set)
 | 
				
			||||||
lock = asyncio.Lock()
 | 
					lock = asyncio.Lock()
 | 
				
			||||||
| 
						 | 
					@ -87,11 +86,5 @@ async def log_forwards(client, message):
 | 
				
			||||||
        if forwardee.is_scam:
 | 
					        if forwardee.is_scam:
 | 
				
			||||||
            user_text += ' <code>[SCAM]</code>'
 | 
					            user_text += ' <code>[SCAM]</code>'
 | 
				
			||||||
        text += f'{user_text} [<code>{forwardee.id}</code>]'
 | 
					        text += f'{user_text} [<code>{forwardee.id}</code>]'
 | 
				
			||||||
        while True:
 | 
					        log_ring.append(text)
 | 
				
			||||||
            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
 | 
					 | 
				
			||||||
        logged[message.chat.id].add(message.message_id)
 | 
					        logged[message.chat.id].add(message.message_id)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,8 +2,7 @@ import html
 | 
				
			||||||
import asyncio
 | 
					import asyncio
 | 
				
			||||||
from collections import defaultdict
 | 
					from collections import defaultdict
 | 
				
			||||||
from pyrogram import Client, filters
 | 
					from pyrogram import Client, filters
 | 
				
			||||||
from pyrogram.errors.exceptions.flood_420 import FloodWait
 | 
					from .. import config, log_errors, log_ring
 | 
				
			||||||
from .. import config, slave, log_errors
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
reported = defaultdict(set)
 | 
					reported = defaultdict(set)
 | 
				
			||||||
lock = asyncio.Lock()
 | 
					lock = asyncio.Lock()
 | 
				
			||||||
| 
						 | 
					@ -92,12 +91,6 @@ async def log_reports(client, message):
 | 
				
			||||||
            if mtext.strip():
 | 
					            if mtext.strip():
 | 
				
			||||||
                text += ':'
 | 
					                text += ':'
 | 
				
			||||||
            text += f'</a></b> {html.escape(mtext.strip()[:1000])}'
 | 
					            text += f'</a></b> {html.escape(mtext.strip()[:1000])}'
 | 
				
			||||||
        while True:
 | 
					        log_ring.append(text)
 | 
				
			||||||
            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
 | 
					 | 
				
			||||||
        reported[message.chat.id].add(message.message_id)
 | 
					        reported[message.chat.id].add(message.message_id)
 | 
				
			||||||
        reported[reply.chat.id].add(reply.message_id)
 | 
					        reported[reply.chat.id].add(reply.message_id)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,9 +2,8 @@ import html
 | 
				
			||||||
import asyncio
 | 
					import asyncio
 | 
				
			||||||
from collections import defaultdict
 | 
					from collections import defaultdict
 | 
				
			||||||
from pyrogram import Client, ContinuePropagation
 | 
					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 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):
 | 
					def sexy_user_name(user):
 | 
				
			||||||
    text = user.first_name
 | 
					    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'
 | 
					                            text += f'- <b>Adder:</b> {adder}\n- <b>Added Users:</b>\n'
 | 
				
			||||||
                            for user in action.users:
 | 
					                            for user in action.users:
 | 
				
			||||||
                                text += f'--- {sexy_user_name(users[user])}\n'
 | 
					                                text += f'--- {sexy_user_name(users[user])}\n'
 | 
				
			||||||
                        while True:
 | 
					                        log_ring.append(text)
 | 
				
			||||||
                            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
 | 
					 | 
				
			||||||
                        handled[sexy_chat_id].add(message.id)
 | 
					                        handled[sexy_chat_id].add(message.id)
 | 
				
			||||||
                        return
 | 
					                        return
 | 
				
			||||||
    raise ContinuePropagation
 | 
					    raise ContinuePropagation
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue