discord.py-rewrite仅在将某些术语列入黑名单的情况下,才尝试将它们列入黑名单

时间:2020-07-07 00:03:53

标签: python python-3.x discord discord.py discord.py-rewrite

我的目标和一些背景信息

您好,大约一天前,我尝试将某些字词列入黑名单,因此,如果用户说了不适当的话,它将无法继续执行该命令(可以在here处找到我的stackoverflow问题),然后尝试将其与将某些人列入黑名单,(可以在here中找到我的stackoverflow问题),但是仅将单词列入黑名单的条件是将其列入黑名单。可以找到code命令

我的命令的代码

@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无效,则允许出现顺序通过。

我对问题的猜测

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

1 个答案:

答案 0 :(得分:1)

当前的问题是,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

如果any中的任何列入黑名单的单词,True函数将返回orderItem.lower()

例如,如果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