现在,我有
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 blacklisted 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代码,但是它不起作用。我不知道为什么,因为我无法查看错误。目标是让Discord僵尸程序在包含bad-words.txt
中列入黑名单的单词或短语时删除一条消息,该文件每行包含一个列入黑名单的短语。该机器人可以运行,但不执行任何操作。谢谢您的帮助。
答案 0 :(得分:2)
由于您可能要等待搜索完成,因此可以将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)