Not able to send an image attached to a message on discordjs via an electron application

Viewed 886

I'm trying to build a graphic interface for a discord bot I wrote using discordjs. However, I encountered a problem with sending image.

This is the part of my code responsible for sending a message to a user :

utilsateur.createDM().then(channeltemp => {
    let attachment = new Discord.MessageAttachment('./image.png');
    channelDM = channeltemp;
    channelDM.send("message", attachment);
});

The image "image.png" is located in the same folder as the script. I've been running my script on nodejs without any problem, however when I tried to run the script via the html page of my electron app, I'm getting this error :

Uncaught (in promise) TypeError [REQ_RESOURCE_TYPE]: The resource must be a string, Buffer or a valid file stream.

I think it has nothing to do with pathing because I tried changing my path (absolute, relative, ...) without any success and moreover, I'm able to access some data in an excel in my electron app.

Does anyone have a clue about how to solve my problem ?

I'm sorry if I made some mistakes about technical terms (I'm a beginner) or even grammatical mistakes as English isn't my native language.

Appreciate your help ! :)

2 Answers

I am not really sure where the problem is, either it's on the declaration of attachment or channelDm.send. But according to my recent discussion with some people in my Discord server, It would have been caused by not giving the whole path of the local data, which in your case, you're giving a relative path of ./image.png, assuming the error was in the declaration of attachment.

You might want to try putting full image path on the first argument of MessageAttachment constructor if you're using NodeJS.

const path = require('path');

const fileName = 'image.png';

utilsateur.createDM().then(channeltemp => {
    let attachment = new Discord.MessageAttachment(path.join(__dirname, fileName));
    channelDM = channeltemp;
    channelDM.send("message", attachment);
});

__dirname is, quoting from NodeJS documentation:

<string>

The directory name of the current module. This is the same as the path.dirname() of the __filename.

I hope this both answered the question and fixes your problem. I have to apologize in advance if it doesn't, I'm also a beginner and currently just started using DiscordJS after migrating from Discord.py.

I'm just gonna edit to say that I solve my problem by splitting my code in half and executing one outside the electron app using a cmd command.

Thanks for helping !

Related