python - 我将如何发出命令并且有人使用它来检测他们是否将受限制的单词放入它运行的受限制单词列表中'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!")
解决方案
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 循环将遍历消息中的每个字符。
推荐阅读
- android-studio - 从 Android 应用程序连接到 MariaDB
- html - 我在我的网站上使用引导导航栏,与其他页面相比,它在索引页面上的作用不同
- java - 集成测试无法连接,使用 SpringBoot/Jsoup 集成测试的 Java RESTful Web 服务手动浏览器测试成功
- javascript - React - 在组件内部使用“文档”
- xpath - XQuery 解码 HTTP 请求 - 无法解析查询
- html - 在具有灵活行的嵌套网格布局中,为什么滚动条需要两个“溢出:自动”
- python - 相关网络的前向传递输出在没有反向传播的情况下发生变化
- vba - 文件不移动 VBA
- tensorflow - 使用机器学习检测 CANBUS 模式
- tensorflow - TensorFlow 中的回调