DISCORD:使用python脚本删除包含黑名单单词的Discord消息

时间:2020-10-24 10:35:41

标签: python discord.py-rewrite

我正在编写一个脚本来删除包含黑名单单词的Discord消息,但是无法正常工作。我成功编写了ping命令,但没有删除消息,并且控制台中未显示任何错误:

# ----- Imports -----
import discord
from discord.ext import commands

# ----- Variables -----
client = commands.Bot(command_prefix = 't.')

blacklist = [
"nigger",
"nigga",
"slut",
"whore",
"cum",
"pussy",
"dick",
"penis",
"vagina",
"cock",
"faggot",
"nig",
"fag"
"test"]


# ----- Functions -----

# Function that runs when bot is logged in and ready
@client.event
async def on_ready():
    print('Bot is ready.')
    activity = discord.Game(name="t.ping")
    await client.change_presence(status=discord.Status.online, activity=activity)

# Ping command
@client.command()
async def ping(ctx):
    await ctx.send(f'Pong! {round(client.latency) * 1000}ms')

# function for deleting messages
@client.event
async def on_message(message):

    for word in blacklist:
        if word in message.content:
            await message.delete(message)
            await message.channel.send("Test")

# Token
client.run('TOKEN_STRING')

在真实应用程序中,“ TOKEN_STRING”被替换为身份验证令牌。

我不确定如何在邮件内容中测试单词是否存在。我尝试过的事情是:

  • if word in message.content:
  • word in message.content
  • message.content == word
  • ...

但到目前为止,没有任何方法可以解决问题。有什么解决办法的想法吗?

1 个答案:

答案 0 :(得分:0)

为简单起见,我确保缩短了黑名单中的单词列表,以便于阅读。如果您在任何时候需要进一步的说明,请告诉我,但是下面的代码应该非常简单。

blacklist = [
  "test",
  "panic",
  "five"
]

@client.event
async def on_message(message):
  if any(word in message.content for word in blacklist):
    await message.delete()
    await message.channel.send("That word is not allowed :(")