import discordimport asyncioimport aiofiles# Discord bot tokenTOKEN = 'your_discord_bot_token'# Discord voice channel IDs for red and blue teamsTEAM_RED_CHANNEL_ID = 123456789012345678 # Replace with your Red team's voice channel IDTEAM_BLUE_CHANNEL_ID = 987654321098765432 # Replace with your Blue team's voice channel ID# Path to the server log fileLOG_FILE_PATH = 'server_logs.txt'# Dictionary to track which team each player belongs toplayer_team = {}# Setting up the Discord client with necessary intentsintents = discord.Intents.default()intents.members = True # Enable member eventsclient = discord.Client(intents=intents)@client.eventasync def on_ready(): # This event triggers when the bot successfully logs in print(f'Bot logged in as {client.user}') # Start monitoring logs when the bot is ready client.loop.create_task(monitor_logs())# Function to move a player to the correct voice channel based on their teamasync def move_player_to_team_channel(member, team): try: # Check which team the player is on and get the appropriate voice channel if team == 'Red': channel = client.get_channel(TEAM_RED_CHANNEL_ID) elif team == 'Blue': channel = client.get_channel(TEAM_BLUE_CHANNEL_ID) else: return # Unknown team, no action taken if member.voice: # Move player to the correct team channel await member.move_to(channel) print(f'Moved {member.name} to {team} channel.') except Exception as e: print(f'Error moving {member.name}: {e}')# Function to monitor the server log file and detect team changesasync def monitor_logs(): async with aiofiles.open(LOG_FILE_PATH, 'r') as f: await f.seek(0, 2) # Go to the end of the log file while True: line = await f.readline() if not line: # If no new line is found, wait for a while and then check again await asyncio.sleep(1) continue # Example log line: "Player X moved to Team Red" if 'moved to Team' in line: parts = line.split() player_name = parts[1] # Player name (adjust depending on your log format) team = parts[-1] # Team (Red/Blue) # Update player's team in the dictionary player_team[player_name] = team # Try to find the player on the Discord server guild = client.guilds[0] # Adjust if the bot is in multiple servers member = discord.utils.get(guild.members, name=player_name) if member: # Move the player to the correct voice channel await move_player_to_team_channel(member, team)# Start the botclient.run(TOKEN)
import discordimport asyncioimport aiofiles# Discord bot tokenTOKEN = 'your_discord_bot_token'# Discord voice channel IDs for red and blue teamsTEAM_RED_CHANNEL_ID = 123456789012345678 # Replace with your Red team's voice channel IDTEAM_BLUE_CHANNEL_ID = 987654321098765432 # Replace with your Blue team's voice channel ID# Path to the server log fileLOG_FILE_PATH = 'server_logs.txt'# Dictionary to track which team each player belongs toplayer_team = {}# Mapping of in-game player names to Discord usernamesplayer_to_discord = { "Sup(BR)": "lala", # Example: Sup(BR) is the in-game name, and lala is the Discord username # Add more mappings as needed # "Player_in_game": "Discord_username",}# Setting up the Discord client with necessary intentsintents = discord.Intents.default()intents.members = True # Enable member eventsintents.message_content = True # Enable message eventsclient = discord.Client(intents=intents)@client.eventasync def on_ready(): print(f'Bot logged in as {client.user}') client.loop.create_task(monitor_logs())@client.eventasync def on_message(message): # Check if the message starts with the command to add a player if message.content.startswith('!addplayer'): # Extract the in-game player name from the message try: in_game_name = message.content.split(' ')[1] discord_username = message.author.name # Get the Discord username of the person who sent the message # Add the player to the dictionary player_to_discord[in_game_name] = discord_username # Send a confirmation message await message.channel.send(f'Player {in_game_name} has been linked to Discord user {discord_username}.') print(f'Added {in_game_name} -> {discord_username}') except IndexError: # If the player name was not provided in the message await message.channel.send('Please provide the in-game name using the command: !addplayer NomeInGame')async def move_player_to_team_channel(member, team): try: if team == 'Red': channel = client.get_channel(TEAM_RED_CHANNEL_ID) elif team == 'Blue': channel = client.get_channel(TEAM_BLUE_CHANNEL_ID) else: return # Unknown team, no action taken if member.voice: await member.move_to(channel) print(f'Moved {member.name} to {team} channel.') except Exception as e: print(f'Error moving {member.name}: {e}')async def monitor_logs(): async with aiofiles.open(LOG_FILE_PATH, 'r') as f: await f.seek(0, 2) # Go to the end of the log file while True: line = await f.readline() if not line: await asyncio.sleep(1) continue if 'moved to Team' in line: parts = line.split() player_name = parts[1] # Player name team = parts[-1] # Team (Red/Blue) player_team[player_name] = team discord_username = player_to_discord.get(player_name) if discord_username: guild = client.guilds[0] member = discord.utils.get(guild.members, name=discord_username) if member: await move_player_to_team_channel(member, team) else: print(f'Member {discord_username} not found in the guild.') else: print(f'Player {player_name} not mapped to a Discord user.')client.run(TOKEN)
something like this:Code: [Select]import discordimport asyncioimport aiofiles# Discord bot tokenTOKEN = 'your_discord_bot_token'# Discord voice channel IDs for red and blue teamsTEAM_RED_CHANNEL_ID = 123456789012345678 # Replace with your Red team's voice channel IDTEAM_BLUE_CHANNEL_ID = 987654321098765432 # Replace with your Blue team's voice channel ID# Path to the server log fileLOG_FILE_PATH = 'server_logs.txt'# Dictionary to track which team each player belongs toplayer_team = {}# Mapping of in-game player names to Discord usernamesplayer_to_discord = { "Sup(BR)": "lala", # Example: Sup(BR) is the in-game name, and lala is the Discord username # Add more mappings as needed # "Player_in_game": "Discord_username",}# Setting up the Discord client with necessary intentsintents = discord.Intents.default()intents.members = True # Enable member eventsintents.message_content = True # Enable message eventsclient = discord.Client(intents=intents)@client.eventasync def on_ready(): print(f'Bot logged in as {client.user}') client.loop.create_task(monitor_logs())@client.eventasync def on_message(message): # Check if the message starts with the command to add a player if message.content.startswith('!addplayer'): # Extract the in-game player name from the message try: in_game_name = message.content.split(' ')[1] discord_username = message.author.name # Get the Discord username of the person who sent the message # Add the player to the dictionary player_to_discord[in_game_name] = discord_username # Send a confirmation message await message.channel.send(f'Player {in_game_name} has been linked to Discord user {discord_username}.') print(f'Added {in_game_name} -> {discord_username}') except IndexError: # If the player name was not provided in the message await message.channel.send('Please provide the in-game name using the command: !addplayer NomeInGame')async def move_player_to_team_channel(member, team): try: if team == 'Red': channel = client.get_channel(TEAM_RED_CHANNEL_ID) elif team == 'Blue': channel = client.get_channel(TEAM_BLUE_CHANNEL_ID) else: return # Unknown team, no action taken if member.voice: await member.move_to(channel) print(f'Moved {member.name} to {team} channel.') except Exception as e: print(f'Error moving {member.name}: {e}')async def monitor_logs(): async with aiofiles.open(LOG_FILE_PATH, 'r') as f: await f.seek(0, 2) # Go to the end of the log file while True: line = await f.readline() if not line: await asyncio.sleep(1) continue if 'moved to Team' in line: parts = line.split() player_name = parts[1] # Player name team = parts[-1] # Team (Red/Blue) player_team[player_name] = team discord_username = player_to_discord.get(player_name) if discord_username: guild = client.guilds[0] member = discord.utils.get(guild.members, name=discord_username) if member: await move_player_to_team_channel(member, team) else: print(f'Member {discord_username} not found in the guild.') else: print(f'Player {player_name} not mapped to a Discord user.')client.run(TOKEN)