discord.py-rewrite 尝试将某些术语列入黑名单仅在其唯一列入黑名单的术语时有效

discord.py-rewrite Trying to blacklist certain terms only works when its only the blacklisted term

我的目标和一些背景信息

你好,大约一天前,我试图将某些术语列入黑名单,这样如果用户说了一些不恰当的话,那么它就不会继续执行命令(我的 Whosebug 问题可以找到 ) And I tried mixing it with blacklisting certain people, (My Whosebug question can be found )但是黑名单这些词只有在它只是列入黑名单的术语时才有效。可以找到代码命令

我的命令代码

@bot.command(pass_context=True)
async def order(ctx, *, orderItem):
    with open('blacklist.json', 'r') as file:
            blacklist = loads(file.read())
    with open('user_blacklist.json', 'r') as file:
        user_blacklist = loads(file.read())
    if ctx.author.id in user_blacklist:
        await ctx.send("You are blacklisted from ordering from Discord Chinese")
        return
    else:
        print(orderItem.lower() in blacklist)
        print(orderItem.lower())
        print(blacklist)
        if orderItem.lower() in blacklist:
            await ctx.send("Due to your order containing one of the blacklisted terms, your order will not be placed.")
        else:
            channel = bot.get_channel(724051586728460290)
            link = await ctx.channel.create_invite(max_age = 300)
            global baseNumberID
            baseNumberID += 1
            global orderIDdf
            global df
            df[str(baseNumberID)] = ctx.author.name
            embed=discord.Embed(title="New order", color=0xfc57ff)
            embed.add_field(name="Who and Where", value="{} in {} in the {} channel".format(ctx.message.author, ctx.message.guild.name, ctx.message.channel.mention), inline=False)
            embed.add_field(name="What", value="{}".format(orderItem), inline=False)
            embed.add_field(name="Invite", value="{}".format(link), inline=False)
            embed.add_field(name="Order ID", value="Order ID is {}".format(baseNumberID), inline=False)
            embed.add_field(name="Time", value="{} GM time".format(strftime("%Y-%m-%d %H:%M:%S", gmtime())), inline=True)
            embed.set_footer(text="End of this Order")
            #Second embed that will be used later.
            deliverIDInfo = str(baseNumberID)
            deliverInfoEmbed=discord.Embed(title="Order Info")
            deliverInfoEmbed.add_field(name="Who and Where", value="{} in {} in the {} channel".format(ctx.message.author, ctx.message.guild.name, ctx.message.channel.mention), inline=False)
            deliverInfoEmbed.add_field(name="What", value="{}".format(orderItem), inline=False)
            deliverInfoEmbed.add_field(name="Invite", value="{}".format(link), inline=False)
            deliverInfoEmbed.add_field(name="Order ID", value="Order ID is {}".format(baseNumberID), inline=False)
            deliverInfoEmbed.add_field(name="Time", value="{} GMT time".format(strftime("%Y-%m-%d %H:%M:%S", gmtime())), inline=True)
            deliverInfoEmbed.set_footer(text="End of this Order")
            orderIDdf[deliverIDInfo] = deliverInfoEmbed
            await ctx.author.send("Your order has been placed!")
            await ctx.author.send(embed=embed)
            await channel.send(embed=embed)

我的问题

用户黑名单正常工作,但关键字没有。所以说黑名单术语是 Ubisoft 那么如果 args 是 Ubisoft/ubisoft 它就可以工作但是如果它的 Words words Ubisoft 那么它就不起作用,它允许订单来通过.

我对问题的猜测

我假设它是因为当它检查它或接受输入时,它没有正确检查它,也许它保存为数组或奇怪的字符串形式?

现在的问题是 string in blacklist 仅在 blacklist 恰好包含 string 时才有效。任何变化,它不会工作。相反,您应该遍历每个列入黑名单的单词并检查该单词是否在消息中,如下所示:

if any(black_word in orderItem.lower() for black_word in blacklist):
    await ctx.send("Due to your order containing one of the blacklisted terms, your order will not be placed.")
    return

如果 任何 黑名单单词在 orderItem.lower()

中,any 函数将 return True

例如,如果 blacklist['word1', 'word2'],而消息是 'word1 extra',则原始代码将不起作用,因为确切的字符串 'word1 extra' 不是在黑名单中。但是黑名单词'word1''word1 extra'.

此外,在 return 之后,您不需要放置 else 语句。所以对于用户黑名单应该是:

if ctx.author.id in user_blacklist:
    await ctx.send("You are blacklisted from ordering from Discord Chinese")
    return

# rest of code, not in an else block