有没有办法用opentdbpy模块直接获取问题、正确答案以及错误答案?

问题描述 投票:0回答:1

我想做一个可以随机生成小问题的python机器人。我正在使用opentdbpy模块来生成一个与电子游戏相关的随机小问题。

我如何才能只得到问题的答案、错误答案和问题值呢?

@bot.command(name='trivia', help='Gives you a random trivia question, if you answer right, you get rb!')
@commands.cooldown(1, 60, commands.BucketType.user)
async def trivia(ctx):
    question = client.get_questions(1, category=15)
    categories = client.get_categories()


    await ctx.send(f'{question}') #if it helps, right now this just says something like: [Question(data={"category": 15, "type": "multiple", "difficulty": "hard", "question": "Which occupation did John Tanner, the main protagonist for Driver and Driver 2, had before turning into an undercover cop?", "correct_answer": "Racing Driver", "incorrect_answers": ['Taxi Driver', 'Delivery Driver', 'Getaway Driver']})], instead i just want it to say name + choices
# this sends the question

    def check(m):
        return m.author.id == ctx.author.id
# this checks if the user who initiated the cmd is the one who talks

    time3 = await bot.wait_for('message', check=check)
# this is waiting for a message to be sent

    if time3.content == '1996': #In here i want to have the correct answer from the questions variable
        if str(ctx.message.author.id) in amounts:
           await ctx.send('Correct! You earned 50rb!')
           amounts[str(ctx.message.author.id)] += 50
           amounts.dump()
        else:
            await ctx.send("Correct! You would've just got some rb, but you aren't registered! Type r!register to start!")

    elif time3.content == '1981': #Here i want to have the list of incorrect answers, and it's any value inside the incorrect answers.
        await ctx.send('F you got it incorrect. The correct answer was 1996')
    else:
        await ctx.send('thats not a valid answer come on man')

我想让机器人说:[Question(data={"category": 15, "type": "multiple", "difficulty": "hard", "question": "Which occupation did John Tanner, the main protagonist for Driver and Driver 2, had before turning into an undercover cop?", "correct_answer": "Racing Driver", "incorrect_answers": ['Taxi Driver', 'Delivery Driver', 'Getaway Driver']})]我想让机器人说:"小游戏时间到了!司机和司机2》的主角约翰-坦纳在变成卧底警察之前从事的是哪种职业? a.要么(随机)是正确答案,要么是错误答案b.要么(随机)是正确答案,要么是错误答案c.要么(随机)是正确答案,要么(随机)是错误答案,这样就会继续进行正确和错误答案的数量。

python discord.py
1个回答
0
投票

这里有一个opentb的实现 该模块


@commands.command()
async def trivia(self, ctx):
    q = get("https://opentdb.com/api.php?amount=1").content.decode("utf-8")
    q = l(q)['results'][0]
    Tr = Embed()
    Tr.title = q["question"].replace(""", "'").replace("'", "'")
    opt = q["incorrect_answers"]
    opt.append(q["correct_answer"])
    s(opt)

    if q["type"] == "multiple":
        Tr.description = f"```A) {opt[0]}\nB) {opt[1]}\nC) {opt[2]}\nD) {opt[3]}```"
        mcq = ["\U0001F1E6", "\U0001F1E7", "\U0001F1E8", "\U0001F1E9"]
        o2e = dict(zip(opt, mcq))

    if q["type"] == "boolean":
        Tr.description = f"```1) True\n2) False```"
         mcq = ["\U0001F1F9", "\U0001F1EB"]
         o2e = {"True": mcq[0], "False": mcq[1]}

    mess = await ctx.send(embed=Tr)
    for emoji in mcq:
        await mess.add_reaction(emoji)
        ans = {"R": {*()}, "W": {*()}}
        while True:
            try:
                reaction, user = await self.bot.wait_for('reaction_add', timeout=10.0)
                em = str(reaction.emoji)
            except TimeoutError:
                await ctx.send(content="```Time's up ⌚!```")
                break
            if em == o2e[q["correct_answer"]] and user != self.bot.user:
                ans["R"].add(user.id)
            else:
                ans["W"].add(user.id)
     await ctx.send(content=f"```Correct answer was {q['correct_answer']}```")
     for right in ans["R"]:
         if right not in ans["W"]:
             await ctx.send(f"<@{right}> won 10 🥥")

get 方法是来自 requests 是已知的阻塞,所以建议使用 aiohttp 而非 discord.py

© www.soinside.com 2019 - 2024. All rights reserved.