Python 你是如何摆脱“你是怎么做的?”;SyntaxError:';等待&x27;“外部功能”;

Python 你是如何摆脱“你是怎么做的?”;SyntaxError:';等待&x27;“外部功能”;,python,terminal,pycharm,discord,bots,Python,Terminal,Pycharm,Discord,Bots,我正在尝试将discord bot更改为联机状态 我一直在使用pycharm和terminal。我尝试过用多种不同的方法对代码重新排序,但我现在就有了这些方法 import discord from discord.ext import commands client = commands.Bot(command_prefix=".") @client.event async def on_ready(): print('Bot is ready.') await cl

我正在尝试将discord bot更改为联机状态

我一直在使用pycharm和terminal。我尝试过用多种不同的方法对代码重新排序,但我现在就有了这些方法

import discord
from discord.ext import commands
client = commands.Bot(command_prefix=".")
@client.event
async def on_ready():
   print('Bot is ready.')
await client.run(bot token)
我也试过这些:

import discord
from discord.ext import commands
client = commands.Bot(command_prefix=".")
@client.event
async def on_ready():
   print('Bot is ready.')
   await client.run(bot token)
on_ready

我犯了以下错误:

:8:运行时警告:从未等待协同程序“on_ready” RuntimeWarning:启用tracemalloc以获取对象分配回溯

SyntaxError:函数外部的“wait”


请帮助我使机器人联机

打印功能需要另外4个空间:

async def on_ready():
        print("Bot is ready.")

如果问题仍然存在,请尝试安装asyncio

客户端不需要wait函数。run() 放

并将client.run放在文件末尾。在本例中,我使用了一个名为config.py的文件,其中声明了令牌的值

token = "Enter your token here" 
为了导入变量,我使用
import-config导入了配置,因为它在配置文件中,所以我输入了
client.run(config.token)


首先,
await
不能在
异步def
函数之外,等待的内容缩进不足,无法在\u ready
上的函数

其次,您不应该尝试手动调用
on\u ready
,一旦bot运行,它将调用
on\u ready
本身

第三,永远不要把
客户端放进去。在
里面运行
,准备好了吗
而是将其放在文件的末尾,如果确实将其放在on_ready中,它将永远不会运行

因此,这将是理想的代码:

@client.event
_ready()上的异步定义:
打印('Bot已准备就绪!')
client.run(令牌)
至于存储bot令牌,我会把它放在数据库中,比如replit或mongoDB数据库

async def on_ready():
        print("Bot is ready.")
token = "Enter your token here"