I have the following code:
def message_handler(update: Update, context: CallbackContext): user = update.effective_user
chat_id = update.effective_chat.id chat_type = update.effective_chat.type
# Check if the message is sent in a supergroup and not from a bot or an administrator
if chat_type == 'supergroup' and user and not user.is_bot and not user.id in [admin.user.id for admin in context.bot.get_chat_administrators(chat_id)]: # Load settings
settings = load_settings()
# Check if flood control is enabled if str(chat_id) in settings and settings[str(chat_id)].get('flood') == 'on':
# Check for flood current_time = time.time()
if user.id not in context.chat_data: context.chat_data[user.id] = {'last_message_time': current_time, 'message_count': 1}
else: user_data = context.chat_data[user.id]
if current_time - user_data['last_message_time'] <= FLOOD_PERIOD: user_data['message_count'] += 1
else: user_data['message_count'] = 1
user_data['last_message_time'] = current_time
# If flood detected if context.chat_data[user.id]['message_count'] >= FLOOD_THRESHOLD:
# Delete the flood messages context.bot.delete_message(chat_id=chat_id, message_id=update.message.message_id)
# Check if user has already been warned
if not context.chat_data[user.id].get('warned', False): # Send warning message
warning_message = f"⚠️ Warning: User {user.mention_html()} has sent {FLOOD_THRESHOLD} or more messages in {FLOOD_PERIOD} second(s). Further spamming will result in a mute." context.bot.send_message(chat_id=chat_id, text=warning_message, parse_mode='HTML')
context.chat_data[user.id]['warned'] = True else:
# Mute the user for 1 hour permissions = ChatPermissions(can_send_messages=False)
try: context.bot.restrict_chat_member(chat_id=chat_id, user_id=user.id, permissions=permissions, until_date=current_time + 3600)
# Send notification about the mute mute_notification = f"???? User {user.mention_html()} has been muted for 1 hour due to continued spamming behavior."
context.bot.send_message(chat_id=chat_id, text=mute_notification, parse_mode='HTML') # Reset user's message count and warned status
context.chat_data[user.id]['message_count'] = 0 context.chat_data[user.id]['warned'] = False
except Exception as e: print(f"Error while muting user: {e}")
The bot is supposed to delete all of the user’s spam messages, but for some reason it deletes only the last message before the warning message, not all spam messages.
I want the bot to delete all of the user’s spam messages. Apparently, according to this code, there is a problem with the condition or I don’t know what the problem is.