繁体   English   中英

on_message 函数重复,Discord.py 重写

[英]on_message function repeating, 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

您正在遍历消息的每个单词,并为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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM