DorianGray
DorianGray

Reputation: 178

How to send an attachment using Python Discord bot

I want my bot to send a file (not necessarily an image, could be a text file) to the channel when called. Here's my code snippet:

@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")

However, it gives me an error: AttributeError: 'Bot' object has no attribute 'send_file' I tried replacing send_file with send as another answer had suggested, but that didn't work either. What should be the correct syntax here?

Upvotes: 3

Views: 9651

Answers (1)

Jawad
Jawad

Reputation: 2041

You have to use the File class:

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

Upvotes: 6

Related Questions