python 3.9 discord.py asyncio.async error

Viewed 859

This question deals with discord.py errors caused by asyncio, but any advice on creating discord bots would be very welcome. I am more than willing to read documentation to fully understand responses, and will pursue provided articles.

Problem:

I am attempting to create a simple discord bot in python 3.9. I am new to python, and have been using pycharm to write my scripts. I found an error while attempting to run the script. Script provided here:

import discord

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

client.run('my bot token')

Error provided here:

Traceback (most recent call last):
  File "C:\Users\User\py_projects\discord\scrambot_main.py", line 2, in <module>
    import discord
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\__init__.py", line 20, in <module>
    from .client import Client, AppInfo, ChannelPermissions
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 38, in <module>
    from .state import ConnectionState
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\state.py", line 36, in <module>
    from . import utils, compat
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\compat.py", line 32
    create_task = asyncio.async
                          ^
SyntaxError: invalid syntax

Solutions I've Seen So Far:

I have seen a myriad of questions asking the same for python 3.7, and none for 3.9. This would either be because nobody else experiences the same error with python 3.9 in pycharm, or the solution is the same as in python 3.7.

I have seen many people recommending dropping to version 3.6 of python because async is a keyword in 3.7, which I want to avoid if possible. I'm fairly sure it still is a keyword in 3.9.

Some responses have said that pycharm creates virtual environments and would not have discord.py installed. This is not true in my case -- I have checked my project preferences and discord.py is in fact included and accessible by my script. More evidence is in the error message: the line causing problems is found in the discord.py directory, so the compiler must be able to access it. Right?

The problem may arise from the fact that i installed using pip install discord.py and then tried again with pip3 install discord.py. As far as I can tell, these worked as intended. There is an error if i try to use pip3 install discord or pip install discord, which I have provided. I do not know if the .py makes a difference, but if it does, i'm sure it would help to know that. The only other version of a solution i've seen is downloading the rewrite version of discord.py off github. I have not tried this yet because i want to make this version work if possible.

C:\Users\User>pip3 install discord
Collecting discord
  Using cached discord-1.0.1-py3-none-any.whl (1.1 kB)
Collecting discord.py>=1.0.1
  Using cached discord.py-1.5.1-py3-none-any.whl (701 kB)
Processing c:\users\user\appdata\local\pip\cache\wheels\b6\9c\bd\6b99bc6ec9dab11f3756d31fb8506d3ecf07aea58b6201f539\aiohttp-3.6.3-py3-none-any.whl
Collecting attrs>=17.3.0
  Using cached attrs-20.2.0-py2.py3-none-any.whl (48 kB)
