【发布时间】: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