Files
userbot/modules/misc/autofwd.py
T
forust 7fb0a0e179 chore: batch lint fixes across userbot and edu_master
- S113: Add timeout=10 to all requests calls (74 fixes)
- E722: Replace bare except: with except Exception:
- B904: Replace redundant re-raise with bare raise
- E402: Add noqa for intentional late imports after import_library()
- S102/S307/S310/S311/S603/S605/S606/S607/S108: Add noqa for intentional usage
- F601: Fix duplicate dict key in unsplash.py
- N802: Rename ReplyCheck -> reply_check with backward compat alias
- N813: Rename bs -> BS in icons.py
- B007/B020: Rename loop var _j in animations.py
- SIM102: Collapse nested if in autofwd.py
- SIM113: Use enumerate() in calculator.py
- A002: Add noqa for builtin shadowing in admlist.py
- F811: Add noqa for cohere redefinition
- edu_master: Fix ARG001, S108, S110, SIM117, apply --unsafe-fixes
- Add modules_list.txt with full module inventory
2026-06-19 15:36:25 +02:00

166 lines
6.8 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#  Moon-Userbot - telegram userbot
#  Copyright (C) 2020-present Moon Userbot Organization
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
from pyrogram import Client, filters
from pyrogram.errors import MessageTooLong
from pyrogram.types import Message
from utils.db import db
from utils.misc import modules_help, prefix
def addtrg(channel_id):
channel_ids = db.get('custom.autofwd', 'chatto', default=[])
if channel_id not in channel_ids:
channel_ids.append(channel_id)
db.set('custom.autofwd', 'chatto', channel_ids)
def rmtrg(channel_id):
channel_ids = db.get('custom.autofwd', 'chatto', default=[])
if channel_id in channel_ids:
channel_ids.remove(channel_id)
db.set('custom.autofwd', 'chatto', channel_ids)
def addsrc(channel_id):
channel_ids = db.get('custom.autofwd', 'chatsrc', default=[])
if channel_id not in channel_ids:
channel_ids.append(channel_id)
db.set('custom.autofwd', 'chatsrc', channel_ids)
def rmsrc(channel_id):
channel_ids = db.get('custom.autofwd', 'chatsrc', default=[])
if channel_id in channel_ids:
channel_ids.remove(channel_id)
db.set('custom.autofwd', 'chatsrc', channel_ids)
def getfwd_data():
source_chats = db.get('custom.autofwd', 'chatsrc')
target_chats = db.get('custom.autofwd', 'chatto')
return source_chats, target_chats
@Client.on_message(filters.command(['addfwd_src', 'addfwd_to'], prefix) & filters.me)
async def addfwd(_, message: Message):
if message.command[0] == 'addfwd_src':
if len(message.command) > 1:
channel_id = message.text.split(maxsplit=1)[1]
if not channel_id.startswith('-100'):
channel_id = '-100' + channel_id
# chat id should be integer
if not channel_id.isdigit():
try:
channel_id = int(channel_id)
except Exception:
return await message.edit_text('Chat id should be in integer')
addsrc(channel_id=channel_id)
await message.edit_text(f'Auto Forwarding Enabled for Chat with id: <code>{channel_id}</code>')
else:
await message.edit_text('Chat id not provided!')
return
elif message.command[0] == 'addfwd_to':
if len(message.command) > 1:
channel_id = message.text.split(maxsplit=1)[1]
if not channel_id.startswith('-100'):
channel_id = '-100' + channel_id
# chat id should be integer
if not channel_id.isdigit():
try:
channel_id = int(channel_id)
except Exception:
return await message.edit_text('Chat id should be in integer')
addtrg(channel_id=channel_id)
await message.edit_text(f'Auto Forwarding Enabled to Chat with id: <code>{channel_id}</code>')
else:
await message.edit_text('Chat id not provided!')
return
@Client.on_message(filters.command(['delfwd_src', 'delfwd_to'], prefix) & filters.me)
async def delfwd(_, message: Message):
if message.command[0] == 'delfwd_src':
if len(message.command) > 1:
channel_id = message.text.split(maxsplit=1)[1]
if not channel_id.startswith('-100'):
channel_id = '-100' + channel_id
# chat id should be integer
if not channel_id.isdigit():
try:
channel_id = int(channel_id)
except Exception:
return await message.edit_text('Chat id should be in integer')
rmsrc(channel_id=channel_id)
await message.edit_text(f'Auto Forwarding Disabled for Chat with id: <code>{channel_id}</code>')
else:
await message.edit_text('Chat id not provided!')
return
elif message.command[0] == 'delfwd_to':
if len(message.command) > 1:
channel_id = message.text.split(maxsplit=1)[1]
if not channel_id.startswith('-100'):
channel_id = '-100' + channel_id
# chat id should be integer
if not channel_id.isdigit():
try:
channel_id = int(channel_id)
except Exception:
return await message.edit_text('Chat id should be in integer')
rmtrg(channel_id=channel_id)
await message.edit_text(f'Auto Forwarding Disabled to Chat with id: <code>{channel_id}</code>')
else:
await message.edit_text('Chat id not provided!')
return
@Client.on_message(filters.command('autofwd', prefix) & filters.me)
async def autofwd(_, message: Message):
source_chats, target_chats = getfwd_data()
return await message.edit_text(f'Source Chats: {source_chats}\nTarget Chats: {target_chats}')
@Client.on_message(filters.channel | filters.group)
async def autofwd_main(client: Client, message: Message):
chat_id = message.chat.id
source_chats = db.get('custom.autofwd', 'chatsrc')
target_chats = db.get('custom.autofwd', 'chatto')
if source_chats is not None and chat_id in source_chats and target_chats is not None:
for chat in target_chats:
try:
await message.copy(chat)
except Exception as e:
try:
await client.send_message(
'me',
f'Auto Forwarding Failed for Chat with id: <code>{chat_id}</code> to <code>{chat}</code>\n\n{e}',
)
except MessageTooLong:
await client.send_message(
'me',
f'Auto Forwarding Failed for Chat with id: <code>{chat_id}</code> to <code>{chat}</code>, Please check logs!',
)
modules_help['autofwd'] = {
'autofwd': 'Retrieve Data of auto fwd',
'addfwd_src [channel_id]*': 'Enable auto forwarding for a channel',
'addfwd_to [channel_id]*': 'Enable auto forwarding to a channel',
'delfwd_src [channel_id]*': 'Disable auto forwarding for a channel',
'delfwd_to [channel_id]*': 'Disable auto forwarding to a channel',
}