on_message函数重复,Discord.py重写

时间:2019-04-02 08:32:07

标签: python discord discord.py-rewrite

我正在制作一个机器人,当它检测到您正在使用被禁止的单词时,它将删除您的消息。很简单,但是,当我这样做时。 on_message函数正在重复自身。我不知道为什么,但我希望你能回答我的问题

@client.event
async def on_message(msg):
    contents = msg.content.split(" ")
    for word in contents:
        if word.lower() in chat_filter: #ChatFilter is a list of words that cannot be used
            try:
                await msg.delete()
                await msg.channel.send("**YOU ARE NOT ALLOWED TO USE THIS WORD!!!**")
            except discord.errors.NotFound:
                return

1 个答案:

答案 0 :(得分:1)

您正在遍历消息的每个单词,并为同样在chat_filter中的每个单词发送响应。相反,如果任何单词都在禁止列表中,请发送一条消息:

@client.event
async def on_message(msg):
    contents = msg.content.split(" ")
    if any(word in chat_filter for word in contents):
        try:
            await msg.delete()
            await msg.channel.send("**YOU ARE NOT ALLOWED TO USE THIS WORD!!!**")
        except discord.errors.NotFound:
            return