Discord.py 尝试按命令获取文件

问题描述 投票:0回答:1

我正在尝试制作一个带有命令的机器人,用户可以触发该命令来发送文件。我设法让它工作,但现在每次我启动机器人时都会说:这个客户端已经有一个关联的命令树。也许有人可以帮助我,如果有旧的,是否有命令在机器人启动时删除它?

我的代码:

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.")

错误:

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

python pip discord discord.py command
1个回答
0
投票

您的

client
变量已经有一个命令树。您可以直接使用它,而无需重新定义另一个。

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.")
© www.soinside.com 2019 - 2024. All rights reserved.