Allow owner to unauthorize a user or a chat

Signed-off-by: lzzy12 <jhashivam2020@gmail.com>
This commit is contained in:
lzzy12 2019-10-14 21:53:31 +05:30
parent d38bc90e82
commit 9f26ed107c
3 changed files with 37 additions and 4 deletions

View File

@ -49,13 +49,13 @@ status_reply_dict = {}
# Value: An object of DownloadStatus # Value: An object of DownloadStatus
download_dict = {} download_dict = {}
# Stores list of users and chats the bot is authorized to use in # Stores list of users and chats the bot is authorized to use in
AUTHORIZED_CHATS = [] AUTHORIZED_CHATS = set()
if os.path.exists('authorized_chats.txt'): if os.path.exists('authorized_chats.txt'):
with open('authorized_chats.txt', 'r+') as f: with open('authorized_chats.txt', 'r+') as f:
lines = f.readlines() lines = f.readlines()
for line in lines: for line in lines:
# LOGGER.info(line.split()) # LOGGER.info(line.split())
AUTHORIZED_CHATS.append(int(line.split()[0])) AUTHORIZED_CHATS.add(int(line.split()[0]))
try: try:
BOT_TOKEN = getConfig('BOT_TOKEN') BOT_TOKEN = getConfig('BOT_TOKEN')
parent_id = getConfig('GDRIVE_FOLDER_ID') parent_id = getConfig('GDRIVE_FOLDER_ID')

View File

@ -8,6 +8,7 @@ class _BotCommands:
self.ListCommand = 'list' self.ListCommand = 'list'
self.StatusCommand = 'status' self.StatusCommand = 'status'
self.AuthorizeCommand = 'authorize' self.AuthorizeCommand = 'authorize'
self.UnAuthorizeCommand = 'unauthorize'
self.PingCommand = 'ping' self.PingCommand = 'ping'
self.DiskCommand = 'disk' self.DiskCommand = 'disk'
self.HelpCommand = 'help' self.HelpCommand = 'help'

View File

@ -7,31 +7,63 @@ from telegram.ext import Filters
from telegram import Update from telegram import Update
from bot.helper.telegram_helper.bot_commands import BotCommands from bot.helper.telegram_helper.bot_commands import BotCommands
@run_async @run_async
def authorize(update: Update, context): def authorize(update: Update, context):
reply_message = update.message.reply_to_message reply_message = update.message.reply_to_message
msg = '' msg = ''
with open('authorized_chats.txt', 'a') as file: with open('authorized_chats.txt', 'a') as file:
if reply_message is None: if reply_message is None:
# Trying to authorize a chat
chat_id = update.effective_chat.id chat_id = update.effective_chat.id
if chat_id not in AUTHORIZED_CHATS: if chat_id not in AUTHORIZED_CHATS:
file.write(f'{chat_id}\n') file.write(f'{chat_id}\n')
AUTHORIZED_CHATS.append(chat_id) AUTHORIZED_CHATS.add(chat_id)
msg = 'Chat authorized' msg = 'Chat authorized'
else: else:
msg = 'Already authorized chat' msg = 'Already authorized chat'
else: else:
# Trying to authorize someone in specific
user_id = reply_message.from_user.id user_id = reply_message.from_user.id
if user_id not in AUTHORIZED_CHATS: if user_id not in AUTHORIZED_CHATS:
file.write(f'{user_id}\n') file.write(f'{user_id}\n')
AUTHORIZED_CHATS.append(user_id) AUTHORIZED_CHATS.add(user_id)
msg = 'Person Authorized to use the bot!' msg = 'Person Authorized to use the bot!'
else: else:
msg = 'Person already authorized' msg = 'Person already authorized'
sendMessage(msg, context, update) sendMessage(msg, context, update)
@run_async
def unauthorize(update, context):
reply_message = update.message.reply_to_message
if reply_message is None:
# Trying to unauthorize a chat
chat_id = update.effective_chat.id
if chat_id in AUTHORIZED_CHATS:
AUTHORIZED_CHATS.remove(chat_id)
msg = 'Chat unauthorized'
else:
msg = 'Already unauthorized chat'
else:
# Trying to authorize someone in specific
user_id = reply_message.from_user.id
if user_id in AUTHORIZED_CHATS:
AUTHORIZED_CHATS.remove(user_id)
msg = 'Person unauthorized to use the bot!'
else:
msg = 'Person already unauthorized!'
with open('authorized_chats.txt', 'a') as file:
file.truncate(0)
for i in AUTHORIZED_CHATS:
file.write(f'{i}\n')
sendMessage(msg, context, update)
authorize_handler = CommandHandler(command=BotCommands.AuthorizeCommand, callback=authorize, authorize_handler = CommandHandler(command=BotCommands.AuthorizeCommand, callback=authorize,
filters=CustomFilters.owner_filter & Filters.group) filters=CustomFilters.owner_filter & Filters.group)
unauthorize_handler = CommandHandler(command=BotCommands.UnAuthorizeCommand, callback=unauthorize,
filters=CustomFilters.owner_filter & Filters.group)
dispatcher.add_handler(authorize_handler) dispatcher.add_handler(authorize_handler)
dispatcher.add_handler(unauthorize_handler)