discord.py 重写 尝试将某些术语列入黑名单仅在其仅列入黑名单的术语时才有效



我的目标和一些背景信息

您好,大约一天前,我试图将某些术语列入黑名单,因此如果用户说了一些不恰当的话,那么它将不会继续执行命令(我的堆栈溢出问题可以在这里找到(我尝试将其与将某些人列入黑名单混合,(我的堆栈溢出问题可以在这里找到(但是单词的黑名单只有在它只不过是黑名单术语时才有效。代码命令可以找到

我的命令代码

@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那么如果参数是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函数将返回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

相关内容

最新更新