C
C
chezas2021-04-15 17:00:18
Python
chezas, 2021-04-15 17:00:18

How to make a discord bot send messages at random times?

Need help. There is a bot and a text channel. There is a set of replicas. Task: make the bot send randomly selected replicas at random times. Is there a way to implement this? (Itself in a python very recently, therefore I ask, please, a little bit more in detail)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2021-04-15
@Vindicar

Well, to begin with, define what is meant by random time.
For example, "one message every t seconds, where t is a uniformly distributed number from 300 to 3600"
Or "choose a random time in a day, wait for it to occur, send a replica"
Or "let the bot roll a die every N seconds if one is rolled - output a message.
All three methods imply different implementations and different limitations.
I'm assuming you're making a bot with discord.py?
Then dig aside:
1. the random module to understand how to generate random numbers
2. the section of the discord.py documentation dedicated to lengthy processes.
The logic is something like this:
1. Use the example above to figure out how to make a simple loop so that the bot writes something to the console.
2. Use asyncio.sleep() to delay the execution of the loop for some time.
3. Learn to generate random numbers using the random module, and make the delay random.
4. Learn how to randomly select a message with random.choice()
5. Learn how to send messages to a channel instead of the console ( a method that will come in handy)
More details - just code.

K
Krytiu_govnocoder, 2021-06-23
@Krytiu_govnocoder

The person above painted everything clearly, although it would be easier to throw off the code already. If you haven’t figured out how to do it yet, then here is the code, it’s primitive, you’ll be finalizing it yourself
@commands.Cog.listener()
async def on_ready(self):
while True:
channel = self.client.get_channel(channel ID)
delay = random.uniform(5, 10)
await asyncio.sleep(delay)
await channel.send("your text")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question