这是我当前的代码:
import discord
import os
from keep_alive import keep_alive
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!something'):
await message.channel.send('SOMETHING')
keep_alive()
client.run(os.getenv("TOKEN"))
我不知道怎么做一个脏话过滤器。我在哪里可以了解更多关于discord.py
库的信息?
首先,我建议用commands.Bot()
代替discord.Client
。commands.Bot()
是Client
的子类,所以它具有相同的方法和属性。有了它,您还可以设置命令前缀,而不是一直使用.startswith
等等。不管怎样…
我们不是来为你写代码的。在提出问题之前,试着查一下或尝试其他的解决方案。如果你确实向我们展示了你所尝试的,这就给了我们更多的信息。无论如何,让我们回到为您编写自己的代码…
试试这个代码:
badWords = ['poopoo', 'peepee', 'check', 'today is my birthday', "i don't like undertale's music", 'you are not cool >:(']
@bot.event
async def on_message(message):
messageContent = message.content
if any(word.lower() in messageContent.lower().replace(' ', '') for word in badWords):
await message.delete()
#other stuff...
在这段代码中,我们创建了一个生成器并将其传递给any()
,该生成器检查可迭代对象中的任何项是否为True
。如果返回True
,则删除该消息。