首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >discord.py: wait_for()方法的check参数阻止执行

discord.py: wait_for()方法的check参数阻止执行
EN

Stack Overflow用户
提问于 2019-09-20 09:30:17
回答 1查看 1.9K关注 0票数 0

上下文

我正在制作一个discord.py(重写-v1.0+) bot,我用一些基本的sql和python创建了一个cog来创建一个聊天机器人,但是当'message‘事件发生时,wait_for方法完全忽略它。

我是Discord.py的新手,所以任何帮助都是非常感谢的!

迷你代码:

代码语言:javascript
复制
"""ai cog"""
import discord
from discord.ext import commands
import asyncio

class ai(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(aliases=['c','ch','cha'])
    async def chat(self, ctx, *, message:str):
        Bot = 'Hello!'

        # I am guessing problem is here
        def nobotspeakshere(message):
                if message.author.bot == True:
                    return False

        while True:
            
            # try:
            msg = await user.wait_for('message', check=nobotspeakshere)
            #                                    ^^^^^^^^^^
            #                  The problem seems to be here.             
            # except asyncio.TimeoutError:
                # await ctx.send('Bot: bye')
                # break
            
            you = msg.content
            
            print(you)

            if you.lower() in ["", "bye!", "bye"]:
                return await ctx.send('Bot: ' + Bot)

            await ctx.send('Bot: Bye!')

            #some working sql
            #sql gives bot = "something else"

def setup(bot):
    bot.add_cog(ai(bot))

预期产出

打印变量“you”并继续执行

输出

忽略在通道中输入的消息。

期望问题

nobotspeakshere函数不像预期的那样起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-20 13:54:16

您的函数从不返回True,只返回False (如果作者是bot)或None (默认返回值)。由于None是一个虚假值,discord.py将其视为false。

最好的改变是总是返回与message.author.bot相反的内容

代码语言:javascript
复制
msg = await user.wait_for('message', check=lambda message: not message.author.bot)  
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58025616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档