如何使用 Python Discord 机器人发送附件

Dor*_*ray 1 python python-3.x discord.py

我希望我的机器人在调用时将文件(不一定是图像,可以是文本文件)发送到通道。这是我的代码片段:

@bot.command(pass_context=True)
async def send(ctx):
    area=ctx.message.channel
    await bot.send_file(area, r"c:\location\of\the_file_to\send.png",content="Message test")
Run Code Online (Sandbox Code Playgroud)

但是,它给了我一个错误: AttributeError: 'Bot' object has no attribute 'send_file' 我尝试按照另一个答案的建议替换send_filesend,但这也不起作用。这里正确的语法应该是什么?

Jaw*_*wad 5

你必须使用该类File

@bot.command()
async def send(ctx):
    file = discord.File("myfilepath")
    await ctx.send(file=file, content="Message to be sent")
Run Code Online (Sandbox Code Playgroud)