【问题标题】:How to use discord.py to censor bad words如何使用 discord.py 审查坏词
【发布时间】:2019-04-26 21:42:32
【问题描述】:

现在,我有以下内容:

import discord
from discord.ext import commands
from discord.ext.commands import Bot
import os
bot = Bot(command_prefix="?")
with open("bad-words.txt") as file: # bad-words.txt contains one phrase per line
    bad_words = [bad_word.strip().lower() for bad_word in file.readlines()]
@bot.event
async def on_message(message):
    message_content = message.content.strip().lower()
    async for bad_word in bad_words:
        if bad_word in message:
            await bot.send_message(message.channel, "{}, your message has been censored.".format(message.author.mention))
            await bot.delete_message(message)
bot.run(os.getenv("TOKEN"))

我有这个 Python 代码,但它不起作用。我不知道为什么,因为我看不到当前托管情况的错误。

目标是让机器人删除包含bad-words.txt 中列入黑名单的短语的邮件,其中每行包含一个短语。

机器人运行,但不做任何事情。我在这里想念什么?谢谢!

【问题讨论】:

  • async for 应该是普通的for。为什么看不到错误?

标签: python discord.py


【解决方案1】:

由于您可能希望等待搜索完成,您可以将 async for 更改为常规的 for,正如 Patrick Haugh 所说:

for bad_word in bad_words:
    if bad_word in message:
        await bot.send_message(message.channel, "{}, your message has been censored.".format(message.author.mention))
        await bot.delete_message(message)

不过,在 Python 3 中,any() 方法可用于进一步简化:

if any(bad_word in message for bad_word in bad_words):
    await bot.send_message(message.channel, "{}, your message has been censored.".format(message.author.mention))
    await bot.delete_message(message)

【讨论】:

    猜你喜欢
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多