Requirement already satisfied: chardet<4.0,>=2.0 in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (from aiohttp<3.7.0,>=3.6.0->discord.py>=1.0.1->discord) (3.0.4)
Collecting yarl<1.6.0,>=1.0
  Using cached yarl-1.5.1.tar.gz (173 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Collecting multidict<5.0,>=4.5
  Using cached multidict-4.7.6.tar.gz (50 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Requirement already satisfied: async-timeout<4.0,>=3.0 in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (from aiohttp<3.7.0,>=3.6.0->discord.py>=1.0.1->discord) (3.0.1)
Collecting idna>=2.0
  Using cached idna-2.10-py2.py3-none-any.whl (58 kB)
Building wheels for collected packages: yarl, multidict
  Building wheel for yarl (PEP 517) ... error
  ERROR: Command errored out with exit status 1:
   command: 'c:\users\user\appdata\local\programs\python\python39\python.exe' 'c:\users\user\appdata\local\programs\python\python39\lib\site-packages\pip\_vendor\pep517\_in_process.py' build_wheel 'C:\Users\User\AppData\Local\Temp\tmpib94rz6b'
       cwd: C:\Users\User\AppData\Local\Temp\pip-install-rrtk0pij\yarl
  Complete output (35 lines):
  **********************
  * Accellerated build *
  **********************
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win-amd64-3.9
  creating build\lib.win-amd64-3.9\yarl
  copying yarl\_quoting.py -> build\lib.win-amd64-3.9\yarl
  copying yarl\_quoting_py.py -> build\lib.win-amd64-3.9\yarl
  copying yarl\_url.py -> build\lib.win-amd64-3.9\yarl
  copying yarl\__init__.py -> build\lib.win-amd64-3.9\yarl
  running egg_info
  writing yarl.egg-info\PKG-INFO
  writing dependency_links to yarl.egg-info\dependency_links.txt
  writing requirements to yarl.egg-info\requires.txt
  writing top-level names to yarl.egg-info\top_level.txt
  reading manifest file 'yarl.egg-info\SOURCES.txt'
  reading manifest template 'MANIFEST.in'
  warning: no previously-included files matching '*.pyc' found anywhere in distribution
  warning: no previously-included files matching '*.cache' found anywhere in distribution
  warning: no previously-included files found matching 'yarl\*.html'
  warning: no previously-included files found matching 'yarl\*.so'
  warning: no previously-included files found matching 'yarl\*.pyd'
  no previously-included directories found matching 'docs\_build'
  writing manifest file 'yarl.egg-info\SOURCES.txt'
  copying yarl\__init__.pyi -> build\lib.win-amd64-3.9\yarl
  copying yarl\_quoting_c.c -> build\lib.win-amd64-3.9\yarl
  copying yarl\_quoting_c.pyi -> build\lib.win-amd64-3.9\yarl
  copying yarl\_quoting_c.pyx -> build\lib.win-amd64-3.9\yarl
  copying yarl\py.typed -> build\lib.win-amd64-3.9\yarl
  running build_ext
  building 'yarl._quoting_c' extension
  error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
  ----------------------------------------
  ERROR: Failed building wheel for yarl
  Building wheel for multidict (PEP 517) ... error
  ERROR: Command errored out with exit status 1:
   command: 'c:\users\user\appdata\local\programs\python\python39\python.exe' 'c:\users\user\appdata\local\programs\python\python39\lib\site-packages\pip\_vendor\pep517\_in_process.py' build_wheel 'C:\Users\User\AppData\Local\Temp\tmp_yeznrct'
       cwd: C:\Users\User\AppData\Local\Temp\pip-install-rrtk0pij\multidict
  Complete output (40 lines):
  **********************
  * Accellerated build *
  **********************
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win-amd64-3.9
  creating build\lib.win-amd64-3.9\multidict
  copying multidict\_abc.py -> build\lib.win-amd64-3.9\multidict
  copying multidict\_compat.py -> build\lib.win-amd64-3.9\multidict
  copying multidict\_multidict_base.py -> build\lib.win-amd64-3.9\multidict
  copying multidict\_multidict_py.py -> build\lib.win-amd64-3.9\multidict
  copying multidict\__init__.py -> build\lib.win-amd64-3.9\multidict
  running egg_info
  writing multidict.egg-info\PKG-INFO
  writing dependency_links to multidict.egg-info\dependency_links.txt
  writing top-level names to multidict.egg-info\top_level.txt
  reading manifest file 'multidict.egg-info\SOURCES.txt'
  reading manifest template 'MANIFEST.in'
  warning: no previously-included files matching '*.pyc' found anywhere in distribution
  warning: no previously-included files found matching 'multidict\_multidict.html'
  warning: no previously-included files found matching 'multidict\*.so'
  warning: no previously-included files found matching 'multidict\*.pyd'
  warning: no previously-included files found matching 'multidict\*.pyd'
  no previously-included directories found matching 'docs\_build'
  writing manifest file 'multidict.egg-info\SOURCES.txt'
  copying multidict\__init__.pyi -> build\lib.win-amd64-3.9\multidict
  copying multidict\_multidict.c -> build\lib.win-amd64-3.9\multidict
  copying multidict\py.typed -> build\lib.win-amd64-3.9\multidict
  creating build\lib.win-amd64-3.9\multidict\_multilib
  copying multidict\_multilib\defs.h -> build\lib.win-amd64-3.9\multidict\_multilib
  copying multidict\_multilib\dict.h -> build\lib.win-amd64-3.9\multidict\_multilib
  copying multidict\_multilib\istr.h -> build\lib.win-amd64-3.9\multidict\_multilib
  copying multidict\_multilib\iter.h -> build\lib.win-amd64-3.9\multidict\_multilib
  copying multidict\_multilib\pair_list.h -> build\lib.win-amd64-3.9\multidict\_multilib
  copying multidict\_multilib\views.h -> build\lib.win-amd64-3.9\multidict\_multilib
  running build_ext
  building 'multidict._multidict' extension
  error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
  ----------------------------------------
  ERROR: Failed building wheel for multidict
Failed to build yarl multidict
ERROR: Could not build wheels for yarl, multidict which use PEP 517 and cannot be installed directly

Questions:

    Has anyone had success with discord bots in python 3.9?
    Is something wrong with my script?
    Is async a keyword in python 3.9? If so, how can i modify my program to solve this problem?
    How can I fix this issue without downgrading to python 3.6? Is that the only way?
    Why does pip install discord not work? What are wheels? I do not want to install C++ build tools.
    Would it be easier to program a bot in C++ or JS? I don't know any JS but i am familiar with C++.

Any help with this issue would be massively appreciated. Also, as this is my first stack overflow post, please feel free to let me know if i could have done anything better in asking this question. if there's anything else i can provide to help you wonderful developers who are willing to spend their time solving noob problems like this one, please let me know.

Also, if willing and able, please provide some tips for writing discord bots. This is intended to be a small Admin bot created to test and develop my python skills. Interaction with people is the focus, not automation of any kind, although I do want to try out as many ideas as possible.

Edit: Included PyCharm python interpreter settings. PyCharm interpreter settings

Also had an issue with opening a new venv project and installing discord.py to it. Same wheels error as above, provided errors and other relevant windows in the image below. also not sure where to put my script file or how to make the run configuration. PyCharm interpreter settings and install error

1 Answers

the issue is with your discord.Client() you should never use that instead use

from discord.ext import commands

client = commands.Bot(prefix="ur prefix")

And you have written command in wrong way

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

correct way :

@client.command()
async def hello(ctx):
  await ctx.send("Hello!")
Related