EchoTheAlpha
EchoTheAlpha

Reputation: 422

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

My Goal and some Background Info

Hello, About a day ago, I tried to blacklist certain terms so if the user said something inappropriate, then it wont continue with the command (My stackoverflow question can be found here) And I tried mixing it with blacklisting certain people, (My stackoverflow question can be found here) But the blacklisting of the words only work if its nothing but the blacklisted term. The code command can be found

My Command's 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)

My Issue

The user blacklisting properly works, but the keywords doesn't. So say the blacklisted term is Ubisoft then it works if the args is Ubisoft/ubisoft But if its Words words Ubisoft then it doesn't work, it allows the order to come through.

My Guess of the Issue

I'm assuming its because when it either checks it, or takes the input, it doesn't check it properly, maybe it saves as an either array or weird form of string?

Upvotes: 0

Views: 207

Answers (1)

TheSinisterShadow
TheSinisterShadow

Reputation: 196

The problem right now is that string in blacklist only works if blacklist exactly contains string. Any variations and it won't work. Instead, you should loop through each blacklisted word and check if the word is in the message, like this:

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

The any function will return True if any blacklisted words are in orderItem.lower()

As an example, if the blacklist is ['word1', 'word2'], and the message is 'word1 extra', the original code will not work because the exact string 'word1 extra' is not in blacklist. But the blacklisted word 'word1' is in 'word1 extra'.

Also, after you return you don't need to put an else statement. So for the user blacklist it should be:

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

Upvotes: 1

Related Questions