我该如何使Discord机器人检测到某些单词,然后将其删除?

时间:2020-06-02 18:27:13

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

因此,我一直在尝试通过制作Discord机器人来学习Python,直到我遇到此错误AttributeError: 'Bot' object has no attribute 'delete'为止,一切都进行得很好。我打算做一个功能,如果它从字典中检测到某些单词,它将被删除。我读过Benjamin Soyka's question,询问如何做,但使用的是Discord.py的旧版本

现在,我的代码是:

with open("bad_words.txt") as file: # bad-words.txt contains one blacklisted phrase per line
    bad_words = [bad_word.strip().lower() for bad_word in file.readlines()]


@client.event    
async def on_message(message):
    print(message.content) #prints messages in console
    for bad_word in bad_words:
        if bad_word in message.content:
            print("bad words detected") #prints when bad word is found
            await client.delete(message) #delete said message
    await client.process_commands(message)

我尝试了各种方法,例如制作类或嵌套函数,但似乎没有一种方法适合我。抱歉,这个问题看起来很傻,谢谢您的帮助!

1 个答案:

答案 0 :(得分:1)

使用(重写版本)

await message.delete()

代替

await client.delete(message)