【问题标题】:Different message on no arguments provided, Discord + Python没有提供参数的不同消息,Discord + Python
【发布时间】:2018-07-24 02:14:37
【问题描述】:

我正在尝试为我的 Discord 机器人创建一个命令列表,并且一直在对整个考验进行原型设计。这就是我所拥有的,但问题是即使前两个有效,最后一个(应该在没有提供参数时触发)根本不会触发。

@bot.command(pass_context=True)

async def test(ctx, *msg):
    msg = ctx.message.content.split(" ", 1)
    if msg[1] == "yes":
        await bot.say("This is a valid argument!")
        # do thing...
    else:
        await bot.say("Not a valid argument!")
    if len(msg) == 0:
        await bot.say("Try using an argument. For example: !test yes")

【问题讨论】:

    标签: python bots discord discord.py


    【解决方案1】:

    不要覆盖msg,使用命令解析器生成的那个。先测试args的长度,再测试有效值。

    @bot.command(pass_context=True)
    async def test(ctx, *args):
        if not args:
            await bot.say("Try using an argument. For example: !test yes")
        elif args[0] == "yes":
            await bot.say("This is a valid argument!")
        else:
            await bot.say("Not a valid argument!")
    

    【讨论】:

      【解决方案2】:

      我认为您使用list 作为消息,因为msg[1] 使用数字作为索引。

      不确定其他类型,因为我对 Python 不太熟悉,但列表的长度是其中的项目数。

      test = ["test"]
      print(len(test))
      

      输出:1

      所以,当您执行if len(msg) == 0 时,您正在检查发送的消息中是否包含 0 个单词,所以是空的。请改用if len(msg) == 1

      【讨论】:

        猜你喜欢
        • 2017-02-11
        • 2020-11-23
        • 2021-09-05
        • 2021-06-20
        • 1970-01-01
        • 2023-03-08
        • 2015-07-24
        • 2021-03-27
        • 2013-05-03
        相关资源
        最近更新 更多