首页 > 解决方案 > 我将如何发出命令并且有人使用它来检测他们是否将受限制的单词放入它运行的受限制单词列表中'print(“Language”)'?

问题描述

所以我正在尝试创建一个命令并且有人使用它,它会检测他们是否在它运行的受限单词列表中放置了一个受限单词 print("Language") 下面是我尝试过的代码:

 @bot.command()
async def order(ctx, food):
    "This is to make an order to the kitchen, just say d.cook (food) to order"
    await ctx.message.delete
    kitchen = bot.get_channel(806579906095874088)
    waitEmbed = discord.Embed(title="Getting food to the kitchen", description=f"Your order has been placed of {food}, it will be out shortly :)", color=0xA52A2A)
    waitEmbed.add_field(name="From:", value=f"{ctx.message.author.mention}", inline=True)
    orderSendEmbed = discord.Embed(title="Cook food", description=f"Chefs! Cook {food}.", color=0x00ff00)
    orderSendEmbed.add_field(name="Food for:", value=f"{ctx.message.author.mention}", inline=True)
    await ctx.send(embed=waitEmbed)
    await kitchen.send(embed=orderSendEmbed)
    print(f"{ctx.message.author}, used the order command in {ctx.message.channel} and ordered {food}.")
    for word in ctx.message.content:
        if word in RestrictedWords.RestrictedWordsList:
            print("Language!")

标签: pythondiscord.py

解决方案


str.split()迭代消息内容时使用:

@bot.command()
async def order(ctx, food):
    "This is to make an order to the kitchen, just say d.cook (food) to order"
    await ctx.message.delete()
    kitchen = bot.get_channel(<channel_id>)
    waitEmbed = discord.Embed(title="Getting food to the kitchen", description=f"Your order has been placed of {food}, it will be out shortly :)", color=0xA52A2A)
    waitEmbed.add_field(name="From:", value=f"{ctx.message.author.mention}", inline=True)
    orderSendEmbed = discord.Embed(title="Cook food", description=f"Chefs! Cook {food}.", color=0x00ff00)
    orderSendEmbed.add_field(name="Food for:", value=f"{ctx.message.author.mention}", inline=True)
    await ctx.send(embed=waitEmbed)
    await kitchen.send(embed=orderSendEmbed)
    print(f"{ctx.message.author}, used the order command in {ctx.message.channel} and ordered {food}.")
    for word in ctx.message.content.lower().split():
        if word in ['foo', 'bar']: # Replace with your all lowercase RestrictedWordsList
            print('Language!')

它将在每个空格处拆分字符串并返回您可以迭代的单词列表。此命令打印Language!内容中找到的每个受限单词。使用lower()beforesplit()会使检查大小写不敏感。

如果没有 split(),for 循环将遍历消息中的每个字符。


推荐阅读