4

I have an async function that I'm trying to get the return variable from but I'm not able to get it to work for some reason, I've tried a few different things from googling but theyre all returning a similar error.

I have this function:

@bot.command()
async def start(ctx):
    """starts the server"""
    try:
        status = server.status()
        await ctx.channel.send("The server is already online!")
    except:
        os.chdir(".\\Minecraft")
        file = subprocess.Popen("Start.bat")
        await ctx.channel.send("starting the server")
        starting = True
        while starting == True:
            time.sleep(int(delay))
            with open("outfile.txt") as outfile:
                for line in outfile:
                    if "Done" in line:
                        await ctx.channel.send("server has loaded")
                        starting = False
                        return file
                    else:
                        continue

and i return the variable file

But then when I try to get the variable in another function

@bot.command()
async def kill(ctx):
    """shuts down the server"""
    x = start(ctx)
    print(x)
    x.terminate()

I get the error:

<coroutine object Command.__call__ at 0x040F7B28>
Ignoring exception in command kill:
Traceback (most recent call last):
  File "C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:/Users/TheRi/OneDrive/Desktop/Python/MinecraftDiscordBot/minecraftBot.py", line 113, in kill
    x.terminate()
AttributeError: 'coroutine' object has no attribute 'terminate'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'coroutine' object has no attribute 'terminate'
C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\asyncio\events.py:81: RuntimeWarning: coroutine 'Command.__call__' was never awaited
  self._context.run(self._callback, *self._args)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

The first line seems to be where I've attempted to print x, to see if I could see what was going on, and the rest is the error. It doesn't seem to be returning any value but just the co routine itself?

I've tried changing the way I refrence the function, x = start(ctx),x = start(),x = start etc.

Is there something I'm doing wrong? How can I return the variable.

1 Answer 1

3

You need to await coroutines; i.e. x = await start(ctx)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.