python Discord.py 2.0.0:不一致,错误,ClientException:此客户端已经有关联的命令树

bogh5gae  于 2023-06-28  发布在  Python
关注(0)|答案(1)|浏览(114)

我是www.example.com的新用户Discord.py,当我尝试向我的discord bot添加斜杠命令时,我遇到了以下错误:

Traceback (most recent call last):
  File "C:\programs\bot.py", line 5, in <module>
    tree = app_commands.CommandTree(bot)
  File "C:\Users\trnt\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\app_commands\tree.py", line 134, in __init__
    raise ClientException('This client already has an associated command tree.')
discord.errors.ClientException: This client already has an associated command tree.

下面是我的代码:

import discord
from discord import app_commands
from discord.ext.commands import Bot
bot = Bot(command_prefix='!', intents=discord.Intents.all())
tree = app_commands.CommandTree(bot)

@bot.event
async def on_ready():
    print(f'Bot Name: {bot.user}')
    for server in bot.guilds:
        await tree.sync(guild=discord.Object(id=server.id))

@tree.command(name="test", description="Test to see if slash commands are working")
async def test(interaction):
    await interaction.response.send_message("Test")

bot.run('My Token')

我使用的是Discord.py 2.0.0和Python 3.10.11
有人能告诉我我哪里做错了吗?我在stackoverflow上搜索过问题,但没有一个对我有用。希望你能帮忙。谢谢你!

qpgpyjmq

qpgpyjmq1#

commands.Bot已经有一个命令树,您不需要重新定义它。

import discord
from discord import app_commands
from discord.ext.commands import Bot

bot = Bot(command_prefix='!', intents=discord.Intents.all())

@bot.event
async def on_ready():
    print(f'Bot Name: {bot.user}')
    for server in bot.guilds:
        await bot.tree.sync(guild=discord.Object(id=server.id))

@bot.tree.command(name="test", description="Test to see if slash commands are working")
async def test(interaction):
    await interaction.response.send_message("Test")

bot.run('My Token')

相关问题