目前,当有人在命令中使用引号时,我的 Discord 机器人出现错误,我收到以下错误:discord.ext.commands.errors.ExpectedClosingQuoteError: Expected closing ".
这似乎是一个开放的错误discord.py
根据这个错误报告 https://github.com/Cog-Creators/Red-DiscordBot/issues/3055也这段代码 https://github.com/Rapptz/discord.py/blob/874c2cd3c9ed47fa42eea184b167b8c0c64ea18e/discord/ext/commands/view.py#L30-L48 in 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)])
当用户通过键入调用此函数时!f "The thing
在 Discord 中,我得到了上面提到的命令。无论如何我可以解决这个问题吗?我认为这是不可能的,因为参数传递到函数的那一刻,错误就被抛出了。我想我可以编辑discord.py
解决这个问题,但它可能会破坏我的机器人的其他区域。当 iOS 用户在键盘上使用引号并输入类似内容时,我通常会收到此错误!f Josh's face
。有什么办法可以让所有引号都通过这个函数成功传递吗?
谢谢你!
它将命令之后的所有内容视为args
并将默认值设置为 None,这样如果没有 args,您就不会收到错误discord.ext.commands.errors.MissingRequiredArgument: args is a required argument that is missing.
我还更改了要使用的格式f string https://www.geeksforgeeks.org/formatted-string-literals-f-strings-python/我相信这更容易。并使用随机选择() https://docs.python.org/3/library/random.html#random.choice对你来说更好
文档中的示例 https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#keyword-only-arguments
@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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)