Typewriter effect in discord.py (i used google translate to ask this question, sry for any mistakes)

338 Views Asked by At
async def type(ctx):
words = "Hello"
for x in words:
    await ctx.send(x)
    time.sleep(0.1)

Output:

H
e
l
l
o

They are not displayed in one line. What must I do to fix this problem?

2

There are 2 best solutions below

0
unex On BEST ANSWER

You are sending a new message for every iteration of your loop, use Message.edit instead. Also, you need to use asyncio.sleep in place of time.sleep in async functions.

So something like:

message = None
for i in range(len(words)):
    if not message:
        message = await ctx.send(words[:i+1])
        continue

    await message.edit(content=words[:i+1])
    await asyncio.sleep(0.1)

Note that discord will likely rate limit you if you do this with longer messages.

6
Goldwave On

This is not possible by sending messages, you need to edit them.

Example of editing your message. The only modification needed for your code, is to first send 'H' as a regular message. Then edit that message, appending a new character at a time.