From 79b0475e5e648e6ff54ffa28fe7fcadd8ece298d Mon Sep 17 00:00:00 2001 From: justuser-31 Date: Wed, 5 Feb 2025 14:30:18 +0300 Subject: [PATCH] up --- db.py | 19 ++++++++ mention.py | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 db.py create mode 100644 mention.py diff --git a/db.py b/db.py new file mode 100644 index 0000000..7b5675a --- /dev/null +++ b/db.py @@ -0,0 +1,19 @@ +import os +import json + +if not os.path.exists('db.json'): + db = {'API_TOKEN': None} + js = json.dumps(db, indent=2) + with open("db.json", "w") as outfile: + outfile.write(js) + print('Created new db.json') + +def read(file = 'db.json'): + with open(file, "r", encoding="utf-8") as openfile: + db = json.load(openfile) + return db + +def write(db, file = 'db.json'): + js = json.dumps(db, indent=2, ensure_ascii=False) + with open(file, "w", encoding="utf-8") as outfile: + outfile.write(js) diff --git a/mention.py b/mention.py new file mode 100644 index 0000000..6021bf9 --- /dev/null +++ b/mention.py @@ -0,0 +1,137 @@ +import telebot +from telebot.types import Message, ChatMemberUpdated +from db import * + +API_TOKEN = read()['API_TOKEN'] # Replace with your bot's API token +bot = telebot.TeleBot(API_TOKEN) + +# Check if a user is an admin +def is_admin(chat_id, user_id): + chat_member = bot.get_chat_member(chat_id, user_id) + return chat_member.status in ['creator', 'administrator'] + +# Handle new chat members +@bot.message_handler(content_types=['new_chat_members']) +def handle_new_member(message: Message): + data = read() + chat_id = str(message.chat.id) + if chat_id not in data: + data[chat_id] = {} + + for new_member in message.new_chat_members: + # If user not bot + if not new_member.is_bot: + user_id = str(new_member.id) + user_nick = new_member.first_name + if user_id not in data[chat_id]: + data[chat_id][user_id] = user_nick + write(data) + +# Handle left chat members +@bot.message_handler(content_types=['left_chat_member']) +def handle_left_member(message: Message): + data = read() + chat_id = str(message.chat.id) + user_id = str(message.left_chat_member.id) + if chat_id in data and user_id in data[chat_id]: + del data[chat_id][user_id] + write(data) + +# Command to mention all users +@bot.message_handler(commands=['all', '#all']) +def mention_all(message: Message): + chat_id = str(message.chat.id) + user_id = str(message.from_user.id) + if is_admin(chat_id, user_id): + if chat_id in data and data[chat_id]: + mention_text = " ".join(f"{nick}" for user_id, nick in data[chat_id].items()) + bot.send_message(chat_id, mention_text, parse_mode='HTML') + else: + bot.send_message(chat_id, "No users in the list.") + +# Admin command to add a user +@bot.message_handler(commands=['add']) +def add_user(message: Message): + chat_id = str(message.chat.id) + user_id = str(message.from_user.id) + if is_admin(chat_id, user_id): + args = message.text.split() + if len(args) == 3: + data = read() + try: + new_user_id = int(args[1]) + new_user_nick = args[2] + if chat_id not in data: + data[chat_id] = {} + data[chat_id][new_user_id] = new_user_nick + bot.send_message(chat_id, f"Added user {new_user_nick} with ID {new_user_id}") + write(data) + except ValueError: + bot.send_message(chat_id, "Invalid user ID. Please provide a numeric ID.") + else: + bot.send_message(chat_id, "Usage: /add ") + else: + bot.send_message(chat_id, "You are not an admin.") + +# Command to list all users +@bot.message_handler(commands=['list']) +def list_users(message: Message): + chat_id = str(message.chat.id) + user_id = str(message.from_user.id) + if is_admin(chat_id, user_id): + data = read() + if chat_id in data and data[chat_id]: + user_list = "\n".join(f"ID: {user_id}, Nick: {nick}" for user_id, nick in data[chat_id].items()) + bot.send_message(chat_id, f"List of users:\n{user_list}") + else: + bot.send_message(chat_id, "No users in the list.") + +# Admin command to delete a user +@bot.message_handler(commands=['del']) +def delete_user(message: Message): + chat_id = str(message.chat.id) + user_id = str(message.from_user.id) + if is_admin(chat_id, user_id): + args = message.text.split() + if len(args) == 2: + data = read() + try: + user_to_delete_id = args[1] + if chat_id in data and user_to_delete_id in data[chat_id]: + del data[chat_id][user_to_delete_id] + bot.send_message(chat_id, f"Deleted user ID {user_to_delete_id}") + write(data) + else: + bot.send_message(chat_id, "User not found in the list.") + except ValueError: + bot.send_message(chat_id, "Invalid user ID. Please provide a numeric ID.") + else: + bot.send_message(chat_id, "Usage: /del ") + else: + bot.send_message(chat_id, "You are not an admin.") + +@bot.message_handler(func=lambda message: True) +def handle_mess(message: Message): + chat_id = str(message.chat.id) + user_id = str(message.from_user.id) + user_nick = message.from_user.first_name + + data = read() + + # Tag all + if '#all' in message.text or '@all' in message.text: + if is_admin(chat_id, user_id): + if chat_id in data and data[chat_id]: + mention_text = " ".join(f"{nick}" for user_id, nick in data[chat_id].items()) + bot.send_message(chat_id, mention_text, parse_mode='HTML') + else: + bot.send_message(chat_id, "No users in the list.") + + # Update user's first name if it changes + if chat_id in data and user_id in data[chat_id]: + if data[chat_id][user_id] != user_nick: + data[chat_id][user_id] = user_nick + write(data) + +# Start polling +bot.polling(none_stop=True)