Search code examples
pythonlistdiscord.pystartswith

How can I make my bot execute the same action while using different messages?


Let's say I have a list.

List = ["Hello", "Sup", "Hi", "Yo"]

I want my bot to send the same message even if I use any of these words. If the person says Hello, bot will reply with Hello, If the user says Hi, bot will reply with Hello, and so on.

Now, I want to use message.content.startswith for this case, so that if the person types Hello along with other things (for example "Hello bot! Nice to see you.") the bot will still reply. I tried to use message.content, but if I have other words besides the "key" one, the bot won't reply.

I tried

List = ["Hello", "Sup", "Hi", "Yo"]

if message.content.startswith(List):
    await bla bla bla

but I got an error saying that the first arg. for startswith cannot be a list. And so yeah, that's basically it. I tried using the "any" function, but I noticed that's not really what I need (as far as I'm concerned, perhaps it is and I am wrong). But this is basically what I want: the bot to reply with the same thing whether if the person says Hello, Sup, Hi, etc...


Solution

  • if message.content.split()[0] is in List:
    #Do something
    

    This should work and allows you to change whatever in your list and it still work

    You could also use message.split()[0].upper() and make sure every word in the list is fully uppercase so it doesnt matter what case the user enters