Skip to content
Advertisement

Discord.py async function does not give any output and does not do anything

here is the code:

print('hmm1') #testing, this one prints
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='&')
client.run('my token', bot=False)

async def testFunction():
    print('hmm') #<- this one does not print.
    channel = await client.get_channel(708848617301082164)
    message_id=715307791379595275
    msg = await client.get_message(channel, message_id)
    await msg.edit(content="L")
    await msg.edit(content="W")
    print('edited message!')
testFunction()
# none of the above works. I only get "hmm1" printed in console.

I have no clue what is happening as there is quite literally no error or output of any sort in the console. does anyone know the problem?

Advertisement

Answer

If you’re not familiar with asynchronous functions, they need to be awaited. Examples of coroutines can be seen in msg.edit(..., as edit() is a coroutine, therefore you need to await it like so: await testFunction()

Additionally, client.get_channel() and client.get_message() aren’t coroutines, so they don’t need to be awaited.

As Eric mentioned, you’ll also want to move your client.run('... down to the last line in your file, otherwise it’ll block the rest of the script. Here’s how the code should be structured:

# imports

# commands, events, functions

# last line
client.run('...

It looks like you’re using some old documentation too, as d.py has moved over to rewrite (v1.x), and it looks as though the client.get_message() you were using is actually from v0.16.x.

I’d recommending wanting to read up on these changes to familiarise yourself with rewrite. Try to avoid outdated tutorials as well.

As a little headstart, your await client.get_message(channel, message_id) should become await channel.fetch_message(message_id).


References:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement