简体   繁体   English

如何使用 on_message_edit 事件来阻止脏话? (discord.py)

[英]How can I use the on_message_edit event to block swear words? (discord.py)

I want my bot to delete the message if someone edits the message into a swear word message using the on_message_edit event, but I've been trying to figure this out and nothing has worked so far.如果有人使用on_message_edit事件将消息编辑为脏话消息,我希望我的机器人删除该消息,但我一直在尝试解决这个问题,但到目前为止没有任何效果。 Here is the code that doesn't work.这是不起作用的代码。

with open("badwords.txt") as file:
    blacklist = file.read().split('\n')

@client.event
async def on_message_edit(before, after, message):
    for word in blacklist:
        regex_match_true = re.compile(fr"[{symbols}]*".join(list(word)), re.IGNORECASE)
        regex_match_none = re.compile(fr"([{letters}]+{word})|({word}[{letters}]+)", re.IGNORECASE)
        if regex_match_true.search(message.content) and regex_match_none.search(message.content) is None:
            #embed here
            await message.delete()
            break

The on_message_edit event has no message argument. on_message_edit事件没有message参数。 There are arguments before and after , both of which are discord.Message object. beforeafter ,都是discord.Message 。消息object。 As far as I understand, after a message is edited, you are trying to delete it if it contains a blacklist word.据我了解,在编辑消息后,如果它包含黑名单单词,您会尝试将其删除。 You can get the content of the edited message using after.content .您可以使用after.content获取已编辑消息的内容。 You can then check if it contains the word blacklist.然后,您可以检查它是否包含单词 blacklist。

blacklist = open('badwords.txt', 'r').read().split('\n')

@client.event
async def on_message_edit(before, after):
    for word in blacklist:
        if word in after.content:
            await after.delete()
            return

This is a simple example of how you you can do this, but if you want to use regex , you can use it too.这是一个简单的示例,说明如何执行此操作,但是如果您想使用regex ,也可以使用它。

Reference参考

with open("badwords.txt") as file:
    blacklist = [line.strip() for line in file.readlines()]

@client.event
async def on_message_edit(before, after):
    for word in after.content.split():
        if word.lower() in blacklist:
            #embed here
            return await message.delete()

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

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