问题
MY GOAL
I am working on a discord bot similar to "Discord Delivers" and "Pizza Byte". And I am trying to take a list of terms/keywords and check if the arguments after the command (The code for the command is at the end of this post); So if they do >order BadWord
then it checks if it contains one of the previously specified blacklisted terms, and if so, does like await ctx.send("Due to your order containing one of the blacklisted terms, your order will not be placed.")
Or something like that. I am sorry for not saying what i have tried, as all i can think of, is doing things like if args in blacklist:
or something like that for each word.
MY CODE
@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)
EXTRA
And if possible, could the blacklisted terms be in like either a json file or a text file? Thanks.
EDIT: CLARIFICATION
I thought i should clarify that I do in fact define the variables that are used. Everything works properly.
回答1:
- Without the
json
library:
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
- Using the
json
library:
The json file (which will contain every blacklisted words)
['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
来源:https://stackoverflow.com/questions/62741116/discord-py-rewrite-taking-a-list-of-blacklisted-terms-and-checks-if-arguments-af