Ask coding questions

← Back to all posts
How do I make a discord bot send a random message
MIfoodie (1)

Hello,
How do I make my discord bot send a random message (A joke) out of a group of messages (I'll add multiple jokes to the group)? (I'm coding in python). My code below is what I already have

Answered by robowolf (435) [earned 5 cycles]
View Answer
Comments
hotnewtop
robowolf (435)

Just create a list of random jokes. Then import random and do random.choice(list_of_jokes). That will return a random joke from the list. Then just send that choice by using await message.channel.send(choice) in the on_message function

LukeWright (114)

you would need something like this (i think):

randomJokelist=["Why did the chicken cross the road? To escape KFC!", "A PUNNY BOOK: “Spaghetti Sauces” by Alfred O.", "A PUNNY BOOK: “Buried Under Everest Snow” by Ava Lanche."]

@client.event
async def on_message(message):
	if message.author != client.user:
		if message.content.startswith('*joke'):
                   numJoke = random.randint(0,len(randomJokelist))
                   await message.channel.send(randomJokelist[numJoke])

also, im pretty sure that you have to define the list where the jokes are being stored before you call them in the code.