Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Delete File Channel Feature #283

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions info.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def is_enabled(value, default):
# Admins, Channels & Users
ADMINS = [int(admin) if id_pattern.search(admin) else admin for admin in environ.get('ADMINS', '').split()]
CHANNELS = [int(ch) if id_pattern.search(ch) else ch for ch in environ.get('CHANNELS', '0').split()]
DELETE_CHANNELS = [int(dch) if id_pattern.search(dch) else dch for dch in environ.get('DELETE_CHANNELS', '0').split()]
auth_users = [int(user) if id_pattern.search(user) else user for user in environ.get('AUTH_USERS', '').split()]
AUTH_USERS = (auth_users + ADMINS) if auth_users else []
auth_channel = environ.get('AUTH_CHANNEL')
Expand Down
57 changes: 57 additions & 0 deletions plugins/deletechannel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import logging
from pyrogram import Client, filters
from info import DELETE_CHANNELS
from database.ia_filterdb import Media, unpack_new_file_id

logger = logging.getLogger(__name__)

media_filter = filters.document | filters.video | filters.audio


@Client.on_message(filters.chat(DELETE_CHANNELS) & media_filter)
async def deltemedia(bot, message):
"""Delete file from database"""
reply = message
if reply and reply.media:
#msg = await message.reply("Processing...⏳", quote=True)
pass
else:
#await message.reply('Reply to file with /delete which you want to delete', quote=True)
return

for file_type in ("document", "video", "audio"):
media = getattr(reply, file_type, None)
if media is not None:
break
else:
#await msg.edit('This is not supported file format')
return

file_id, file_ref = unpack_new_file_id(media.file_id)

result = await Media.collection.delete_one({
'_id': file_id,
})
if result.deleted_count:
logger.info('File is successfully deleted from database')
else:
file_name = re.sub(r"(_|\-|\.|\+)", " ", str(media.file_name))
result = await Media.collection.delete_many({
'file_name': file_name,
'file_size': media.file_size,
'mime_type': media.mime_type
})
if result.deleted_count:
logger.info('File is successfully deleted from database')
else:
# files indexed before https://github.com/EvamariaTG/EvaMaria/commit/f3d2a1bcb155faf44178e5d7a685a1b533e714bf#diff-86b613edf1748372103e94cacff3b578b36b698ef9c16817bb98fe9ef22fb669R39
# have original file name.
result = await Media.collection.delete_many({
'file_name': media.file_name,
'file_size': media.file_size,
'mime_type': media.mime_type
})
if result.deleted_count:
logger.info('File is successfully deleted from database')
else:
logger.info('File not found in database')
1 change: 1 addition & 0 deletions sample_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
CHANNELS = [-10012345678, -100987654321, 'channelusername']
AUTH_USERS = []
AUTH_CHANNEL = None
DELETE_CHANNELS = [-100123456789]

# MongoDB information
DATABASE_URI = "mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb]?retryWrites=true&w=majority"
Expand Down