0

I know this question was asked before but I can't seem to find it anywhere so I'm just asking it. I have my discord bot send a message saying not to use a word unless in any channel but one (working); however, the bot also flags words that contain that word 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))

If a user says a word like 'testing' the bot flags it. I know there's a module or library or something that allows you to add word boundaries but I can't seem to find the post that talks about it.

  • 2
    Sounds like you're looking for regular expressions. https://docs.python.org/3/library/re.html – Chris Clayton Sep 15 '20 at 15:16
  • It sounds like you want to [check if a word is in a string](https://stackoverflow.com/questions/5319922). – PiCTo Sep 15 '20 at 15:23
  • @ChrisClayton Yes thank you, I tried looking up re but didn't ever see the correct thing – DartRuffian Sep 15 '20 at 16:07
  • @PiCTo That's what I have as of now, but the problem was that if someone said anything that contained 'test', such as 'testing' then it would warn them but I want it so it only warns them when they say 'test' specifically – DartRuffian Sep 15 '20 at 16:09
  • Please look at the question I link, including more than its first (accepted) answer. What your code is currently doing isn't checking for a _word_ in `message.content`, it's instead looking for a sub-string. You should use a regular expression or (more rudimentary) check `if ' {} '.format(word) in message.content.lower()` (notice the spaces). All of this is mentioned in the answers to [that question](https://stackoverflow.com/questions/5319922) and yours is merely putting it into context. – PiCTo Sep 15 '20 at 16:15
  • Well `string.find()` didn't even work properly and it sent ~20 messages so I think I'll stick with `re.search` – DartRuffian Sep 15 '20 at 17:13

1 Answers1

0

I used the below to test the basic requirement online without needing the Discord. This should be a good starting point. \b delimits words in RegEx.

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))
Chris Clayton
  • 241
  • 3
  • 9