Search code examples
pythondiscord.pychatbot

How to make the discord bot respond different (multiple) keywords in the others message?


I'd like to know how to make the discord bot respond differently (multiple) keywords in the others message. Here's what I want it to do:


Case 1:


Discord User: Hi


Bot: Excesue me, are you talking to me?



Case 2:


Discord User: Hi bot


Bot: Oh hi


Here is the code that I have tried:

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    if "Hi" in message.content or "hi" in message.content or "Hello" in message.content or "hello" in message.content:
        if "Adam" not in message.content or "ADAM" not in message.content or "adam" not in message.content or "everyone" not in message.content or "Everyone" not in message.content or "EVERYONE" not in message.content or "everybody" not in message.content or "Everybody" not in message.content or "EVERYBODY" not in message.content:
            random_msg_greeting_1 = random.randint(1,3)
            if random_msg_greeting_1 == 1:
                await message.channel.send("Oh are you saying Hello to me?")
            elif random_msg_greeting_1 == 2:
                await message.channel.send("Oh are you talking to me?")
            elif random_msg_greeting_1 == 3:
                await message.channel.send("Hmmmmmm... are you talking to me? (Hello?)")

        else:
            random_msg_greeting_2 = random.randint(1,3)
            if random_msg_greeting_2 == 1:
                await message.channel.send ("Hello!")
            elif random_msg_greeting_2 == 2:
                await message.channel.send("Hi!")
            elif random_msg_greeting_2 == 3:
                await message.channel.send ("Oh hi!!!")

And this is the result:


Me: Hi


ADAM (my bot): Oh are you talking to me?


Me: Hi ADAM!


ADAM: Hmmmmmm... are you talking to me? (Hello?)


I want him to recognize multiple keywords in one message, like here I want him to be able to tell if someone is saying hello by checking for keywords like "Hi". And by checking for keywords like "ADAM" to tell if someone is talking to him.

If you know what I should do, please guide me. I desperately need your help. Thank you!


Solution

  • Fixed your messy code. But you need to use something like language processing for it if you want to make a chatbot

    @bot.event
    async def on_message(message):
        if message.author.id == bot.user.id:
            return
        if "hi" in message.content.lower() or "hello" in message.content.lower():
            if "adam" in message.content.lower():
                random_msg_greeting_1 = random.randint(1,3)
                if random_msg_greeting_1 == 1:
                    await message.channel.send("Hello!")
                elif random_msg_greeting_1 == 2:
                    await message.channel.send("Hi!")
                elif random_msg_greeting_1 == 3:
                    await message.channel.send ("Oh hi!!!")
    
            else:
                random_msg_greeting_2 = random.randint(1,3)
                if random_msg_greeting_2 == 1:
                    await message.channel.send("Oh are you saying Hello to me?")
                elif random_msg_greeting_2 == 2:
                    await message.channel.send("Oh are you talking to me?")
                elif random_msg_greeting_2 == 3:
                    await message.channel.send("Hmmmmmm... are you talking to me? (Hello?)")