首页 > 解决方案 > 如何禁止某些单词然后删除消息

问题描述

我的目标是检查用户在不和谐频道中输入的内容是否包含我在列表中的某个单词或短语,如下所示:

illegal_words = ["apple", "pear", "banana"]

预期的结果是当我输入的内容包含以下一个或多个单词时,它会看到该单词,然后删除,然后发送一条消息,指出您不允许键入该特定消息。

这是我当前的代码,包括列表

client = commands.Bot(command_prefix="..")

illegal_words = ["apple", "pear", "banana"]

@client.event
async def on_message(ctx):
    [word in message.content for word in illegal_words]
    any([word in message.content for word in illegal_words])
    await ctx.channel.purge(limit=1)
    await ctx.send("""That Word Is Not Allowed To Be Used!
    Continued Use Of Mentioned Word Would Lead To Punishment!""")

标签: pythonpython-3.xdiscorddiscord.pydiscord.py-rewrite

解决方案


@client.event
async def on_message(message):
    if any(word in message.content for word in illegal_words):
        await message.delete()
        await message.channel.send("""That Word Is Not Allowed To Be Used! Continued Use Of Mentioned Word Would Lead To Punishment!""")
    else:
        await client.process_commands(message)

on_message接收一个Message对象,而不是一个Context. 我们可以直接删除消息,并且应该通过向它所在的频道发送消息来响应它。


推荐阅读