Discord.py多斜杠命令配置异常:仅单个命令显示求助
解决Discord.py多斜杠命令仅显示一个的问题
你的核心问题是没有在on_ready事件中执行命令同步操作,导致只有第一个命令被成功注册到Discord服务器。以下是修复后的完整代码:
import discord from discord import app_commands import os TOKEN = os.environ['TOKEN'] # 替换为你的实际服务器ID GUILD_ID = 123456789012345678 intents = discord.Intents.default() client = discord.Client(intents=intents) tree = app_commands.CommandTree(client) @client.event async def on_ready(): print('We have logged in as {0.user}'.format(client)) await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='for furries')) # 关键操作:将所有注册的命令同步到指定服务器 await tree.sync(guild=discord.Object(id=GUILD_ID)) print("命令已同步完成") @tree.command(name="testing", description="The Description", guild=discord.Object(id=GUILD_ID)) async def first_command(interaction): await interaction.response.send_message("Hello! V2") @tree.command(name="ping", description="Ping Pong", guild=discord.Object(id=GUILD_ID)) async def second_command(interaction): await interaction.response.send_message("Pong!") try: client.run(os.getenv("TOKEN")) except discord.HTTPException as e: if e.status == 429: print("The Discord servers denied the connection for making too many requests") else: raise e
关键修复说明
- 必须执行
tree.sync():这一步会把你定义的所有斜杠命令提交给Discord服务器,未同步的命令不会在客户端显示。 - 统一管理服务器ID:用变量存储服务器ID,避免重复硬编码,降低出错概率。
- 生效速度说明:服务器专属命令同步后几秒即可生效;如果是全局命令(去掉
guild参数),需要等待约1小时才能在所有服务器显示。
测试时可以退出Discord客户端再重新登录,确保客户端缓存的命令列表被刷新。
内容的提问来源于stack exchange,提问作者Droomish
相关产品推荐
相关产品推荐

