【发布时间】:2026-01-11 10:45:01
【问题描述】:
我正在尝试制作一个可以生成随机琐事问题的 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的主角约翰坦纳在成为卧底警察之前从事过什么职业?一个。 (随机)一个正确或不正确的答案 b. (随机)一个正确或不正确的答案 c. (随机)一个正确或不正确的答案,这将继续正确和错误答案的数量。
【问题讨论】:
-
您在解析问题数据方面做了哪些尝试?
-
现在机器人说:琐事时间! 。我这样做的方法是通过 question[0] 获取第一个属性(问题名称)。我尝试为答案做问题[1],但它只是说没有属性 1。我希望机器人说:琐事时间! 。为了随机化它,一旦我能得到问题的值,我可能会将答案放在一个列表中并随机播放。
-
您在
question变量中只获得了一个问题对象,因为您在question = client.get_questions(1, category=15)中仅指定了1。您会在问题的最后一段中看到可用的属性。当然,您可以输入一些打印语句来查看带有print(f'Attributes are {vars(question[0])}')之类的属性。然后,您使用print(f'question is {question[0].question}'或print(fanswers are {question[0].answers}` 等内容进行验证。你只需要做一些挖掘。
标签: python discord.py