"await message.channel.send()" discord.py 的代码给我语法错误

"await message.channel.send()" code for discord.py giving me syntax error

我正在尝试为 Python 中的 discord 服务器创建一个简单的机器人。现在,开个玩笑,我只是想创建一个“报告”命令,它将整个蜜蜂电影脚本发送给任何键入它的人。 出于某种原因,每当我尝试执行 await message.channel.send(line.strip()) 时,我都会收到“无效语法错误”

有没有人对如何解决这个问题有任何提示?

# bot.py
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('TOKEN')

client = commands.Bot(command_prefix ='$')

def read_lines(file):
    with open(file, 'rb') as f:
        lines = f.readlines()
        for line in lines:
            if line == "\n":
                print("\nEmpty Line\n")
            else:
                await message.channel.send(line.strip())
                return


@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

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

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

    if message.content.startswith('$report'):
        read_lines('bot.txt')


client.run(TOKEN)

P.S:我是 Python 的新手,上述行之后的 return 也给了我一个缩进块问题

那是因为你没有在 read_lines() 函数中指定 messagedef read_lines(file) 中放置另一个参数并使其异步,如下所示: async def read_lines(file, message)read_lines("bot.txt") 像这样添加此消息:await read_lines("bot.txt", message) 并查看它现在是否有效。 最终代码:(编辑还把 from discord.ext import commands 放在最上面)

import os
from discord.ext import commands
import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('TOKEN')

client = commands.Bot(command_prefix ='$')

async def read_lines(file, message):
    with open(file, 'rb') as f:
        lines = f.readlines()
        for line in lines:
            if line == "\n":
                print("\nEmpty Line\n")
            else:
                await message.channel.send(line.strip())
                return


@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

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

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

    if message.content.startswith('$report'):
        await read_lines('bot.txt', message)


client.run(TOKEN)