【问题标题】:Discord bot is flagging other words that I don't want it toDiscord 机器人正在标记其他我不希望它标记的词
【发布时间】: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
  • 听起来你想check if a word is in a string
  • @ChrisClayton 是的,谢谢,我尝试查找重新但没有看到正确的内容
  • @PiCTo 这就是我现在所拥有的,但问题是如果有人说任何包含“测试”的内容,例如“测试”,那么它会警告他们,但我只想要它当他们特别说“测试”时警告他们
  • 请查看我链接的问题,包括其第一个(接受的)答案。您的代码当前所做的不是检查message.content 中的word,而是寻找子字符串。您应该使用正则表达式或(更基本的)检查if ' {} '.format(word) in message.content.lower()(注意空格)。所有这些都在that question 的答案中提到,而您只是将其置于上下文中。

标签: python discord python-3.6 discord.py-rewrite


【解决方案1】:

我在不需要 Discord 的情况下使用以下测试基本要求 online。这应该是一个很好的起点。 \b 分隔正则表达式中的单词。

import re

while(input('End? ').lower() != 'y'):
  words=['test']
  message = input('Enter your message: ')
  for word in words:
      if re.search(rf"\b{word}\b", message.lower()):
        print('AUTHOR make sure that you do not use word unless you are in <#750445277130784788>'.format(message))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-18
    • 2021-03-23
    • 2019-12-30
    • 2021-10-05
    • 2021-10-27
    • 2021-02-16
    • 2021-08-01
    • 2021-11-10
    相关资源
    最近更新 更多