我正在为Discord创建一个机器人,我只是编写了以下简单代码:
import discord
TOKEN = "token"
client = discord.Client()
@client.event
async def on_ready():
print('Bot is ready.')
client.run(TOKEN)
并产生以下错误:
Traceback (most recent call last):
File "/Users/pcaires/Desktop/Programação/Python/Discord Bots/Main.py", line 1, in <module>
import discord
File "/Users/pcaires/Desktop/Programação/Python/Discord Bots/venv/lib/python3.7/site-packages/discord/__init__.py", line 20, in <module>
from .client import Client, AppInfo, ChannelPermissions
File "/Users/pcaires/Desktop/Programação/Python/Discord Bots/venv/lib/python3.7/site-packages/discord/client.py", line 38, in <module>
from .state import ConnectionState
File "/Users/pcaires/Desktop/Programação/Python/Discord Bots/venv/lib/python3.7/site-packages/discord/state.py", line 36, in <module>
from . import utils, compat
File "/Users/pcaires/Desktop/Programação/Python/Discord Bots/venv/lib/python3.7/site-packages/discord/compat.py", line 32
create_task = asyncio.async
^
SyntaxError: invalid syntax
我在互联网上进行了搜索,大多数人都说使用Python 3.7,这就是我一直在使用的语言。另外,我一直在使用PyCharm作为我的Python IDE。
答案 0 :(得分:16)
您使用的discord.py版本不支持Python 3.7 (其中async
成为保留关键字),如this issue中所述。
此版本的discord.py是GitHub repo上的默认分支,可悲的是Pip安装的版本。
您可以:
python3 -m pip install --user -U https://github.com/Rapptz/discord.py/archive/rewrite.zip
答案 1 :(得分:5)
您可以手动编辑文件并将该行从create_task = asyncio.async
更改为create_task = getattr(asyncio, 'async')
答案 2 :(得分:0)
It is only relevant for Python 3.3, which does not include asyncio in its stdlib.