为什么此命令未在机器人中同步 -discord.py

问题描述 投票:0回答:1

我正在使用discord.py并有一个使用app_commands.CommandTree的机器人。

当我去定义并添加 2 个斜杠命令时,1 个同步并显示在 UI 中,另一个则没有。

有人可以帮助我理解为什么会发生这种情况吗?

async def is_owner(interaction: discord.Interaction) -> bool:
    return interaction.user.id == OWNER_ID

@app_commands.command(name="resyncmessage", description="Checks message sent and posts or edits it")
@app_commands.check(is_owner)
async def resyncmessage(interaction: discord.Interaction, guildId: int, channelId: int, messageId: int):
    logging.debug("Entering resyncMessage():")
    await interaction.response.defer()
    embed = discord.Embed(title = "resyncMessage")
    embedMessage = await interaction.followup.send(embed=embed, ephemeral=True)
    await procResyncmessage(guildId, channelId, messageId, embed, embedMessage)
    await embedMessage.edit(embed=embed)

@app_commands.command(name="resync", description="Checks all messages and syncs")
@app_commands.check(is_owner)
async def resync(interaction: discord.Interaction, channel: discord.TextChannel):
    logging.debug("Entering resync():")
    await interaction.response.defer()
    embed = discord.Embed(title = "Resync")
    embedMessage = await interaction.followup.send(embed=embed, ephemeral=True)
    await procResync(channel, embed, embedMessage)
    await embedMessage.edit(embed=embed)

class aclient(discord.Client):
    def __init__(self):
        intents = discord.Intents.default()
        intents.members = True
        intents.messages = True
        intents.message_content = True
        logging.debug(f"intents={intents}")
        super().__init__(intents=intents)
        self.synced = False
    
    async def on_ready(self):
        logging.debug("on_ready started: {self}")
        await self.wait_until_ready()
        logging.debug("ready.")
    
    async def setup_hook(self):
        logging.debug(f"We have logged in as {self.user}.")
        if not self.synced:
            logging.debug(f"Adding command={resync}, guild={LH}")
            tree.add_command(resync, guild=LH)
            logging.debug(f"Adding command={resyncmessage}, guild={LH}")
            tree.add_command(resyncmessage, guild=LH)
            logging.debug("Starting sync...")
            await tree.sync()
            self.synced = True
            logging.debug("Sync complete.")
        else:
            logging.debug("Already synced.")
        logging.debug("Reboot complete, resync started...")
        await self.resyncTrackers()

client = aclient()
tree = app_commands.CommandTree(client)
client.tree = tree

logging.debug("Running...")
client.run(TOKEN)

应用程序日志:

2024-02-15 20:45:59,308 - root - DEBUG - create_pool:<mysql.connector.pooling.MySQLConnectionPool object at 0x7f43f4e97fd0>
2024-02-15 20:45:59,309 - root - DEBUG - __init__:<MySQLPool.MySQLPool object at 0x7f43f4f8f850>
2024-02-15 20:45:59,590 - root - DEBUG - intents=<Intents value=3276543>
2024-02-15 20:45:59,592 - root - DEBUG - Running...
2024-02-15 20:45:59,593 - asyncio - DEBUG - Using selector: EpollSelector
2024-02-15 20:45:59,595 - discord.client - INFO - logging in using static token
2024-02-15 20:46:00,215 - root - DEBUG - We have logged in as ******.
2024-02-15 20:46:00,215 - root - DEBUG - Adding command=<discord.app_commands.commands.Command object at 0x7f43f1ef4d90>, guild=<Object id=646... type=<class 'discord.object.Object'>>
2024-02-15 20:46:00,215 - root - DEBUG - Adding command=<discord.app_commands.commands.Command object at 0x7f43f1eb33d0>, guild=<Object id=646... type=<class 'discord.object.Object'>>
2024-02-15 20:46:00,215 - root - DEBUG - Starting sync...
2024-02-15 20:46:00,442 - root - DEBUG - Sync complete.
2024-02-15 20:46:00,442 - root - DEBUG - Reboot complete, resync started...

当我在带有机器人的服务器上时,这就是我所看到的:

如果我尝试检查斜杠命令,我只能看到 /resync,没有提及 /resyncmessages。

如何将第二个斜杠命令添加到命令树中并为我的公会同步?

python discord.py
1个回答
0
投票

同步

resyncmessage
命令失败,因为其参数必须小写。

@app_commands.command(name="resyncmessage", description="Checks message sent and posts or edits it")
@app_commands.check(is_owner)
async def resyncmessage(interaction: discord.Interaction, guildid: int, channelid: int, messageid: int):
    ...

但是你应该看到这个错误

discord.app_commands.errors.CommandSyncFailure: Failed to upload commands to Discord (HTTP status 400, error code 50035)
In command 'resyncmessage' defined in function 'resyncmessage'
  In parameter 'guildId'
  In parameter 'channelId'
  In parameter 'messageId'

这表明参数存在问题。检查类型注释后,唯一的问题与名称有关。 由于命令名称必须小写,因此我们只能假设其参数相同。

这就是我调试这个问题的方法。

© www.soinside.com 2019 - 2024. All rights reserved.