-1

I need my bot to reply when someone says Hi or Hello, but when someone says a word including the letters 'hi' it will also respond to it, any way to fix this and tell the code to look for the exact word only? I'm using a very simple code ofcourse:

if message.content.startswith('hi') or message.content.startswith('hello'):
      #await message.reply("Hello Bro ")
MrSouheil
  • 21
  • 1
  • 7
  • An example of the thing i want fixed is if someone started his message with 'Hide' it will also trigger the response. – MrSouheil Jul 12 '22 at 17:07
  • split by spaces and check the first index. you'll still have an issue with the bot responding to some who's saying hi or hello to someone else, but it won't trigger on other words. if message.content.split(" ")[0] in ['hi', 'Hi', 'hello', 'Hello']: – smcrowley Jul 12 '22 at 17:23
  • 1
    @smcrowley This worked! Thank you so much, do you mind answering it instead of commenting so I can mark it as an answer? :) – MrSouheil Jul 12 '22 at 17:28
  • Does this answer your question? [Check if a word is in a string in Python](https://stackoverflow.com/questions/5319922/check-if-a-word-is-in-a-string-in-python). Specifically [this answer](https://stackoverflow.com/a/5320179/16177247) – TheFungusAmongUs Jul 13 '22 at 04:39

3 Answers3

2

You can use regular expressions

>>> import re
>>> check_hello = re.compile(r"^\bhi\b|\bhello\b", re.IGNORECASE)
>>> re.search(check_hello, "Hi,de")
<re.Match object; span=(0, 2), match='Hi'>
>>> re.search(check_hello, "Hide")
>>> re.search(check_hello, "Hello,de")
<re.Match object; span=(0, 5), match='Hello'>
>>> re.search(check_hello, "Hellode")

So your code might look like this

import re
check_hello = re.compile(r"^\bhi\b|\bhello\b", re.IGNORECASE)
if re.search(check_hello, message.content):
      #await message.reply("Hello Bro ")

Explanation

In a regular expression, the character ^ means the beginning of a line (if you want the message to start with the word you are looking for). The symbol \b means the word boundary. The | symbol allows you to use multiple search options.

Wild Zyzop
  • 580
  • 1
  • 3
  • 13
  • Thank you! This answer is too professional for my level i'm a starter here, it works and i'll have to learn about it to understand it. – MrSouheil Jul 13 '22 at 18:13
  • Regular expressions are a very powerful tool that it is desirable to be able to use. There are excellent sites for debugging regular expressions, for example https://regex101.com/r/8pOwoA/1 (link directly to your example). You can read more about regular expressions here https://realpython.com/regex-python/ – Wild Zyzop Jul 16 '22 at 10:44
0

split by spaces and check the first index. you'll still have an issue with the bot responding to some who's saying hi or hello to someone else, but it won't trigger on other words.

if message.content.split(" ")[0] in ['hi', 'Hi', 'hello', 'Hello']:
    # respond

if you want more general check allowing for any capitalization (hI, hElLo, ...)

if message.content.split(" ")[0].lower() in ['hi', 'hello']:
    # respond
smcrowley
  • 451
  • 3
  • 10
-1

I don't understand why the other answers are more complicated, unless I'm misunderstanding your question. Just simply do a if x in y check.

if "hi" in message.content.lower():
    await message.reply("Hello Bro ")
Aditya Tomar
  • 1,601
  • 2
  • 6
  • 25
  • Hi, it's because i had this in my code and it returned the reply with every instance of the 2 letters 'hi' anywhere, if someone said 'Let's play hide' it will reply as well. I hope this made it clear i'm not a professional in these. – MrSouheil Jul 13 '22 at 18:11
  • @MrSouheil ah got it. I see now. – Aditya Tomar Jul 13 '22 at 19:31