def shutdown(client, arg): if client in clients: broadcast(Participant.Bot("server"), f"{clients[client].name} is shutting down the server") else: broadcast(Participant.Bot("server"), f"The host is shutting down the server") for c in clients: c.close() clients.clear() server.close() raise SystemExit
def kick(client, participant): to_be_kicked = None for c in clients: if clients[c].name.lower() == participant.lower(): to_be_kicked = c broadcast(Participant.Bot("server"), f"{clients[c].name} has been kicked from the chat!") print(f"{clients[c].name} has been kicked from the server!") if isinstance(to_be_kicked, socket.socket): clients.pop(to_be_kicked) to_be_kicked.close()
def update_name(client, new_name): # Do not let host change name: if client == host: print( "Sorry, as a host we have chosen that you should remained named as \"Host\"" ) else: # If a command has been performed, broadcast it to all the other clients: # Broadcast as bot because we don't want the bots to respond broadcast( Participant.Bot("server"), f"{clients[client].name} has changed their name to {new_name}") # Then update the clients new name: clients[client].name = new_name
def listen_for_data(c): while True: try: sender, message = pickle.loads(c.recv(1024)) if not perform_command(c, message) and len(message) > 0: print(f"{sender.name}: {message}") broadcast(sender, message) except ConnectionResetError: p = clients.pop(c, Participant.Bot("Someone")) print(f"{p.name} left the server") broadcast(Participant.Person("server"), f"{p.name} left the chat!") c.close() break except ConnectionAbortedError: break
def send_help(client, arg): available_commands = list(commands.keys()) available_commands.append("/logout") help_message = f"These are the following available commands:\n{available_commands}" for c in clients: if isinstance(clients[c], Participant.Bot): help_message += f"\n{clients[c].name} " \ f"responds to any sentences that includes one or more of thse keywords:" \ f"\n{clients[c].get_help()}" if client == host: print(help_message) else: package = pickle.dumps((Participant.Bot("server"), help_message)) client.send(package)