英文:
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 "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.
Below is my code:
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')
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='!', 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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论