如何使用Python Discord bot发送附件

如何使用Python Discord bot发送附件,python,python-3.x,discord.py,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") 但是,它

我希望我的机器人在被调用时向频道发送一个文件(不一定是图像,可以是文本文件)。以下是我的代码片段:

@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")
但是,它给了我一个错误:
AttributeError:“Bot”对象没有属性“send\u file”
我试着按照另一个答案的建议,用
send
替换
send\u文件
,但也没用。这里的正确语法应该是什么?

您必须使用该类:


你好你的回答完全符合我的要求。但是,当我尝试创建类似类型的on_消息事件时,它抛出了一个“unhandled message”错误。你能帮忙吗?我的事件代码片段:@bot.event
async def on_message(message):wait bot.process_命令(message)if message.content[0]==“@”:file=discord.file(“filepath”)wait message.channel.send(file=file,content=“message to sent”)
很抱歉,不确定注释中的代码格式:(,希望您能阅读。
File
是一个类,请确保将其大写。您的错误在以下行:
File=discord.File(“文件路径”)
,将其更改为:
File=discord.File(“文件路径”)
@bot.command()
async def send(ctx):
    file = discord.File("myfilepath")
    await ctx.send(file=file, content="Message to be sent")