1

I've been trying to make my discord.py bot post a random_image from a local dictionary upon called with the command !meme. But something goes wrong in the process and returns me the error message:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\viodo\PycharmProjects\pythonProject\venv\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\viodo\PycharmProjects\pythonProject\bot.py", line 43, in on_message
    await message.channel.send(file=discord.File(random_image))
  File "C:\Users\viodo\PycharmProjects\pythonProject\venv\lib\site-packages\discord\file.py", line 73, in __init__
    self.fp = open(fp, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '3b202f6.jpg'

The code in question:

import discord
from discord.ext import commands
import random
import os
client = commands.Bot(command_prefix = ".")

@client.event
async def on_ready():
    print("Bot is in the HOUSE!")

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

#__________________________________________________________________________
    random_quotes = [
        ("I'm a bot, bing bong"),
        ('Hej'),
        ("Hello"),
        ("Yes yes yes yes yes"),
    ]

    if message.content == '!bot':
        response = random.choice(random_quotes)
        await message.channel.send(response)
#_________________________________________________________________________
    specified_meme = [r"C:\Users\viodo\memes\Scotland icetruck salter GPS.png"]

    if message.content == '!meme2':
        random_meme = random.choice(specified_meme)
        await message.channel.send(file=discord.File(random_meme))
#__________________________________________________________________________

    meme = []
    for root, dirs, files in os.walk(r'C:\Users\viodo\memes'):
        for file in files:
            if file.endswith('.jpg'):
                meme.append(file)
    random_image = random.choice(meme)

    if message.content == "!meme":
        await message.channel.send(file=discord.File(random_image))

client.run("xxxx")

The thing is it seems from what I can see that it does find a file since it displays the file 3b202f6.jpg in the error message which is a picture from the specified directory. How can that be... I tried isolating the image picking code and it ran smoothly, outputting a random image in the terminal so that just confused me even more. To make matters worse, it all worked beautifuly yesterday for about half an'hour and then the error came along for no apparent reason.

Just to be sure the dictionary wasnt the problem or locating it, I made another line of code that when called upon !meme2 picked a specified image Scotland icetruck salter GPS.png from sat dictionary and it worked like it's supposed to do.

VioDor
  • 13
  • 1
  • 4
  • I believe your problem is that you are trying to open the file simply by its file name, when you should be using the full path to the file? This post might have the answer to your question: https://stackoverflow.com/a/16465439/13042738 – Jason Rebelo Neves Feb 26 '21 at 14:14

1 Answers1

0

You can't upload an image from your computer. You have to use a link. Instead of using sites to do this, simply paste the image you want in discord. Then right click it and press 'Copy Link'. Then use this link as the image in the message. I'd also recommend using @client.command() because it's easier, faster, and cleaner.

import discord
from discord.ext import commands

token = ""

client = commands.Bot(command_prefix = "Whatever you want")

@client.event
async def on_ready():
    print("Bot is online")

@client.command()
async def image(ctx):
    await ctx.send("image link")

client.run(token)

The link from discord will look something like this: https://media.discordapp.net/attachments/xxxxxxxxx/xxxxxxxxx/unknown.png (file extension may be different)

Kai
  • 115
  • 1
  • 2
  • 7
  • Thanks for your input! But wouldnt that mean I would have to post every image (protentially hundreds) manually and copy the link since I want to post a random image from a collection? Or am I looking at this the wrong way? – VioDor Mar 01 '21 at 13:03
  • Your best bet would be to use a website that can get links for lots of images at a time. On `https://imgbb.com/` you can host images with a 32mb limit. I'd try that, otherwise possibly look for another method. Good luck – Kai Mar 01 '21 at 21:59