【问题标题】:Using multiple values for one parameter in a command in discord.py在 discord.py 的命令中为一个参数使用多个值
【发布时间】:2021-07-11 16:04:43
【问题描述】:

我目前正在尝试编写一个不和谐的测验机器人,现在我想找到一种方法让我的机器人从特定参数中提取随机问题。我不太确定这是正确的表达方式,但这是我的代码:

@client.command()
async def ask1(ctx , q):
    quiz_data = {
        'question1' : (["1" , "one"] , "one") , 
        'question2' : (["2" , "two"] , "two") , 
    }

    questionp1 = random.choice(list(quiz_data.keys()))
    answersp1 , hintp1 = quiz_data[questionp1]

    quiz_data1 = {
        'question3' : (["3" , "three"] , "three") , 
        'question4' : (["4" , "four"] , "four")
    }

    questionp2 = random.choice(list(quiz_data1.keys()))
    answersp2 , hintp2 = quiz_data1[questionp2]

    global var
    if var == 0:
        var = 1
        if q == "1":
            await ctx.send("What is the answer to this question?")
            await asyncio.sleep(1)
            await ctx.send(questionp1)

            def check_sender(msg):
                return msg.channel == ctx.channel
            def check_answer(msg):
                return any(answer in msg.content.lower() for answer in answersp1)

            try:
                async with timeout(10):
                    while var == 1:
                        msg = await client.wait_for('message', check=check_sender)
                        if check_answer(msg):
                            await ctx.send("Well done.")
                            var = 0                     
                            break
                        else:
                            print(msg.content)
            except asyncio.TimeoutError:
                if var != 1:
                    var = 0
                else:
                    await ctx.send(f"What is the answer to this question? hint: {hintp1}")
                    await ctx.send(questionp1)

                    try:
                        async with timeout(5):
                            while var == 1:
                                msg = await client.wait_for('message', check=check_sender)
                                if check_answer(msg):
                                    await ctx.send("Well done.")
                                    var = 0
                                    break
                                else:
                                    print(msg.content)
                    except asyncio.TimeoutError:
                        await ctx.send("No one got it right.")
                        var = 0
        elif q == "2":
            await ctx.send("What is the answer to this question?")
            await asyncio.sleep(1)
            await ctx.send(questionp2)

            def check_sender(msg):
                return msg.channel == ctx.channel
            def check_answer(msg):
                return any(answer in msg.content.lower() for answer in answersp2)

            try:
                async with timeout(10):
                    while var == 1:
                        msg = await client.wait_for('message', check=check_sender)
                        if check_answer(msg):
                            await ctx.send("Well done.")
                            var = 0                     
                            break
                        else:
                            print(msg.content)
            except asyncio.TimeoutError:
                if var != 1:
                    var = 0
                else:
                    await ctx.send(f"What is the answer to this question? hint: {hintp2}")
                    await ctx.send(questionp2)

                    try:
                        async with timeout(5):
                            while var == 1:
                                msg = await client.wait_for('message', check=check_sender)
                                if check_answer(msg):
                                    await ctx.send("Well done.")
                                    var = 0
                                    break
                                else:
                                    print(msg.content)
                    except asyncio.TimeoutError:
                        await ctx.send("No one got it right.")
                        var = 0
    else:
        await ctx.send("A round has already begun.")

这是我目前使用的概念。命令$ask1 1$ask1 2 工作正常,但我希望用户可以选择输入$ask1 q:1,2 或类似的东西,然后机器人从第一个测验数据集或第二个中提取一个问题。请注意,整个代码运行良好,并且执行时没有任何错误。几天来我一直在绞尽脑汁,我认为要做到这一点,我应该使用关键字 args 而不是位置 args,但我不完全确定也不知道如何在这种情况下正确应用 kwargs 的概念. vars 对于不同的命令(顺便说一句,停止命令)是必需的,因此您不必注意它。

抱歉,如果这很难理解,我愿意回答任何问题以使这一点更清楚。任何答案将不胜感激,谢谢!

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    你可以只使用随机库,这里是文档https://docs.python.org/3/library/random.html。我的想法是,如果随机数为 1,您首先可以生成 1 和 2 之间的随机数,如果 2 到 quiz data2,您将转到测验数据,然后在 1 和 to 之间生成另一​​个随机数,这将表明来自这个的哪个问题将使用测验数据

    【讨论】:

    • 感谢@SimonTheAnt,但我不确定我们是否在同一页面上。我想我明白你在说什么,但我不明白用户将如何使用 $ask1 q:1,2 或类似的东西,因为你的答案只涉及数据中随机选择的部分。我不确定我是否是错误的人,但我认为我已经在我的代码中得到了这个概念。谢谢你的回答,虽然我很感激~
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 2020-10-10
    • 2020-10-09
    • 1970-01-01
    • 2019-08-26
    • 2021-04-08
    相关资源
    最近更新 更多