我想知道如何正确使用 wait_for 命令。任何人都可以帮助我吗?它的不和谐.py

时间:2021-07-19 09:38:57

标签: python discord.py command

我试图让机器人在我回答问题时说“正确”或“不正确”。但是,机器人不会说正确或不正确。

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:```

1 个答案:

答案 0 :(得分:0)

要等待消息,请使用 self.client.wait_for('message')

这里也是文档中的一个示例:Bot.wait_for

{question[0]} 也没什么意义,因为这样总是只回答第一个问题。 要创建混合,您应该从 questionrandom.choice(question) 中选择一个问题。这里的困难部分是等待正确答案/选择它,因为您使用了两个列表,需要一些代码。

为了一一询问所有问题,我们使用 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