简体   繁体   English

如何制作过滤脏话的 discord 机器人

[英]How To make a discord bot which filters swear words

This is my current code:这是我当前的代码:

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"))

I don't how I would make a swear filter.我不知道我会怎么做一个发誓过滤器。 Where can I learn more about the discord.py library?在哪里可以了解有关discord.py库的更多信息?

First of all, I suggest using commands.Bot() instead of discord.Client .首先,我建议使用commands.Bot()而不是discord.Client commands.Bot() is a subclass of Client so it has the same methods and attributes. commands.Bot()Client的子类,因此它具有相同的方法和属性。 With it, you can also set a command prefix instead of using .startswith all the time and much much more.有了它,您还可以设置命令前缀,而不是一直使用.startswith等等。 anyways...无论如何...

We are not here to write your code for you.我们不是来为您编写代码的。 Try looking it up or trying other solutions before asking a question.在提问之前尝试查找或尝试其他解决方案。 If you did show us what you tried, this gives us more information.如果您确实向我们展示了您的尝试,这将为我们提供更多信息。 Anyways, let's get back to writing your own code for you...无论如何,让我们回到为您编写自己的代码......

try this code:试试这个代码:

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...

In this code, we are creating a generator and passing it to any() which checks if any item in an iterable is True .在这段代码中,我们创建了一个生成器并将其传递给any() ,它检查迭代中的任何项目是否为True If it does return True then the message is deleted.如果它确实返回True ,则删除该消息。

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

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