【问题标题】:Discord py Cog problem with on_message event, don't work与 on_message 事件有关的 Discord py Cog 问题,不起作用
【发布时间】:2021-03-11 17:07:49
【问题描述】:

最近我的 bot 正在成长,我花时间重写代码以使其与 Discord Py 的 cogs 系统一起工作

我已经正确调整了所有代码,但是我停止工作的所有on_message 事件都没有抛出任何类型的错误消息。

模块加载正确,没有语法错误,所以我不明白会发生什么。

我的一些代码:

import discord
import random
import datetime
from discord.ext import commands

class eastereggs(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self._last_member = None

    @commands.Cog.listener()
    async def on_message(self,message):
        Cheers= ["Hi", "hi", "Hello", "hello"]
        if message.content in Cheers:
            await message.channel.send('Hello again')
            await self.bot.process_commands(message)

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

但是,它不会对数组中的任何问候做出反应

我编辑:我有多个带有数组的 on_message 事件

但似乎只有一个有效

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    问题在于你不能有两个同名的函数。如果你这样做,它只会调用最后一个。该文件将加载而不会出现任何错误。因为所有都是on_message 事件,所以只有最后一个会起作用。但是,您可以告诉听众“听”什么。

    您可以使用@Cog.listener("on_message")(或以相同方式的其他事件),然后为您的函数调用不同的名称。

        @Cog.listener("on_message")
        async def greet(self,message):
            Cheers= ["Hi", "hi", "Hello", "hello"]
            if message.content in Cheers:
                await message.channel.send('Hello again')
                await self.client.process_commands(message)
    
        @Cog.listener("on_message")
        async def agree(self,message):
            Agree = ["yes", "yep", "ok"]
            if message.content in Agree:
                await message.channel.send('good')
                await self.client.process_commands(message)
    
    
        @Cog.listener("on_message")
        async def dAgree(self,message):
            dAgree= ["no", "nope"]
            if message.content in dAgree:
                await message.channel.send('why')
                await self.client.process_commands(message)
    
    

    【讨论】:

    • 我知道它已经一年多了,但是如果我的 discord_main.py 中有一个 on_message 并且 COG 中有另一个 on_message ,这仍然适用吗?
    • @AryanGarg 你可以有一个on_message 主,每个COG 有一个,它会正常工作。但我仍然建议使用 `@Cog.listener("on_message") ,因为您可以将函数命名为更具描述性的名称。以后更容易回忆
    【解决方案2】:

    你需要取消缩进process_commands

    @commands.Cog.listener()
    async def on_message(self, message):
        # some code
    
        await self.bot.process_commands(message)
    

    在您遇到的所有其他on_message 事件中, 另外,如果你想让你的代码更简洁、更健壮,可以使用 bot.dispatch 来创建自定义事件,遗憾的是没有关于它的文档

    @commands.Cog.listener()
    async def on_message(self, message):
        if message.content in ['some', 'values']:
                              # name of the event, args
            self.bot.dispatch('custom_event', message)
    
        await self.bot.process_commands(message)
    
    
    @commands.Cog.listener()
    async def on_custom_event(self, message):
        # custom event
    

    【讨论】:

    • 非常感谢,我正在努力解决它
    • 如果有帮助记得采纳答案
    猜你喜欢
    • 2021-04-21
    • 2019-09-14
    • 2022-01-04
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 2022-10-02
    • 2021-02-22
    • 2021-05-04
    相关资源
    最近更新 更多