斜杠命令不同步到特定的公会在discord.py



所以我有一个不和谐机器人,整体功能正确,但命令同步行为非常奇怪。

我读到,在'on_ready'中运行完整的全局同步是一个坏主意,可以得到我的bot速率限制,而另一种选择是有一个/sync函数,它只存在于测试服务器上,并将运行完整的tree.sync()。我试图实现这一点,但由于某种原因,我不能得到/同步功能出现在我的测试服务器上,更糟糕的是,由于某种原因,我的完整的全局同步似乎无论如何都在运行。

为了测试,我有两个不同的公会,其中一个是主要的测试公会,将用于bot管理。下面是相关的代码片段:
# -- setup --
# create client
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
global_synced = False
tree = app_commands.CommandTree(client)
# -- events --
@client.event
async def on_ready():
# sync commands
await client.wait_until_ready()
if(not global_synced):
for g in guilds: 
# Before I added this loop and following if statement I was getting a "403 Forbidden"
# I guess the error was because the secondary guild doesn't have access to the
# discord Object of the main guild? IDK
if(g.id == TEST_GUILD_ID): 
await tree.sync(guild=discord.Object(id=TEST_GUILD_ID))
global_synced = True
# -- commands --
@tree.command(name = "run_bot", description="Runs the bot")
async def self(interaction: discord.Interaction):
# this function is available to all guilds and runs the bot
return
@tree.command(name = "sync", description="Syncs the bot commands", guild=discord.Object(id=TEST_GUILD_ID))
async def self(interaction: discord.Interaction):
# this function is supposed to be available to only the main test server
await client.wait_until_ready()
await tree.sync()
return

所以这是我的问题:

  1. "/sync"没有出现在我的主测试公会
  2. "/run_bot"出现在我的二级测试公会,即使我明确地说不同步所有?

我很茫然。我没有得到任何错误,我已经仔细阅读了文档,但找不到答案。这是否与异步代码(我的宿敌)有关?请帮助!

你错过了向公会复制全局命令。

self.tree.copy_global_to(guild=guild)
await self.tree.sync(guild=guild)

最新更新