Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Discord.py : coroutine was never awaited

Can’t seem to fix this code where I’m trying to get the name of the discord server from it’s invite link.

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

async def get_invite_name(link):
    name = await client.fetch_invite(link).guild.name
    return name

print(get_invite_name('https://discord.com/invite/veefriends'))

Tried putting await infront of client.fetch_invite(link).guild.name but it didn’t work. I don’t understand async.

Tried what @matthew-barlowe suggested but it spit out more errors –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  File "~/DLG/sandbox.py", line 14, in <module>
    print(asyncio.run(get_invite_name('https://discord.com/invite/veefriends')))
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "~/DLG/sandbox.py", line 12, in get_invite_name
    return await invite.guild.name
AttributeError: 'coroutine' object has no attribute 'guild'
sys:1: RuntimeWarning: coroutine 'Client.fetch_invite' was never awaited

>Solution :

You have to await the async wrapper function get_invite_name as well. Running it in asyncio.run(get_invite_name('https://discord.com/invite/veefriends')) will handle that in a non async setting. You will need to import asyncio as well.

import discord
import asyncio
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

async def get_invite_name(link):
    response = await client.fetch_invite(link)
    name = response.guild.name
    return name

print(asyncio.run(get_invite_name('https://discord.com/invite/veefriends')))

If you were calling it in another async function then just await get_invite_name('https://discord.com/invite/veefriends') would be sufficient

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading