我正在尝试制作一个可以生成随机琐事问题的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的主要角色约翰·坦纳(John Tanner)在成为卧底警察之前曾从事哪些职业?一种。 (随机)正确或不正确的答案b。 (随机)正确或不正确的答案c。 (随机)正确或不正确的答案,然后还会出现大量正确和不正确的答案。
答案 0 :(得分: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