Search code examples
pythonpipdiscorddiscord.pycommand

Discord.py Trying to get a file per command


I'm trying to make a bot with a command wich can be triggered by a user to send a file. I managed to get it working, but now it's says everytime i launch the bot, that: This client already has an associated command tree. Maybe someone can help me with that, if there is a old, is there a command to delete it on startup of the bot?

My code:

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True
client = commands.Bot(command_prefix="/", intents=intents)

commandTree = discord.app_commands.CommandTree(client)

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')

@commandTree.command(name="command", description="Some command")
async def upload(ctx, *, file: discord.File = None):
    if not file:
        await ctx.send("You didn't include a file.")
    else:
        await file.save(f"uploads/{file.filename}")
        await ctx.send(f"File '{file.filename}' uploaded successfully.")

The error:

discord.errors.ClientException: This client already has an associated command tree.


Solution

  • Your client variable already have a command Tree. You can just use it without redefine another one.

    import discord
    from discord.ext import commands
    
    intents = discord.Intents.default()
    intents.message_content = True
    client = commands.Bot(command_prefix="/", intents=intents)
    
    # commandTree = discord.app_commands.CommandTree(client)
    
    @client.event
    async def on_ready():
        print(f'We have logged in as {client.user}')
    
    @client.tree.command(name="command", description="Some command")
    async def upload(ctx, *, file: discord.File = None):
        if not file:
            await ctx.send("You didn't include a file.")
        else:
            await file.save(f"uploads/{file.filename}")
            await ctx.send(f"File '{file.filename}' uploaded successfully.")