Discord.py-重写获取黑名单术语列表并检查命令后的参数是否包含黑名单术语之一

Discord.py-Rewrite Taking a list of blacklisted terms and checks if arguments after command contains one of the blacklisted terms

我的目标

我正在开发类似于“Discord Delivers”和“Pizza Byte”的 Discord 机器人。我正在尝试获取 terms/keywords 的列表并检查命令后的参数(命令的代码位于此 post 的末尾);因此,如果他们这样做 >order BadWord,那么它会检查它是否包含先前指定的列入黑名单的术语之一,如果是,则像 await ctx.send("Due to your order containing one of the blacklisted terms, your order will not be placed.") 或类似的东西。很抱歉没有说出我已经尝试过的,正如我所能想到的,正在为每个单词做 if args in blacklist: 或类似的事情。

我的代码

@bot.command(pass_context=True)
async def order(ctx, *, orderItem):
    channel = bot.get_channel(CHANNEL ID OF THE CHANNEL)
    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)

额外

如果可能的话,列入黑名单的条款是否可以放在 json 文件或文本文件中?谢谢。

编辑:澄清

我想我应该澄清一下,我确实定义了所使用的变量。一切正常。

  • 没有 json 库:
blacklist = ['test', 'yolo'] #Your blacklisted words here
@bot.command(pass_context=True)
async def order(ctx, *, orderItem):
    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:
        #Your code here
  • 使用 json 库:

json 文件(将包含所有列入黑名单的词)

['test', 'yolo']
from json import loads

@bot.command(pass_context=True)
async def order(ctx, *, orderItem):
    with open('blacklist.txt', 'r') as file:
        blacklist = loads(file.read())
    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:
        #Your code here