不断收到语法错误: async def on_ready():

时间:2021-02-04 23:31:57

标签: python discord.py

我正在尝试使用此代码创建一个不和谐的机器人,在 Visual Code Studio 中一切正常,但在终端中出现此错误。我知道 I get a syntax error when I import discord in the cmd but not in vs code 但它没有解决问题,我也提前道歉,我是一个 python 初学者,几乎不知道如何运行代码:

from discord.ext import commands
from utils.logger import Logger

logger = Logger().logger
glados_cores = ["cogs.angry_core"]
bot = commands.Bot(command_prefix="!")
bot.logger = logger


@bot.event
async def on_ready():
    logger.info("---------------bot-ready---------------")
    logger.info("Hello and, again, welcome to the Aperture Science computer-aided enrichment center.")


if __name__ == "__main__":
    for extension in glados_cores:
        try:
            bot.load_extension(extension)
        except Exception as e:
            exc = f"{type(e).__name__}: {e}"
            logger.info(f"{exc} Failed to load extension {extension}")

bot.run(TOKEN)

当我尝试运行代码时,我不断收到此语法错误:

Aleksandars-16-MacBook-Pro:Genetic-Lifeform-and-Disk-Operating-System-master aleksandarvasilic$ python main.py
  File "main.py", line 11
    async def on_ready():
        ^
SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:2)

如果您没有 python 3.5+,就会发生这种情况。如果您在 cmd 中遇到语法错误,可能是因为您的路径上安装了 python <3.5。