【问题标题】:on_message Cog triggers 2 bot replieson_message Cog 触发 2 个机器人回复
【发布时间】:2021-09-14 08:03:54
【问题描述】:

我正在开发一个用于随机使用的 discord.py 机器人,我第一次尝试在 Cog 中制作我的 on_message 事件,我查看了不同的示例并尝试了这个:

import discord

from discord.ext import commands


class AntiSpam(commands.Cog):

    def __init__(self, client):
        self.client = client

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

        if len(message.content) > 350: # Anti-Spam for message lenght
            await message.delete()
            await message.channel.send(f'{message.author.mention}, <a:cat_no:843799913958866955> You are **not** allowed to send unnecessarily long and annoying messages in this server!', delete_after=5)

        await self.client.process_commands(message)

def setup(client):
    client.add_cog(AntiSpam(client))

现在,一切正常,直到我在添加该事件后尝试使用命令,它回复了 2 个答案,例如,我使用了一个简单的“hello”命令,它会回复 "{ctx.author.mention} World" 但是,它现在会回复该消息 2 次而不是 1 次,我见过有人问某人是否有 on_message 事件,但从未继续。 我还尝试关闭我正在运行的每个 python 文件,用client.logout() 重新启动我的电脑,但仍然是同样的错误,有什么帮助吗?

【问题讨论】:

  • 我觉得你可以删除 await self.client.process_commands(message) 并且它会起作用

标签: python discord discord.py


【解决方案1】:

您不在监听器中处理命令,仅在事件中,只需删除最后一行

    @commands.Cog.listener()
    async def on_message(self, message):
        if len(message.content) > 350: # Anti-Spam for message lenght
            await message.delete()
            await message.channel.send(f'{message.author.mention}, <a:cat_no:843799913958866955> You are **not** allowed to send unnecessarily long and annoying messages in this server!', delete_after=5)

您可以有多个侦听器(用commands.Cog.listener()bot.listen() 装饰),但只能有一个事件(用bot.event 装饰)

【讨论】:

    猜你喜欢
    • 2020-10-11
    • 2018-08-24
    • 2022-01-26
    • 1970-01-01
    • 2020-09-06
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多