【问题标题】:"[some string] is a required argument that is missing" just won't go away\"[some string] is a required argument that is missing\" 只是不会消失
【发布时间】:2022-12-13 00:37:54
【问题描述】:

所以我只是一个业余爱好者,正在制作一个简单的 discord 机器人,这里有一个命令,如您所见,它是“添加”。问题是,当我运行它时它很好,但是当“mes”为空时,我们得到了好的 ol:

“mes 是缺少的必需参数。”。

编码

@bot.command()
async def add(ctx, *, mes):
    if not mes == '':
        await ctx.send('Added *' + mes + '* to the list')
        file1 = open('file1.txt', 'a')
        c = mes.lower()
        word = '\n' + c
        file1.writelines(word)
        file1.close()
    else:
        await ctx.send("Please enter a word to add")

我寻找有类似问题的人,但我所能找到的只是具有所需“mes”的代码,修复是 *mes,即 (ctx, *, mes)。我尝试了很多东西但无济于事而且我不知道这里出了什么问题所以我可以寻求帮助

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    发生这种情况的原因是因为参数是必需的。 要使其成为可选的,请将默认值设置为None

    @bot.command()
    async def add(ctx, *, mes: str = None):
        if not mes:
            await ctx.send('Added *' + mes + '* to the list')
            file1 = open('file1.txt', 'a')
            c = mes.lower()
            word = '
    ' + c
            file1.writelines(word)
            file1.close()
        else:
            await ctx.send("Please enter a word to add")
    

    【讨论】:

      猜你喜欢
      • 2017-05-01
      • 2021-05-02
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      • 2016-09-14
      • 2020-03-06
      • 1970-01-01
      • 2016-04-12
      相关资源
      最近更新 更多