discord.py 接受 - 如果子句:.MissingRequiredArgument:命令是缺少的必需参数 [错误] - 然后做一些事情

discord.py accept - if clause: .MissingRequiredArgument: command is a required argument that is missing [error] - then do something

我目前正在编写一个 discord 机器人,它有一个名为 !tikhelp 的命令,它显示所有 tiktok 命令,我试图包含一个函数,可以通过键入以下命令获取有关该命令的特定信息:!tikhelp commandname ,但是,这会阻止正常的 !tikhelp 命令,因为如果用户不包含第二个参数(应该是命令名称但不是必需的),它会引发错误。

这是我尝试检查参数是否存在,但在它可以之前就出现了错误:

@bot.command()
    async def tikhelp(self, ctx, command):
        
        if command:
            if command == "tikinfo"
                await ctx.send("To us this command, type !tikinfo accountname")
                return

        message = """```html
╔══════════════════════════════════════════════════════════════════════╗
    [TikTok Panel]   ║ Prefix: [!] ║  Commands: [0]  ║ version: [1.0]      
║══════════════════════════════════════════════════════════════════════║
║   [!]tikinfo  »» Get someones account information                    ║
║   [!]settik   »» set you TikTok Account Name                         ║
║   [!]mytik    »» Get you TikTok Account information                  ║
║   [!]tikuser  »» Display current Account Name                        ║
║══════════════════════════════════════════════════════════════════════║

╚══════════════════════════════════════════════════════════════════════╝
        ```"""
        
        await ctx.send(message)

即使使用 if 检查,它也会引发错误,是否有解决方案?

错误:

Traceback (most recent call last):
  File "C:\Users\fkahd\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\fkahd\Downloads\Selfbot\Selfbot\selfbot.py", line 56, in on_command_error
    raise error
  File "C:\Users\fkahd\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\fkahd\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
    await self.prepare(ctx)
  File "C:\Users\fkahd\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 789, in prepare
    await self._parse_arguments(ctx)
  File "C:\Users\fkahd\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 697, in _parse_arguments
    transformed = await self.transform(ctx, param)
  File "C:\Users\fkahd\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 542, in transform
    raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: command is a required argument that is missing.

感谢您的帮助

Discord 要求设置参数,除非您给它一个默认值。你可以定义command默认为None,然后检查它是否是None

    @bot.command()  # WARNING: It looks like this command is in a cog. If it is, you should be using `@commands.command` and then adding the command by adding the cog.
    async def tikhelp(self, ctx, command: str = None):
        if command is None:
            await ctx.send('Sorry, please specify command')
            return

        # do stuff

使用@bot.group() 代替@bot.command()

@bot.group()
async def tikhelp(self, ctx):
    ...

@tikhelp.command()
async def something(self, ctx):
    ...

调用一些东西:!tikhelp something

调用 tikhelp: !tikhelp