Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
async def not registering the function as a coroutine
-
I am making a bot for discord and it requires Asyncio, but I am running into an error when I try to define my functions. This has never happened before, but I am now getting a “Callback must be a coroutine” error on all of my functions. Here is an example of one of those functions:
@bot.command(pass_context = True) async def ping(ctx): '''-> Pings Bot''' channel = ctx.message.channel t1 = time.perf_counter() await bot.send_typing(channel) t2 = time.perf_counter() await bot.say(':ping_pong: Pong! - {}'.format(round((t2-t1)*1000)) + "ms")
This raises an error for no apparent reason because I am using async def to define the function. Any help would be appreciated!
-
@TRGCapn, is this a runtime or a syntax error? There seems to be something called a ”rewrite of discord.py” - are you sure you are using the right version?
-
Are you sure you are running on Python 3?
-
Have you gotten things running before?
Try doing a force quit on pythonista, that sometimes solves wierd issues.When you get this error, do the following:
- print the full traceback -- make sure it is barfing on the command you think it is, and not some other where you forgot the async def. also, make sure the code is checking asyncio.iscoroutinefunction, not asyncio.iscoroutine (sunce async def passes the former, but fails the latter)
- Type
import pdb
thenpdb.pm()
and explore to make sure the func is the one you think, and you can explore to make sure things are being called correctly..
-
@ccc I am currently running on Python 3.6
-
@mikael It is actually a TypeError and I am using the regular version (0.16.12).
-
@JonB I have gotten things running before and force quitting didn’t help, but I’ve tried it multiple times. Printing the full traceback shows that the error is being raised in core.py of the discord module, making me think that is the screwed up one, not asyncio! Even so, I tried completely reinstalling discord.py, but that didn’t work so I’m confused about that. I checked and the function running is in fact asyncio.iscoroutinefunction so that’s not what is causing the error. I don’t know how to use pdb, but when I used the command you gave me the same thing popped up as in the Traceback, just not as an error message.
-
what might help is for you to print a complete traceback, and post. also, if you have a stripped down version of your code that displays this problem, others can help debug.
my bet is that the function it is getting hung up on maybe forgot the async. or, you have a whitespace problem, and the decorator is not being applied where you think. a full tracebCk, along with source should reveAl the issue
-
@JonB Full Traceback:
Traceback (most recent call last):
File "/private/var/mobile/Containers/Shared/AppGroup/9378EF36-EE10-41EA-8875-F8ADFD217EE5/Pythonista3/Documents/CapnBot/CapnBot/CapnBot/CapnBotV1.2.8.py", line 71, in <module>
@bot.command(pass_context = True)
File "/private/var/mobile/Containers/Shared/AppGroup/9378EF36-EE10-41EA-8875-F8ADFD217EE5/Pythonista3/Documents/site-packages-3/discord/ext/commands/core.py", line 573, in decorator
result = command(*args, **kwargs)(func)
File "/private/var/mobile/Containers/Shared/AppGroup/9378EF36-EE10-41EA-8875-F8ADFD217EE5/Pythonista3/Documents/site-packages-3/discord/ext/commands/core.py", line 680, in decorator
raise TypeError('Callback must be a coroutine.')
TypeError: Callback must be a coroutine. -
@JonB And here is a stripped down version of my code with just one function. All it does now is if a user sends “!hello” the bot will respond with “Hello There”. It still doesn’t work and has the same error, maybe you can try and fix it? You will have to use your own token because I’m not comfortable giving that away as it would give complete and total access over my bot and servers.
import discord from discord.ext import commands import asyncio import tokens TOKEN = tokens.my_token bot = commands.Bot(command_prefix = "!") @bot.command(pass_context = True) async def hello(ctx): await bot.say("Hello There!") bot.run(TOKEN)
-
Well for some weird reason, it just started working again. I did absolutely nothing to change it and yet when I tried it again for the hundredth time, it worked. Problem Solved?
-
See https://forum.omz-software.com/topic/5011/discord-py-asyncio-not-working
Make sure asyncio does not exist in site-packages-3 or site-packages