-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
224 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import psycopg2 | ||
from psycopg2 import Error | ||
from bot import AUTHORIZED_CHATS, SUDO_USERS, DB_URI, LOGGER | ||
|
||
class DbManger: | ||
def __init__(self): | ||
self.err = False | ||
|
||
def connect(self): | ||
try: | ||
self.conn = psycopg2.connect(DB_URI) | ||
self.cur = self.conn.cursor() | ||
except psycopg2.DatabaseError as error : | ||
LOGGER.error("Error in dbMang : ", error) | ||
self.err = True | ||
|
||
def disconnect(self): | ||
self.cur.close() | ||
self.conn.close() | ||
|
||
def db_auth(self,chat_id: int): | ||
self.connect() | ||
if self.err : | ||
return "There's some error check log for details" | ||
else: | ||
sql = 'INSERT INTO users VALUES ({});'.format(chat_id) | ||
self.cur.execute(sql) | ||
self.conn.commit() | ||
self.disconnect() | ||
AUTHORIZED_CHATS.add(chat_id) | ||
return 'Authorized successfully' | ||
|
||
def db_unauth(self,chat_id: int): | ||
self.connect() | ||
if self.err : | ||
return "There's some error check log for details" | ||
else: | ||
sql = 'DELETE from users where uid = {};'.format(chat_id) | ||
self.cur.execute(sql) | ||
self.conn.commit() | ||
self.disconnect() | ||
AUTHORIZED_CHATS.remove(chat_id) | ||
if chat_id in SUDO_USERS: | ||
SUDO_USERS.remove(chat_id) | ||
return 'Unauthorized successfully' | ||
|
||
def db_addsudo(self,chat_id: int): | ||
self.connect() | ||
if self.err : | ||
return "There's some error check log for details" | ||
else: | ||
if chat_id in AUTHORIZED_CHATS: | ||
sql = 'UPDATE users SET sudo = TRUE where uid = {};'.format(chat_id) | ||
self.cur.execute(sql) | ||
self.conn.commit() | ||
self.disconnect() | ||
SUDO_USERS.add(chat_id) | ||
return 'Successfully promoted as sudo' | ||
else: | ||
sql = 'INSERT INTO users VALUES ({},TRUE);'.format(chat_id) | ||
self.cur.execute(sql) | ||
self.conn.commit() | ||
self.disconnect() | ||
AUTHORIZED_CHATS.add(chat_id) | ||
SUDO_USERS.add(chat_id) | ||
return 'Successfully Authorized and promoted as sudo' | ||
|
||
def db_rmsudo(self,chat_id: int): | ||
self.connect() | ||
if self.err : | ||
return "There's some error check log for details" | ||
else: | ||
sql = 'UPDATE users SET sudo = FALSE where uid = {};'.format(chat_id) | ||
self.cur.execute(sql) | ||
self.conn.commit() | ||
self.disconnect() | ||
SUDO_USERS.remove(chat_id) | ||
return 'Successfully removed from Sudo' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,141 @@ | ||
from bot.helper.telegram_helper.message_utils import sendMessage | ||
from telegram.ext import run_async | ||
from bot import AUTHORIZED_CHATS, dispatcher | ||
from bot import AUTHORIZED_CHATS, SUDO_USERS, dispatcher | ||
from telegram.ext import CommandHandler | ||
from bot.helper.telegram_helper.filters import CustomFilters | ||
from telegram.ext import Filters | ||
from telegram import Update | ||
from bot.helper.telegram_helper.bot_commands import BotCommands | ||
|
||
from bot.helper.ext_utils.db_handler import DbManger | ||
|
||
@run_async | ||
def authorize(update,context): | ||
reply_message = None | ||
message_ = None | ||
reply_message = update.message.reply_to_message | ||
msg = '' | ||
with open('authorized_chats.txt', 'a') as file: | ||
message_ = update.message.text.split(' ') | ||
if len(message_) == 2: | ||
chat_id = int(message_[1]) | ||
if chat_id not in AUTHORIZED_CHATS: | ||
msg = DbManger().db_auth(chat_id) | ||
else: | ||
msg = 'User already authorized' | ||
else: | ||
if reply_message is None: | ||
# Trying to authorize a chat | ||
chat_id = update.effective_chat.id | ||
if chat_id not in AUTHORIZED_CHATS: | ||
file.write(f'{chat_id}\n') | ||
AUTHORIZED_CHATS.add(chat_id) | ||
msg = 'Chat authorized' | ||
msg = DbManger().db_auth(chat_id) | ||
else: | ||
msg = 'Already authorized chat' | ||
|
||
else: | ||
# Trying to authorize someone in specific | ||
user_id = reply_message.from_user.id | ||
if user_id not in AUTHORIZED_CHATS: | ||
file.write(f'{user_id}\n') | ||
AUTHORIZED_CHATS.add(user_id) | ||
msg = 'Person Authorized to use the bot!' | ||
msg = DbManger().db_auth(user_id) | ||
else: | ||
msg = 'Person already authorized' | ||
sendMessage(msg, context.bot, update) | ||
msg = 'User already authorized' | ||
sendMessage(msg, context.bot, update) | ||
|
||
|
||
@run_async | ||
def unauthorize(update,context): | ||
reply_message = None | ||
message_ = None | ||
reply_message = update.message.reply_to_message | ||
if reply_message is None: | ||
# Trying to unauthorize a chat | ||
chat_id = update.effective_chat.id | ||
message_ = update.message.text.split(' ') | ||
if len(message_) == 2: | ||
chat_id = int(message_[1]) | ||
if chat_id in AUTHORIZED_CHATS: | ||
AUTHORIZED_CHATS.remove(chat_id) | ||
msg = 'Chat unauthorized' | ||
msg = DbManger().db_unauth(chat_id) | ||
else: | ||
msg = 'User already unauthorized' | ||
else: | ||
if reply_message is None: | ||
# Trying to unauthorize a chat | ||
chat_id = update.effective_chat.id | ||
if chat_id in AUTHORIZED_CHATS: | ||
msg = DbManger().db_unauth(chat_id) | ||
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: | ||
msg = DbManger().db_unauth(user_id) | ||
else: | ||
msg = 'User already unauthorized' | ||
sendMessage(msg, context.bot, update) | ||
|
||
|
||
@run_async | ||
def addSudo(update,context): | ||
reply_message = None | ||
message_ = None | ||
reply_message = update.message.reply_to_message | ||
message_ = update.message.text.split(' ') | ||
if len(message_) == 2: | ||
chat_id = int(message_[1]) | ||
if chat_id not in SUDO_USERS: | ||
msg = DbManger().db_addsudo(chat_id) | ||
else: | ||
msg = 'Already unauthorized chat' | ||
msg = 'Already Sudo' | ||
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!' | ||
if reply_message is None: | ||
msg = "Give ID or Reply To message of whom you want to Promote" | ||
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') | ||
# Trying to authorize someone in specific | ||
user_id = reply_message.from_user.id | ||
if user_id not in SUDO_USERS: | ||
msg = DbManger().db_addsudo(user_id) | ||
else: | ||
msg = 'Already Sudo' | ||
sendMessage(msg, context.bot, update) | ||
|
||
|
||
@run_async | ||
def removeSudo(update,context): | ||
reply_message = None | ||
message_ = None | ||
reply_message = update.message.reply_to_message | ||
message_ = update.message.text.split(' ') | ||
if len(message_) == 2: | ||
chat_id = int(message_[1]) | ||
if chat_id in SUDO_USERS: | ||
msg = DbManger().db_rmsudo(chat_id) | ||
else: | ||
msg = 'Not a Sudo' | ||
else: | ||
if reply_message is None: | ||
msg = "Give ID or Reply To message of whom you want to remove from Sudo" | ||
else: | ||
user_id = reply_message.from_user.id | ||
if user_id in SUDO_USERS: | ||
msg = DbManger().db_rmsudo(user_id) | ||
else: | ||
msg = 'Not a Sudo' | ||
sendMessage(msg, context.bot, update) | ||
|
||
|
||
@run_async | ||
def sendAuthChats(update,context): | ||
sendMessage(f'Authorized Chats are : {AUTHORIZED_CHATS.__str__()}\nSudo Users are : {SUDO_USERS}', context.bot, update) | ||
|
||
|
||
send_auth_handler = CommandHandler(command=BotCommands.AuthorizedUsersCommand, callback=sendAuthChats, | ||
filters=CustomFilters.owner_filter | CustomFilters.sudo_user) | ||
authorize_handler = CommandHandler(command=BotCommands.AuthorizeCommand, callback=authorize, | ||
filters=CustomFilters.owner_filter & Filters.group) | ||
filters=CustomFilters.owner_filter | CustomFilters.sudo_user) | ||
unauthorize_handler = CommandHandler(command=BotCommands.UnAuthorizeCommand, callback=unauthorize, | ||
filters=CustomFilters.owner_filter & Filters.group) | ||
filters=CustomFilters.owner_filter | CustomFilters.sudo_user) | ||
addsudo_handler = CommandHandler(command=BotCommands.AddSudoCommand, callback=addSudo, | ||
filters=CustomFilters.owner_filter) | ||
removesudo_handler = CommandHandler(command=BotCommands.RmSudoCommand, callback=removeSudo, | ||
filters=CustomFilters.owner_filter) | ||
|
||
dispatcher.add_handler(send_auth_handler) | ||
dispatcher.add_handler(authorize_handler) | ||
dispatcher.add_handler(unauthorize_handler) | ||
dispatcher.add_handler(addsudo_handler) | ||
dispatcher.add_handler(removesudo_handler) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters