在 discord.py 中将引号作为参数传递时出错

Has*_*Ali 2 python discord.py

我有一个错误在我的不和谐机器人目前,当有人在命令使用引号,我得到以下错误:discord.ext.commands.errors.ExpectedClosingQuoteError: Expected closing ".看起来这是一个开放的bugdiscord.py根据这个bug报告,以及该代码discord.py。这是问题有点烦人,我想知道目前是否有任何解决方案。到目前为止,这是我的代码:

@bot.command()
async def f(ctx, *args):
hearts = (':heart:', ':orange_heart:', ':yellow_heart:', ':green_heart:', ':blue_heart:', ':purple_heart:')

if not args:
    response = '**{0}** has paid their respects {1}'.format(ctx.author.name,
                                                            hearts[random.randint(0, len(hearts) - 1)])
else:
    response = '**{0}** has paid their respects {1} {2}'.format(ctx.author.name, ' '.join(args),
                                                                hearts[random.randint(0, len(hearts) - 1)])
Run Code Online (Sandbox Code Playgroud)

当用户通过输入!f "The thingDiscord调用此函数时,我会得到上面提到的命令。无论如何我有可能解决这个问题吗?我认为这是不可能的,因为从参数传递到函数的那一刻起,就会抛出错误。我想我可以编辑discord.py来解决这个问题,但它可能会破坏我机器人的其他区域。当 iOS 用户使用键盘上的引号并键入类似!f Josh's face. 有没有办法让所有的引号都通过这个函数成功传递?

谢谢!

Abd*_*ziz 5

它将命令之后的所有内容都作为args默认值设置为 None,因此如果没有 args,您将不会收到错误消息discord.ext.commands.errors.MissingRequiredArgument: args is a required argument that is missing.

我也更改了格式以使用f 字符串,我相信它更容易。在你的情况下使用random.choise()更好

来自文档的示例

@bot.command()
async def f(ctx, *, args=None):
    hearts = (':heart:', ':orange_heart:', ':yellow_heart:',
              ':green_heart:', ':blue_heart:', ':purple_heart:')

    if not args:
        response = f'**{ctx.author.name}** has paid their respects {random.choice(hearts)}'
    else:
        response = f'**{ctx.author.name}** has paid their respects {args} {random.choice(hearts)}'

    await ctx.send(response)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明