0

I am currently programming a Discord Bot for a friend. I have a new PC and now the following error occured after using my "!meme" command. It worked on my old Laptop, but i dont have it anymore...

Code for the command:

elif '!meme' in message.content:
    random_message_i = ["Here you go:", "Classic one:", "This one is good:", "lol xD:"]
    await message.channel.send(f"{random.choice(random_message_i)}")
    meme_file_paths = [os.path.abspath(_) for _ in glob.glob("botmemes/*.png")]
    random_meme_i = random.choice(meme_file_paths)
    await message.channel.send(file=discord.File(random_meme_i))
    print("[INFO] Last command: !meme")

Error:

   Traceback (most recent call last):
      File "C:\Users\cxyro\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 409, in _run_event
        await coro(*args, **kwargs)
      File "c:\Users\cxyro\Desktop\DiscordBot\discordbot.py", line 103, in on_message
        random_meme_i = random.choice(meme_file_paths)
      File "C:\Users\cxyro\AppData\Local\Programs\Python\Python310\lib\random.py", line 378, in choice
        return seq[self._randbelow(len(seq))]
    IndexError: list index out of range

(Yes, the code ant the memes are in the same main folder...)

Can somebody help me?

3
  • 1
    Try print(meme_file_paths)…!? Commented Oct 12, 2022 at 18:50
  • I tried it, the same error occured :( Commented Oct 12, 2022 at 18:55
  • 2
    @cxyro You will get exactly that error if meme_file_paths is an empty list. That was the point of the previous comment. Commented Oct 12, 2022 at 19:49

1 Answer 1

1

As far as, I can see , it occurs when you pass an empty list to choice() function. (in case, file not found) P.S :- Make sure, you are executing that script within the same directory where your code and meme is kept. otherwise, your current working directory gets changed and your code won't be able to find the meme path. you can verify this by writing this code in a print statement in your script:

import os; os.getcwd()

Sign up to request clarification or add additional context in comments.

5 Comments

Hey, i now have a new Error: Traceback (most recent call last): File "C:\Users\cxyro\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 409, in run_event await coro(*args, **kwargs) File "c:\Users\cxyro\Desktop\DiscordBot\discordbot.py", line 105, in on_message meme_file_paths = [os.path.abspath() for _ in glob.glob("memes/*.png")] AttributeError: 'function' object has no attribute 'glob'
Do you know how i can fix that?
import discord import asyncio import time from glob import glob import random import os; os.getcwd() from difflib import SequenceMatcher This is what i import
You don’t need to write glob.glob, as you have already imported glob function from glob module.
So just call glob() function wherever you need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.