【问题标题】:How to make bot send multiple reply for 1 commmand如何让机器人为 1 个命令发送多个回复
【发布时间】:2018-06-22 10:02:11
【问题描述】:

如何让机器人发送多个命令,用一个命令回复。例如标签。

browsers = ['chrome', 'mozilla', 'ie', 'safari']

@bot.command(pass_context=True)
async def chrome(ctx):
    msg = "about Chrome. {0.author.mention}".format(ctx.message)
    await bot.say(msg)

@bot.command(pass_context=True)
async def mozilla(ctx):
    msg = "about Mozilla. {0.author.mention}".format(ctx.message)
    await bot.say(msg)

@bot.command(pass_context=True)
async def safari(ctx):
    msg = "about Safari. {0.author.mention}".format(ctx.message)
    await bot.say(msg)

所以最后如果用户键入?browsers 命令,它应该发送browsers 列表中的所有命令回复。如果键入 ?chrome 或 ?mozilla 或 ?safari,它也应该单独工作

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    这是我能找到的唯一方法,但有点尴尬。

    @bot.command(pass_context=True)
    async def chrome(ctx):
        msg = "about Chrome. {0.author.mention}".format(ctx.message)
        await bot.say(msg)
    
    @bot.command(pass_context=True)
    async def mozilla(ctx):
        msg = "about Mozilla. {0.author.mention}".format(ctx.message)
        await bot.say(msg)
    
    @bot.command(pass_context=True)
    async def safari(ctx):
        msg = "about Safari. {0.author.mention}".format(ctx.message)
        await bot.say(msg)
    
    @bot.group(pass_context=True)
    async def browsers(ctx):
        if ctx.invoked_subcommand is None:
            for command in browsers.walk_commands():
                await command.invoke(ctx)
    
    browsers.add_command(chrome)
    browsers.add_command(mozilla)
    browsers.add_command(safari)
    

    我非常怀疑有一种更简洁的方法可以做到这一点,您可以使用@browsers.command() 定义命令,然后还可以直接调用它们,但我找不到它。

    【讨论】:

    • 运行机器人时出错。 in <module> browsers.add(chrome) AttributeError: 'Group' object has no attribute 'add'
    • @Demotry 其实是add_command,我只是忘记了正确的名字。
    • 是的,它工作完美。但是browsers.add_command(firefox) 这应该是browsers.add_command(mozilla) 谢谢。
    猜你喜欢
    • 2019-06-29
    • 1970-01-01
    • 2016-08-05
    • 2018-05-02
    • 2021-05-11
    • 1970-01-01
    • 2021-07-28
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多