4
1
Fork 1
This repository has been archived on 2024-08-07. You can view files and clone it, but cannot push or open issues or pull requests.
qBitDownload-Bot/bot/bot.py

184 lines
5.4 KiB
Python
Raw Permalink Normal View History

2023-10-30 22:02:26 +00:00
#!/usr/bin/python3
# -- coding: utf-8 --
import func, telebot, os, log, sys
2023-10-30 23:37:37 +00:00
from db import PATH
2023-10-31 22:49:17 +00:00
from lang import LANG as msg
2023-10-30 22:02:26 +00:00
TOKEN = os.environ.get('TOKEN','None')
2023-10-30 22:02:26 +00:00
bot = telebot.TeleBot(TOKEN)
2023-10-30 23:37:37 +00:00
folder_list = []
2023-10-30 23:59:17 +00:00
dir = None
2023-10-30 22:02:26 +00:00
# Start
@bot.message_handler(commands=['start'])
def welcome(message):
id = message.from_user.id
if func.auth_check(id):
2023-10-31 22:49:17 +00:00
bot.reply_to(message,str(msg.get('type')),reply_markup=home())
2023-10-30 22:02:26 +00:00
else:
2023-10-31 22:49:17 +00:00
bot.reply_to(message,str(msg.get('adeny')))
2023-10-30 22:02:26 +00:00
# Keyboard: Homepage
def home():
keyboard = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
2023-10-31 22:49:17 +00:00
file = telebot.types.KeyboardButton(str(msg.get('file')))
magnet = telebot.types.KeyboardButton(str(msg.get('magnet')))
2023-10-30 23:03:14 +00:00
keyboard.add(file,magnet)
2023-10-30 22:02:26 +00:00
return keyboard
# Login
@bot.message_handler(commands=['login'])
def login(message):
id = message.from_user.id
name = message.from_user.first_name
2023-10-30 22:02:26 +00:00
passwd = message.text.replace('/login ', '')
f = str(func.u_auth(name,id,passwd))
2023-10-31 22:49:17 +00:00
if f == str(msg.get('sucauth')) or f == str(msg.get('alauth')):
2023-10-30 22:02:26 +00:00
bot.reply_to(message,f,reply_markup=home())
else:
bot.reply_to(message,f)
# Folders
def folder_menu():
folders = func.dirlist()
if len(folders) == 0:
return None
else:
keyboard = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
i = 0
2023-10-30 23:03:14 +00:00
global folder_list
folder_list = []
2023-10-30 22:02:26 +00:00
lst = []
for folder in folders.keys():
globals()[f'var_{i}'] = telebot.types.KeyboardButton(f'{folder}')
lst.append(f'var_{i}')
2023-10-30 23:03:14 +00:00
folder_list.append(folder)
2023-10-30 22:02:26 +00:00
i += 1
for var in lst:
keyboard.add(globals()[var])
return keyboard
# Folder add
@bot.message_handler(commands=['add'])
def add(message):
id = message.from_user.id
name = message.from_user.first_name
2023-10-30 22:02:26 +00:00
txt = message.text.split(' ', 2)
if len(txt) == 3:
key = txt[1]
path = txt[2]
f = str(func.add_dir(name,id,key,path))
2023-10-30 22:02:26 +00:00
else:
2023-10-31 22:49:17 +00:00
f = str(msg.get('aerr'))
2023-10-30 22:02:26 +00:00
bot.reply_to(message,f,reply_markup=home())
# Folder del
@bot.message_handler(commands=['del'])
def rm(message):
id = message.from_user.id
name = message.from_user.first_name
2023-10-30 22:02:26 +00:00
folder = message.text.replace('/del ', '')
f = func.del_dir(name,id,folder)
2023-10-30 22:02:26 +00:00
bot.reply_to(message,str(f),reply_markup=home())
# Magnet
2023-10-31 22:49:17 +00:00
@bot.message_handler(func=lambda message: message.text == str(msg.get('magnet')))
2023-10-30 22:02:26 +00:00
def magnet(message):
id = message.from_user.id
if func.auth_check(id):
2023-10-30 23:03:14 +00:00
global type
type = 'magnet'
2023-10-30 22:02:26 +00:00
f = folder_menu()
if f == None:
2023-10-31 22:49:17 +00:00
bot.reply_to(message,str(msg.get('cff')))
2023-10-30 22:02:26 +00:00
else:
2023-10-31 22:49:17 +00:00
bot.reply_to(message,str(msg.get('chf')),reply_markup=f)
2023-10-30 22:02:26 +00:00
else:
2023-10-31 22:49:17 +00:00
bot.reply_to(message,str(msg.get('adeny')))
2023-10-30 22:02:26 +00:00
2023-10-30 23:37:37 +00:00
# File
2023-10-31 22:49:17 +00:00
@bot.message_handler(func=lambda message: message.text == str(msg.get('file')))
2023-10-30 23:37:37 +00:00
def file(message):
id = message.from_user.id
if func.auth_check(id):
global type
type = 'file'
f = folder_menu()
if f == None:
2023-10-31 22:49:17 +00:00
bot.reply_to(message,str(msg.get('cff')))
2023-10-30 23:37:37 +00:00
else:
2023-10-31 22:49:17 +00:00
bot.reply_to(message,str(msg.get('chf')),reply_markup=f)
2023-10-30 23:37:37 +00:00
else:
2023-10-31 22:49:17 +00:00
bot.reply_to(message,str(msg.get('adeny')))
2023-10-30 23:37:37 +00:00
# File download
@bot.message_handler(content_types=['document'])
def download(message):
id = message.from_user.id
name = message.from_user.first_name
2023-10-30 23:37:37 +00:00
if func.auth_check(id):
global type, dir, folder_list
if dir != None and type == 'file':
if message.document.file_name.lower().endswith('.torrent'):
file_info = bot.get_file(message.document.file_id)
file_path = file_info.file_path
file = bot.download_file(file_path)
file_name = os.path.join(PATH, message.document.file_name)
with open(file_name, 'wb') as dl:
dl.write(file)
f = str(func.file(name,id,file_name,dir))
2023-10-30 23:37:37 +00:00
dir, type, folder_list = None,None,[]
bot.reply_to(message,f)
else:
2023-10-31 22:49:17 +00:00
bot.reply_to(message,str(msg.get('ntorr')))
bot.reply_to(message,str(msg.get('type')),reply_markup=home())
2023-10-30 23:37:37 +00:00
else:
2023-10-31 22:49:17 +00:00
bot.reply_to(message,str(msg.get('adeny')))
2023-10-30 23:37:37 +00:00
2023-10-30 23:03:14 +00:00
# Dir choose
def dirchoose(message):
global dir
dir = message.text
if type == 'magnet':
2023-10-31 22:49:17 +00:00
bot.reply_to(message,str(msg.get('sendm')))
2023-10-30 23:37:37 +00:00
if type == 'file':
2023-10-31 22:49:17 +00:00
bot.reply_to(message,str(msg.get('sendf')))
2023-10-30 23:03:14 +00:00
2023-10-30 22:02:26 +00:00
# Unknown message
@bot.message_handler(func=lambda message: True)
def unknown(message):
2023-10-30 23:03:14 +00:00
global type, dir, folder_list
2023-10-30 22:02:26 +00:00
id = message.from_user.id
name = message.from_user.first_name
2023-10-30 22:02:26 +00:00
if func.auth_check(id):
2023-10-30 23:03:14 +00:00
txt = message.text
if txt in folder_list:
dirchoose(message)
return None
if dir != None and type == 'magnet':
f = str(func.magnet(name,id,txt,dir))
2023-10-30 23:03:14 +00:00
dir, type, folder_list = None,None,[]
bot.reply_to(message,f)
2023-10-31 22:49:17 +00:00
bot.reply_to(message,str(msg.get('type')),reply_markup=home())
2023-10-30 22:02:26 +00:00
else:
2023-10-31 22:49:17 +00:00
bot.reply_to(message,str(msg.get('adeny')))
2023-10-30 22:02:26 +00:00
def run():
if os.path.exists(PATH) == False:
os.mkdir(PATH)
log.start()
try:
func.qbt()
except:
log.errqbt()
sys.exit(1)
try:
bot.polling()
except:
log.errtelebot()
sys.exit(1)
if __name__ == "__main__":
run()