在 discord.py 中,如何制作脏话过滤器

时间:2021-02-04 14:21:06

标签: python discord.py

这就是我目前所拥有的 当我通过在列表中输入一个坏词来测试它时,没有任何反应。这是为什么,我可以用什么代码来修复它?

infractions = {}
limit = 5
blacklist = ['bad words go here']
@bot.event
async def on_message(message):
    content = message.content.lower()
    if any(word in content for word in blacklist):
        id = message.author.id
        infractions[id] = infractions.get(id, 0) + 1
        if infractions[id] >= limit:
            await bot.kick(message.author)
        else:
            warning = f"{message.author.mention} this is your {infractions[id]} warning"
            await bot.send_message(message.channel, warning)
    await bot.process_commands(message)

1 个答案:

答案 0 :(得分:0)

您的问题是您使用的是 discord.py 的旧版本/教程。 bot.send_message 之类的东西已经过时,bot.kick 之类的东西甚至都不存在。 理想的代码是™:

infractions = {}
limit = 5
blacklist = ['old', 'version', 'of', 'discord', '.', 'py']

@bot.event
async def on_message(message):
    content = message.content.replace(' ', '')

    if any(word in content.lower() for word in blacklist):
        id = message.author.id
        infractions[id] = infractions.get(id, 0) + 1

        if infractions[id] >= limit:
            await message.author.kick()
        else:
            warning = f"{message.author.mention} this is your {infractions[id]} warning"
            await message.channel.send(warning)
    await bot.process_commands(message)