Skip to content
How do I randomize responses?
Profile icon

Is there a way to give a bot a short list in the code so it can generate out one of a few responses? If someone types in p.h, the bot could respond with hi, hello, or hey. I've so far only seen tutorials that use APIs for random generation, but I don't need anything like that.

You are viewing a single comment. View All
Profile icon

It looks like you have a myriad of conditions, some using startswith() and some just using in. I guess the first thing I would ask is why not just use the in nomenclature instead of using startswith that way you can have a consistent format. And then secondly, you should separate all of those responses into a function that you can call with particular content like I'm going to do below.

Ideally, you would store all responses in a database, JSON store, and anywhere else with persistent storage and remove all of the if statements and just retrieve the appropriate response based on the message content. But for now, just moving the logic out of the event into its own function will clean up that event a lot and separate things out nicely.

async def probeForResponse(message): if "What" in message.content: await message.add_reaction("Lacie:822228153685508156") #all of the other logic would be here

And then inside of the client event where a message is posted, call the probeForResponse function.

@client.event async def on_message(message): if == client.user: return await probeForResponse(message)

Good luck to you, Carnelian!