I'm trying to create a bot that can mimic a normal user. One of the thing that the bot will have to do is to display "The bot is typing ...", synchronously with the player it mimics (delay of 1 or 2 seconds is not a problem). This implies two things:
In the first case, the on_typing() event is great.
The second case is more complicated. The user can have finished to type its message because he send it or because he deleted it.
Detecting he send his message is easy with on_message. But with a deleted message ? I didn't find anything. There is no such event in the Client documentation. Does anyone knows a workaround ?
EDIT : I know how to detect if a user started to type. The problem is how to detect he has finished.
Base on @Blckknght's comment
There isn't a event that discord offers to detect when a user stops typing. However you can monitor when a user starts typing & wait 10 seconds which is the time it takes for discord to no longer display them as typing. Or catch when they've sent their message to be sent. Before removing them from a dictionary of people typing.
class MyClient(discord.Client):
users_typing = {}
async def on_ready(self):
print(f"Logged in as {self.user}!")
# (Can be more channels types)
async def on_typing(self, channel:discord.TextChannel|discord.Thread, user:discord.User, when:datetime):
print(f"{user.name} is typing in {channel.name} at {when}")
self.users_typing[user.id] = {
"when": when,
"channel": channel # Optional, just in case you want to use it elsewhere
}
# It takes around 10 seconds for discord to display the user as no longer typing since it started.
await asyncio.sleep(10)
# Check user still in dict
# Also checks if our when matches the dict otherwise, they have started typing again
if user.id in self.users_typing and \
self.users_typing[user.id]['when'] == when:
print(f"{user.name} has stopped typing in {channel.name}.")
del self.users_typing[user.id]
async def on_message(self, message):
user = message.author
# If a user sends the message before the 10secs
# then they are typing remove them from users_typing
if user.id in self.users_typing:
print(f"{user.name} has stopped typing in {message.channel.name} and sent the message!")
del self.users_typing[user.id]
# Setup intents
intents = discord.Intents.default()
intents.typing = True # Required for on_typing
intents.message_content = True # Required for on_message
intents.members = True # Optional however it gives you access to more info on the user
client = MyClient(intents=intents)
client.run('DISCORD_BOT_TOKEN')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With