【发布时间】:2021-01-02 08:54:29
【问题描述】:
我知道以前有人问过这个问题,但我似乎无法在任何地方找到它,所以我只是问一下。我让我的不和谐机器人发送一条消息,说不要使用单词,除非在除一个(工作)之外的任何渠道中;但是,该机器人还会标记包含该单词的单词 ex:
import discord
from discord.ext import commands
from discord.utils import get
bot = commands.Bot(command_prefix = '.')
@bot.event
async def on_message(message):
if message.author.id == bot.user.id:
return
words=['test']
if message.channel!='no-rules-lol':
for word in words:
if word in message.content.lower():
await message.channel.send('{0.author.mention} make sure that you do not use word unless you are in <#750445277130784788>'.format(message))
如果用户说出“测试”之类的词,机器人会对其进行标记。我知道有一个模块或库或其他允许您添加单词边界的东西,但我似乎找不到谈论它的帖子。
【问题讨论】:
-
听起来您正在寻找正则表达式。 docs.python.org/3/library/re.html
-
@ChrisClayton 是的,谢谢,我尝试查找重新但没有看到正确的内容
-
@PiCTo 这就是我现在所拥有的,但问题是如果有人说任何包含“测试”的内容,例如“测试”,那么它会警告他们,但我只想要它当他们特别说“测试”时警告他们
-
请查看我链接的问题,包括其第一个(接受的)答案。您的代码当前所做的不是检查
message.content中的word,而是寻找子字符串。您应该使用正则表达式或(更基本的)检查if ' {} '.format(word) in message.content.lower()(注意空格)。所有这些都在that question 的答案中提到,而您只是将其置于上下文中。
标签: python discord python-3.6 discord.py-rewrite