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

问题描述 投票:0回答:1

我正确设置了cog(我知道,因为我有一个单独的cog来处理所有命令,因此on_message不会弄乱它们),但on_message只是没有做任何事情。

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

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

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()

希望机器人应该只将所有消息打印到控制台,因为那时我会知道它的工作,并可以继续前进我想要它做的其他事情。

python bots discord discord.py
1个回答
1
投票

齿轮中的事件监听器需要用commands.Cog.listener进行装饰

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

新式齿轮can be found here的文档

© www.soinside.com 2019 - 2024. All rights reserved.