Gaming Zone > Unreal Tournament 2004

Discord - autokick to red/blue voicechannel

<< < (2/2)

sup:
something like this:


--- Code: ---import discord
import asyncio
import aiofiles

# Discord bot token
TOKEN = 'your_discord_bot_token'

# Discord voice channel IDs for red and blue teams
TEAM_RED_CHANNEL_ID = 123456789012345678  # Replace with your Red team's voice channel ID
TEAM_BLUE_CHANNEL_ID = 987654321098765432  # Replace with your Blue team's voice channel ID

# Path to the server log file
LOG_FILE_PATH = 'server_logs.txt'

# Dictionary to track which team each player belongs to
player_team = {}

# Mapping of in-game player names to Discord usernames
player_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 intents
intents = discord.Intents.default()
intents.members = True  # Enable member events
intents.message_content = True  # Enable message events
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'Bot logged in as {client.user}')
    client.loop.create_task(monitor_logs())

@client.event
async 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)

--- End code ---

Ottiishiia:

--- Quote from: sup on September 15, 2024, 16:37 ---something like this:


--- Code: ---import discord
import asyncio
import aiofiles

# Discord bot token
TOKEN = 'your_discord_bot_token'

# Discord voice channel IDs for red and blue teams
TEAM_RED_CHANNEL_ID = 123456789012345678  # Replace with your Red team's voice channel ID
TEAM_BLUE_CHANNEL_ID = 987654321098765432  # Replace with your Blue team's voice channel ID

# Path to the server log file
LOG_FILE_PATH = 'server_logs.txt'

# Dictionary to track which team each player belongs to
player_team = {}

# Mapping of in-game player names to Discord usernames
player_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 intents
intents = discord.Intents.default()
intents.members = True  # Enable member events
intents.message_content = True  # Enable message events
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'Bot logged in as {client.user}')
    client.loop.create_task(monitor_logs())

@client.event
async 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)

--- End code ---

--- End quote ---
Heart eyes ❤

Navigation

[0] Message Index

[*] Previous page

Go to full version