import logging import sys import os sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from bot.TeamTalk import TeamTalk, Event, EventType, MessageType # Load the watchlist from a file (watchlist.txt) def load_watchlist(): """Load the watchlist of prohibited words.""" watchlist = [] try: with open("watchlist.txt", "r") as f: watchlist = [line.strip().lower() for line in f.readlines()] except FileNotFoundError: logging.error("Watchlist file not found. Please create 'watchlist.txt'.") return watchlist # Check if the user's message contains any prohibited words def contains_prohibited_word(message: str, watchlist: list) -> bool: """Check if a message contains any prohibited words from the watchlist.""" for word in watchlist: if word in message.lower(): return True return False # Kick the user and broadcast the reason def kick_user_and_broadcast(ttclient: TeamTalk, user_name: str) -> None: """Kick the user and broadcast a message about the kick.""" ttclient.kick_user(user_name) broadcast_message = f"{user_name} has been kicked for using prohibited language." ttclient.broadcast_message(broadcast_message) logging.info(f"Kicked user: {user_name} for prohibited message.") # Event handler for a user text message def on_user_text_message(event: Event, ttclient: TeamTalk): """Handle the event when a user sends a text message.""" watchlist = load_watchlist() message = event.message.text user_name = event.message.sender.name # Check if the message contains a prohibited word if contains_prohibited_word(message, watchlist): logging.info(f"User {user_name} used a prohibited word. Kicking user...") kick_user_and_broadcast(ttclient, user_name) else: logging.info(f"User {user_name}'s message: '{message}' is clean.") # Map event types to their respective handler functions event_handlers = { EventType.USER_TEXT_MESSAGE: on_user_text_message, # You can add more event handlers here for different event types. } def run_event_handler(event: Event, ttclient: TeamTalk): """Run the appropriate event handler based on the event type.""" try: event_handler = event_handlers.get(event.event_type) if event_handler: event_handler(event, ttclient) else: logging.info(f"No handler for event type: {event.event_type.name}") except Exception as e: logging.error(f"Error in event handling: {e}")