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.