diff --git a/mod.py b/mod.py index 29d7068..f621bbb 100644 --- a/mod.py +++ b/mod.py @@ -4,7 +4,7 @@ import json from telebot import types,util -global db, users, la +global users, la ####### CREATE DB IF NOT EXIST @@ -31,25 +31,24 @@ if not os.path.exists('la.json'): ############WORK WITH DBs########## def read_db(): - global db - with open('db.json', 'r') as openfile: - db = json.load(openfile) -def write_db(): - global db - js = json.dumps(db, indent=2) - with open('db.json', 'w') as outfile: - outfile.write(js) + with open('db.json', 'r') as openfile: + db = json.load(openfile) + return db +def write_db(db): + js = json.dumps(db, indent=2) + with open('db.json', 'w') as outfile: + outfile.write(js) def read_users(): - global users - with open('users.json', 'r') as openfile: - users = json.load(openfile) + global users + with open('users.json', 'r') as openfile: + users = json.load(openfile) def write_users(): - global users - js = json.dumps(users, indent=2) - with open('users.json', 'w') as outfile: - outfile.write(js) + global users + js = json.dumps(users, indent=2) + with open('users.json', 'w') as outfile: + outfile.write(js) # LA - Low Admin. # Admin permissions in bot without admin rights. @@ -189,7 +188,7 @@ def analytic(message): #############TOKEN INIT##### -read_db() +db = read_db() read_users() bot = telebot.TeleBot(db['token']) @@ -320,20 +319,19 @@ def unban(message): def setwelcome(message): try: if have_rights(message): - global db + db = read_db() db[str(message.chat.id)] = message.html_text[ message.text.find(' ') + 1 :] bot.reply_to(message, f'''Установлено новое приветственное сообщение: \n{db[str(message.chat.id)]}''', parse_mode='HTML') - write_db() + write_db(db) except Exception as e: catch_error(message, e) @bot.message_handler(commands=['welcome']) def welcome(message): try: - global db - read_db() + db = read_db() bot.reply_to(message, f'''Приветственное сообщение: \n{db[str(message.chat.id)]}''', parse_mode='HTML') except Exception as e: @@ -377,6 +375,22 @@ def delete(message): except Exception as e: catch_error(message, e) +@bot.message_handler(commands=['pin']) +def pin(message): + try: + if have_rights(message): + bot.pin_chat_message(message.chat.id, message.reply_to_message.id) + except: + catch_error(message, e) + +@bot.message_handler(commands=['unpin']) +def unpin(message): + try: + if have_rights(message): + bot.unpin_chat_message(message.chat.id, message.reply_to_message.id) + except: + catch_error(message, e) + ### EXPEREMENTAL @bot.message_handler(commands=['secret']) @@ -438,7 +452,7 @@ def la_list(message): try: if have_rights(message, set_la=True): la = read_la() - if have_la(str(message.chat.id)): + if have_la(message): s = 'Список администраторов в режиме low-admin:\n' for i in la[str(message.chat.id)]: s = s + '\n@' + i @@ -451,7 +465,7 @@ def la_add(message): try: if have_rights(message, set_la=True): la = read_la() - if have_la(message.chat.id): + if have_la(message): nick = message.text.split()[1][1:] print(la) la[str(message.chat.id)].append(nick) @@ -466,7 +480,7 @@ def la_del(message, set_la=True): try: if have_rights(message, set_la=True): la = read_la() - if have_la(message.chat.id): + if have_la(message): nick = message.text.split()[1][1:] if nick in la[str(message.chat.id)]: @@ -514,8 +528,12 @@ def support(message): @bot.message_handler(content_types=['new_chat_members']) def handler_new_member(message): try: - global db - read_db() + db = read_db() + if str(message.chat.id) not in db: + db[str(message.chat.id)] = 'Приветственное сообщение не задано.\n Задайте его с помощью /setwelcome ...' + la = read_la() + la[str(message.chat.id)] = [] + write_la(la) bot.reply_to(message, db[str(message.chat.id)], parse_mode='HTML') analytic(message) except Exception as e: