How do I make my bot delete a message when it contains a certain word?

时光毁灭记忆、已成空白 提交于 2020-02-08 10:36:47

问题


Okay so I'm trying to make a filter for my bot, but one that isn't too complicated. I've got this:

@bot.event
async def on_message(ctx,message):
    if 'fuck' in Message.content.lower:
        Message.delete()

But it gives the error:

    Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 270, in _run_event        
    await coro(*args, **kwargs)
TypeError: on_message() missing 1 required positional argument: 'message'

Could you guys help me out?

Thanks!


回答1:


The on_message event handler only gets passed a single Message object, not a Context object.




回答2:


As you can see here, the delete function has to be awaited. What I've also noticed in your code is that you are capitalizing message when the argument to the function passed is lowercase.

@bot.event
async def on_message(message):
    if 'fuck' in message.content.lower:
        await message.delete()

This should work now.



来源:https://stackoverflow.com/questions/59809874/how-do-i-make-my-bot-delete-a-message-when-it-contains-a-certain-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!