Home > OS >  Discord.py : coroutine was never awaited
Discord.py : coroutine was never awaited

Time:12-05

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 -

  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

CodePudding user response:

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

  • Related