Is this unsolvable?

How do I send multiple images in chronological order to a discord channel with a bot.

It’s not just a few images and I can’t send the same images more than once.

I don’t want to randomize them but that seems to be the only code available to handle image uploads where you don’t need to manually input the filenames into the code.

It’s baffling to be honest. The images are scraped from the web like they would be for a randomized image function, so I’m not sure why they can’t just be sent as they come in.

Screen shots with 15% of the code of tried and ideas I had on how to approach it.
I deleted the other 85% in frustration.

I have sent this question to 3 discord and replit related developer help discors servers, friends I have who have been working with python for a couple of years cannot figure it out either (i mean, i don’t know how much time they were able to spend on it).

It’s crickets. I have researched github codes, solutions of stack overflow and so far no luck.

The reason I haven’t given up, is because I know it can be done.
It’s based on the functionality of a bot we use on our server but it’s looking for different content on the web.

I can get the images into replit just fine, I’m just not sure why I can’t send them in chronological order.



Can you elaborate on the problem a little, instead of sharing a story?
What have you tried, what did happen, how did it differ from what you expected to happen?

If you want to preserve a specific order, can’t you just have a queue and a loop, that only progresses once the previous upload returns some kind of success-response?

1 Like

I do believe that I shared screenshots of some of the code I attempted, which includes explanations of what I was trying to do (please let me know of they aren’t showing up).

I will explain in more detail my thinking rather than assuming people will pick it up from my code. We don’t all think the same way so that was shortsighted of me.

I initially thought that I could use the file structure i.e. the date and time structure example in the screenshots.
My thinking was the code could use representative elements rather than specific numbers.

That did not work. Or I could not make it work (maybe someone else can)

Then i thought because the images come in date order, I can just request that the bot send all the photos that had not already been sent by file extension i.e. (jpg, png etc.) and then I’d use a lamda function to sort them by date and time before sending them.

The problem is that there are a lot of images and I’m trying to automate the process of sending them.

What you are suggesting is the plan (i.e. sorting them) but i cannot queue those inages in order without having to put in the exact filename manually.

The filename is the problem. I have not been able to find a way to upload the images without a specific filename.

As mentioned my first attempts were based on trying to create a filename that is representative of the structure not the specific numbers in the filename.
Then I tried just using the file extension and then sorting it chronologically (without doing it manually).

There are 7 people whose images are being gathered from the web.
My friends would type in a specific command followed by the name of the person.

I know the command works because the embed that goes before the images sends when the on_message function is triggered. The components of the embed are static, hence I can just specify a filename.

That is the gist of the problem, how do I upload images without, needing a specific filename.

Thank you.

Without knowing exactly what code is supposed to do, elaborating into the reasoning and thoughts can confuse even more. First post mentioned uploading in order, now comes into play filename. It’s not clear what’s the problem.

Could you give specific example? What are you doing (What is the goal)? What should happen? What happens instead?

You again write a lot of story about your thought process and very little story about what the code is doing. That’s not meant to sound mean, but we want to help your code, not read stories about how many people are working with it or where you gather images from. This makes it really distracting if I want to reference certain aspects but have a wall of text filled with fluff.

Filename? I mean, a file must have a name.
What’s the problem with the name? If you don’t like using that, either use metadata of the image or just have another data-structure track it instead.

Don’t want to change the name of the file? Well make a dictionairy which holds the data as keys and the unchanged files as value.
Grab the keys, sort those and go through the dict to send the files.

Maybe a clarification question I’m curious about might help users here help you out a little easier. What does chronological order mean to you in this context?
Is it the chronological order in which you scraped the images? The datetimes the images were taken? the datetimes they were last modified?

It seems like you can already get images into your server and upload to discord so maybe we don’t focus on that at all. You just need a system to order the images. Correct?

At its simplest form, this means constructing an ordered queue (list) of filenames to traverse. If you have total control over the filenames of every image you could save the desired datetime in the filename when you scrape the images and later order by this. It seems like this is something you’ve already tried, and if so, letting us know why that did not work (preferably by sharing some code) would be helpful.
Perhaps I’m missing something from the code you’ve provided, but so far is seems like there is an assumption that the order of the files in the list you get from the os.listdir method is inherently going to be the chronological order you desire. Maybe, maybe not. I would instead actively order the file list you get back according to your needs.

# Modified code youve already sent us
files_to_send = os.listdir('./agustd')
# ['2021-12-08_20-37-09_UTC.jpg',
# '2021-12-06_20-37-10_UTC.jpg',
# '2021-12-07_20-37-09_UTC.jpg']
# Use some method to chronologically sort the list here
files_to_send.sort()
# ['2021-12-06_20-37-10_UTC.jpg',
# '2021-12-07_20-37-09_UTC.jpg',
# '2021-12-08_20-37-09_UTC.jpg']
for filename in files_to_send:
    filepath = os.path.join('./agustd', filename)
    await message.channel.send(file=discord.File(filepath))

This should send the files in the order they appear in the list (I believe) as the underlying for loop is synchronous. I’m not sure I would use the files kwarg as I don’t know how discord.py is handling the iterable you pass it under-the-hood (it might be asynchronous, so there wouldn’t be a guarantee the items were sent in the same order as the they appear in the iterable).

If you don’t have full control over the naming of files used, relying on the filename for ordering things obviously won’t work. In that case you can still construct an ordered list of filenames, but the method for ordering that list will need to rely on information outside the filename. If this is the case, or ‘chronological order’ here means something more than the order in which you’ve acquired them, you will need to look into the file metadata to construct an ordered queue. I would recommend looking into the Pillow (PIL) library for extracting things like image creation or modification dates (google keyword: EXIF).

I need code that that will go into a folder and send all the images ending in (.jpg) without needing me to write out the file names individually into the code.

I can deal with the sorting code on my own.

Why do you need to write out the file names individually?

That’s the problem Im having. If I don’t write the names out, they won’t be sent.

The code I need has to instruct the bot to, “send all files in this folder” then I can add the conditional statement ‘if they haven’t been sent already’.

I don’t know how to instruct the bot to send all files in a specific folder.

I can only tell it to send a specific file from that folder.

You’ll always need to specify what file is being sent when you do it, but there’s no reason you can’t do it systematically for a folder. What’s stopping you from doing a something like this?

def get_folder_for_user(user: str) -> list:
    ...
def image_already_in_channel(chanel: discord.Channel) -> bool:
    ...
def send_image_to_channel(image_path: str, channel: discord.Channel) -> bool:
    ...
def send_all_images_for_user(user: str, channel: discord.Channel) -> bool:
    images_to_send = get_folder_for_user(user)
    try:
        for image in images_to_send:
            if not image_already_in_channel(channel):
                send_image_to_channel(image, channel)
        return True
    except discord.Some_Exception as e:
        return False
1 Like

Thank you sooo much! This, this is what I needed. This is what I was trying to do but was going about it with a very limited mindset.

I won’t go into details because I talk and write too much as you’ve witnessed, but just know that you’ve made my week and challenged my way of approaching this.

I can’t say how many times this has brought me to tears.

Python is new to me. Up until now, I’d only ever worked with HTML and CSS.

Thank you all so much for taking the time to try and help me.

I know i wasn’t always clear or to the point with my explanations, so I appreciate your patience.