编程语言
首页 > 编程语言> > 如何使用python discord bot cog读取所有已发送的消息? on_message似乎不起作用

如何使用python discord bot cog读取所有已发送的消息? on_message似乎不起作用

作者:互联网

我已经正确设置了齿轮(我知道,因为我有一个单独的齿轮来处理所有命令,所以on_message不会弄乱它们),但是on_message却什么也没做.

我已经尝试将其包含到另一个齿轮中,但是我仍然没有收到任何错误,只是不起作用.我也尝试过使用@ bot.event的不同形式,但是所有这些都会导致错误.最后,我知道该齿轮正在工作,因为主.py中的on_ready提醒我它已成功加载.

这是齿轮中的代码,应该读取所​​有消息(减去所有导入内容):

class autoresponse(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
    async def on_message(self, message):
        print(message.content)

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

这是加载它的代码


@bot.event
async def on_ready():
    print('bot is up')
    await bot.change_presence(status=discord.Status.online, activity=discord.Game("bl help"))
    for cog in [f.replace('.py', "") for f in listdir("cogs") if isfile(join("cogs", f))]:
        try:
            if not "__init__" in cog:
                bot.load_extension("cogs." + cog)
                print("Loaded cog")
        except Exception as e:
            print("Cog {} not loaded!".format(cog))
            traceback.print_exc()

希望该机器人只将所有消息打印到控制台,因为这样我就知道它的工作原理,并且可以继续执行我希望它执行的其他操作.

解决方法:

嵌齿轮中的事件侦听器需要装饰commands.Cog.listener

@commands.Cog.listener()
async def on_message(self, message):
    print(message.content)

新型嵌齿轮can be found here的文档

标签:discord,python,bots,discord-py
来源: https://codeday.me/bug/20191010/1884102.html