Discord.py 2.0.0:discord.errors.ClientException:此客户端已有关联的命令树

huangapple go评论77阅读模式
英文:

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

问题

我是新手 Discord.py 用户,当我尝试将斜杠命令添加到我的 Discord 机器人时,我遇到了以下错误:

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 上搜索了问题,但没有一个适用于我。感谢您的帮助。谢谢!

英文:

I am new to Discord.py and when i tries to add a slash command to my discord bot, I encountered the following error:

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

Below is my code:

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

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

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

bot.run(&#39;My Token&#39;)

I am using Discord.py 2.0.0 and Python 3.10.11

Can someone tell me where I did wrong? I have searched questions on stackoverflow but none of them worked for me. Help would be appreciated. Thank you!

答案1

得分: 2

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="测试是否正常工作")
async def test(interaction):
    await interaction.response.send_message("测试")

bot.run('我的令牌')
英文:

commands.Bot already has a command tree, there's no need for you to define it again.

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

bot = Bot(command_prefix=&#39;!&#39;, intents=discord.Intents.all())

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

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

bot.run(&#39;My Token&#39;)

huangapple
  • 本文由 发表于 2023年6月12日 06:59:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定