我想知道如何正确使用 wait_for 命令。任何人都可以帮助我吗?它的 discord.py
I'm wondering how to use the wait_for command correctly. Anyone can help me out? its discord.py
我正在尝试让机器人在我回答问题时说正确或不正确。但是,机器人不会说正确或不正确。
import discord
from discord.ext import commands
class Trivia(commands.Cog):
def __init__(self, client):
self.client = client```
@commands.command()
async def Trivia(self, ctx):
question = ['Who invented The Theory of Special Relavity?',
'Who created Algebra?',
"What is Newton's third law? **use 'and' instead of '&'**",
'Who discover prism?']
responses = ['Albert Einstein',
'al-Khwarizmi',
'Action and Reaction',
'Isaac Newton']
await ctx.send('**Welcome to this Trivia! There will be 5 questions that you have to answer.**')
await ctx.send(f'Question: {question[0]}')
async def on_message(self, message):
if self.message.content.wait_for('Albert Einstein'):
await self.channel.send('Correct!')
else:
self.channel.send('Incorrect!')
def setup(client):
client.add_cog(Trivia(client))
修改后:
await ctx.send(f'Question: {question[0]}')
await ctx.wait_for('Albert Einstein')
await self.channel.send('Correct!')
def setup(client):
client.add_cog(Trivia(client))```
and then it shows this error : ```AttributeError: 'Context' object has no attribute 'wait_for'
The above exception was the direct cause of the following exception:```
要等待您使用 self.client.wait_for('message')
的消息。
这也是来自文档的示例:Bot.wait_for
{question[0]}
也没有什么意义,因为那样总是只回答第一个问题。
要创建混合,您应该 select 来自 question
和 random.choice(question)
的问题。这里困难的部分是等待正确的 answer/choose 它因为你使用两个列表,需要一些代码。
要一一提出所有问题,我们使用 for i in question
。
我为您创建了一个小示例,也许它会对您有所帮助:
@commands.command()
async def trivia(self, ctx):
def check(m): # Checks that the author of the command gives the answer
return m.author == ctx.author and m.channel == ctx.channel
await ctx.send('**Welcome to this Trivia! There will be 5 questions that you have to answer.**')
question = ['Who invented The Theory of Special Relavity?',
'Who created Algebra?',
"What is Newton's third law? **use 'and' instead of '&'**",
'Who discover prism?']
responses = ['Albert Einstein',
'al-Khwarizmi',
'Action and Reaction',
'Isaac Newton']
for i in question:
await ctx.send(i) # Send the question
try:
await self.client.wait_for('message', check=check) # Wait for the message
# Your event that checks if the answer is correct
except:
return
我正在尝试让机器人在我回答问题时说正确或不正确。但是,机器人不会说正确或不正确。
import discord
from discord.ext import commands
class Trivia(commands.Cog):
def __init__(self, client):
self.client = client```
@commands.command()
async def Trivia(self, ctx):
question = ['Who invented The Theory of Special Relavity?',
'Who created Algebra?',
"What is Newton's third law? **use 'and' instead of '&'**",
'Who discover prism?']
responses = ['Albert Einstein',
'al-Khwarizmi',
'Action and Reaction',
'Isaac Newton']
await ctx.send('**Welcome to this Trivia! There will be 5 questions that you have to answer.**')
await ctx.send(f'Question: {question[0]}')
async def on_message(self, message):
if self.message.content.wait_for('Albert Einstein'):
await self.channel.send('Correct!')
else:
self.channel.send('Incorrect!')
def setup(client):
client.add_cog(Trivia(client))
修改后:
await ctx.send(f'Question: {question[0]}')
await ctx.wait_for('Albert Einstein')
await self.channel.send('Correct!')
def setup(client):
client.add_cog(Trivia(client))```
and then it shows this error : ```AttributeError: 'Context' object has no attribute 'wait_for'
The above exception was the direct cause of the following exception:```
要等待您使用 self.client.wait_for('message')
的消息。
这也是来自文档的示例:Bot.wait_for
{question[0]}
也没有什么意义,因为那样总是只回答第一个问题。
要创建混合,您应该 select 来自 question
和 random.choice(question)
的问题。这里困难的部分是等待正确的 answer/choose 它因为你使用两个列表,需要一些代码。
要一一提出所有问题,我们使用 for i in question
。
我为您创建了一个小示例,也许它会对您有所帮助:
@commands.command()
async def trivia(self, ctx):
def check(m): # Checks that the author of the command gives the answer
return m.author == ctx.author and m.channel == ctx.channel
await ctx.send('**Welcome to this Trivia! There will be 5 questions that you have to answer.**')
question = ['Who invented The Theory of Special Relavity?',
'Who created Algebra?',
"What is Newton's third law? **use 'and' instead of '&'**",
'Who discover prism?']
responses = ['Albert Einstein',
'al-Khwarizmi',
'Action and Reaction',
'Isaac Newton']
for i in question:
await ctx.send(i) # Send the question
try:
await self.client.wait_for('message', check=check) # Wait for the message
# Your event that checks if the answer is correct
except:
return