1

I am creating a discord bot (irrelevent) that sends images into the chat. The user can type out the name of the image without needing to type the file extention. The problem is that the bot doesn't know what the file extention is so it will crash if the picture is a .jpg and the program was expecting a .png. Is there a way to make the program not require a file extention to open the file?

let image = imageName;
message.channel.send({ files: [`media/stickers/${imageName}.png`] });
6
  • how will it open the file if it doesn't know the full file name? the "extension" is part of the filename Commented Mar 9, 2022 at 4:57
  • @Bravo that's my question. I need it to open even if it doesn't know the extension. Commented Mar 9, 2022 at 4:59
  • 2
    do these files exist on your server? you can use fs functions to read the folder (readdir) then find the file whose name matches the given filename part - then you'll have the extension part Commented Mar 9, 2022 at 5:03
  • @Bravo I've never heard of that before, but that sounds like what I need. Thanks for your help! Commented Mar 9, 2022 at 5:05
  • if you use the "promise" version of fs you can find it by const file = (await readdir(folder)).find(f => f.split('.').slice(0,-1).join('.') === target); where folder is the folder the file is in, and target is the filename without the the extension Commented Mar 9, 2022 at 5:15

3 Answers 3

1

Unfortunately, the extension of the filename is required. You know file.mp4 and file.mp3 is entirely different.

However, you can use a try-except and a for loop to get the correct file!

I would suggest:

let image = imageName;
let extensions = [".png", ".jpg", "gif"] // All the extensions you can think of 
const pass = () => {}
for (const extension of extensions) {
  try {
     message.channel.send({ files: [`media/stickers/${imageName}${extension}`] }); // successfully get file and send
     break
  } catch(error) {
     pass() // do nothing, and go back to the loop and test other extension
  }
}

I haven't tried that before, and I am a Python programmer. But I hope you get the idea.

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

Comments

1

Using fs - specifically the Promise version of fs, makes this quite simple

import { readdir } from 'fs/promises';


const getFullname = async (path, target)  => 
    (await readdir(path))
    .find(file => 
        file === target || file.split('.').slice(0,-1).join('.') === target
    );
try {
    const actualName = await getExtension('media/stickers', imageName);
    if (!actualName) {
        throw `File ${imageName} not found`;
    }
    message.channel.send({ files: [`media/stickers/${actualName}`] });
} catch(error) {
    // handle your errors here
}

You can pass in the name with or without the extension and it will be found - note, this is NOT case insensitive ... so XYZ won't match xyz.jpg - easily changed if you need case insensitivity

Comments

-1

There are only a few known image extensions like jpg, png, gif, jpeg. Maybe try and fetch the file with best guess extension, if it throws exception try the next format.

Comments

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.