python - 在 discord.py 中将引号作为参数传递时出现错误
问题描述
当前,当有人在命令中使用引号时,我的 Discord 机器人出现错误,我收到以下错误:根据此discord.ext.commands.errors.ExpectedClosingQuoteError: Expected closing ".
错误报告以及. 这个问题有点烦人,我想知道目前是否有任何解决方法。到目前为止,这是我的代码:discord.py
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
. 有没有办法让所有的引号都通过这个函数成功传递?
谢谢!
解决方案
它会将命令之后的所有内容都设为 asargs
并将默认设置为 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)
推荐阅读
- python - 使用 Celery 进行长时间运行的异步作业
- performance - 如何隐藏要在生成的仪表板中报告的 JMeter 设置线程组采样器?
- survival-analysis - 使用“surv.rfsrc”作为学习器的 mlr3 包中的错误
- reactjs - 开玩笑运行导入树中的所有文件
- c++ - munmap_chunk():在 C++ 中创建数组数据结构时出现无效指针错误
- python - AttributeError 模块 'discord.client' 没有属性 'command'
- flutter - Flutter:如何知道 Flutter 插件的大小
- javascript - 处理对象的异步更新
- node.js - 正文中的 Node.js 数据未保存
- function - 已知方差的高斯混合模型