import telebot
from telebot import types
# Replace with your bot token
API_TOKEN = 'YOUR_BOT_TOKEN'
bot = telebot.TeleBot(API_TOKEN)
# A dictionary to store group members for each chat (chat_id -> set(user_ids))
group_members = {}
def is_admin(chat_id, user_id):
try:
member = bot.get_chat_member(chat_id, user_id)
# Check if the user is an admin or creator
return member.status in ['administrator', 'creator']
except Exception as e:
print(f"Error checking admin status: {e}")
return False
# When new members join, add them to our group member store.
@bot.message_handler(content_types=['new_chat_members'])
def new_member(message):
chat_id = message.chat.id
if chat_id not in group_members:
group_members[chat_id] = set()
for member in message.new_chat_members:
group_members[chat_id].add(member.id)
# When a member leaves, remove them from our store.
@bot.message_handler(content_types=['left_chat_member'])
def left_member(message):
chat_id = message.chat.id
if chat_id in group_members:
group_members[chat_id].discard(message.left_chat_member.id)
@bot.message_handler(commands=['remove'])
def handle_remove(message):
chat_id = message.chat.id
user_id = message.from_user.id
# Ensure the command is used only in groups
if message.chat.type == 'private':
bot.reply_to(message, "This command can only be used in groups.")
return
# Check if the user is an admin with the required permissions
if not is_admin(chat_id, user_id):
bot.reply_to(message, "You must be an admin with the appropriate permissions to use this command.")
return
args = message.text.split()
if len(args) < 2:
bot.reply_to(message, "Usage: /remove all, /remove <number>, or /remove bot")
return
arg = args[1].lower()
# Remove all non-admin members
if arg == "all":
if chat_id not in group_members or not group_members[chat_id]:
bot.reply_to(message, "No members found to remove.")
return
removed = 0
for member_id in list(group_members[chat_id]):
# Do not try to remove admins
if is_admin(chat_id, member_id):
continue
try:
bot.kick_chat_member(chat_id, member_id)
group_members[chat_id].remove(member_id)
removed += 1
except Exception as e:
print(f"Error removing {member_id}: {e}")
bot.reply_to(message, f"Removed {removed} members.")
# Remove all bot accounts (that are not admins)
elif arg == "bot":
removed = 0
if chat_id not in group_members or not group_members[chat_id]:
bot.reply_to(message, "No members found to remove.")
return
for member_id in list(group_members[chat_id]):
try:
member = bot.get_chat_member(chat_id, member_id)
if member.user.is_bot and member.status not in ['administrator', 'creator']:
bot.kick_chat_member(chat_id, member_id)
group_members[chat_id].remove(member_id)
removed += 1
except Exception as e:
print(f"Error removing bot {member_id}: {e}")
bot.reply_to(message, f"Removed {removed} bot members.")
# Remove a specified number of members (skipping admins)
else:
try:
count = int(arg)
except ValueError:
bot.reply_to(message, "Invalid argument. Use 'all', a number, or 'bot'.")
return
if chat_id not in group_members or not group_members[chat_id]:
bot.reply_to(message, "No members found to remove.")
return
removed = 0
for member_id in list(group_members[chat_id]):
if removed >= count:
break
if is_admin(chat_id, member_id):
continue
try:
bot.kick_chat_member(chat_id, member_id)
group_members[chat_id].remove(member_id)
removed += 1
except Exception as e:
print(f"Error removing {member_id}: {e}")
bot.reply_to(message, f"Removed {removed} members.")
# Example /start handler (optional)
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.reply_to(message, "Welcome! Add me to your group and make sure I have admin permissions.")
# Start polling for messages
bot.polling()
Comments
Post a Comment