如何禁止某些单词然后删除消息

时间:2020-02-06 21:21:01

标签: python python-3.x discord discord.py discord.py-rewrite

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

#ifndef _GAME_
#define _GAME_

class CGame
{
private:
    static CGame* s_instance;

public:
    static CGame* getInstance();

    void run(); // For testing purposes
    CGame();
};

#endif _GAME_

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

>

这是我当前的代码,其中包含列表

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

1 个答案:

答案 0 :(得分:2)

@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。我们可以直接删除该消息,并应通过将消息发送到其所在的频道来对此做出响应。