首页 > 解决方案 > discord.py wait_for 没有读取消息

问题描述

我正在制作自己的不和谐机器人。一旦用户发送-find我希望机器人等待用户响应它,机器人就会发送一条消息。机器人等待,但它无法识别消息。

@commands.command()
async def find(self, ctx):
    members = [self.xxx, self.xxx, self.xxx]
    member = random.choice(members)
    image = member[0]
    await ctx.send(f'{image}')
    def check(m):
        m.author == ctx.author and m.channel == ctx.channel 
    try:
        await self.client.wait_for('message', timeout=8.0, check=check)
        await ctx.send('yay!')
    except: 
        await ctx.send('booo you suck')
        try:     @commands.command()
     async def find(self, ctx):
        members = [self.xxx, self.xxx, self.xxx]

        member = random.choice(members)
        image = member[0]

        await ctx.send(f'{image}')

        def check(m):
            m.author == ctx.author and m.channel == ctx.channel 

            await self.client.wait_for('message', timeout=8.0, check=check)
            await ctx.send('yay!')
        except: 
            await ctx.send('booo you suck')

没有错误。我删除了尝试,除了查看是否有任何错误,但唯一显示的错误是超时。

标签: discorddiscord.py

解决方案


那是因为您没有在 check 函数中返回任何内容,因此它永远不会评估为 True 因为默认情况下函数返回 None 可以用作 False

 @commands.command()
 async def find(self, ctx):
    members = [self.xxx, self.xxx, self.xxx]

    member = random.choice(members)
    image = member[0]

    await ctx.send(f'{image}')

    def check(m):
        return m.author.id == ctx.author.id and m.channel.id == ctx.channel.id

    try:
        await self.client.wait_for('message', timeout=8.0, check=check)
        await ctx.send('yay!')
    except: 
        await ctx.send('booo you suck')

推荐阅读