Cri
Cri

Reputation: 95

Discord.py attach image from bot

I making a bot that sends a random photo of Will Smith from a folder in my Downloads folder thats called willy. I dont have the random part implemented but I'm trying to have the bot send an image in the willy folder called 0aPw0J4.png. I just want the script to be able to find the image and upload it. Currently the error is that python cant find the directory even though I tried to list the directory starting from my C: drive but that didnt work so I deleted it.

import discord
from discord.ext import commands



client = commands.Bot(command_prefix = "!_")


@client.event
async def on_ready():
    print('bots online')

@client.command()
async def will(ctx):
    await ctx.send('will')



@client.event
async def on_message(message):
    if message.content.startswith('!_will'):
        embed = discord.Embed(title="A photo of Will Smith", description="Here he is", color=0x00ff00)
        embed.add_field(name="Field2", value="hi2", inline=False)
        embed.set_image(url="https://discordapp.com/assets/e4923594e694a21542a489471ecffa50.svg")
        await message.channel.send(embed=embed)
        await client.send_file(channel, "0aPw0J4.png", content="...", filename="...")

@client.event
async def on_message(message):
    if message.content.startswith('!willy'):
        file = discord.File("0aPw0J4.png", filename="0aPw0J4.png")
        await message.channel.send("0aPw0J4.png", file=file)


client.run('my bot token hidden')

I tried changing my default directory but that didnt work either.

Upvotes: 3

Views: 4893

Answers (1)

marcos
marcos

Reputation: 4510

Probably it's a working directory problem, always try to reference the file with an absolute path in cases like this. Use the os module to see if it can find the folder with the images:

import os
# it will print all files in that folder
print(os.listdir('folder path of the images'))

# it will print True of False if the file exists with this path
print(os.path.isfile('absolute path of the image'))

Upvotes: 1

Related Questions