'Making a discord bot able to handle several inputs at once
This is a pretty basic question but here is the issue. I am trying to make a discord bot that takes a message and delays it by a set amount of time. However, it only can handle one message at a time and will not delay any others until that one has gone through. Is there a way to make it so the bot can handle all the messages at once? here is the code for the function
@client.event
async def on_message(message):
if not message.author.bot:
await message.delete()
time.sleep(100)
await message.channel.send('{} : {}'.format(message.author.name,
message.content))
await client.process_commands(message)
Solution 1:[1]
This is because time.sleep()
blocks code and pretty much stops everything from working until the time set expires. What you want to use is asyncio.sleep()
import asyncio
@client.event
async def on_message(message):
if not message.author.bot:
await message.delete()
await asyncio.sleep(100)
await message.channel.send('{} : {}'.format(message.author.name,
message.content))
await client.process_commands(message)
asyncio.sleep()
will still allow you to do what you intended to do but also allow the bot to continue working and accept more requests/tasks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |