Reputation: 23
I am trying to make a Discord Bot with discord.js but every time I try to run the command, it returns with an error saying:
UnhandledPromiseRejectionWarning: Error: ENOENT, No such file or directory './background.jpg'
This is my code:
const Discord = require('discord.js');
const Canvas = require('canvas')
module.exports = {
name: 'level',
description: 'Level command.',
execute: async (message, client) => {
const canvas = Canvas.createCanvas(700, 250);
const ctx = canvas.getContext('2d')
const background = await Canvas.loadImage('./background.jpg')
ctx.drawImage(background, 0, 0, canvas.width, canvas.height)
const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'image.png')
message.channel.send(attachment)
}
}
Image of my files and folders:
Upvotes: 1
Views: 807
Reputation: 23160
It seems your image file is in the same folder. Maybe try using absolute paths instead. You could use the path
module's resolve()
method to resolve the __dirname
and the filename into an absolute path. Using it like this will work:
const Discord = require('discord.js');
const Canvas = require('canvas');
const path = require('path');
module.exports = {
name: 'level',
description: 'Level command.',
execute: async (message, client) => {
const canvas = Canvas.createCanvas(700, 250);
const ctx = canvas.getContext('2d');
const background = await Canvas.loadImage(
path.resolve(__dirname, './background.jpg'),
);
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
const attachment = new Discord.MessageAttachment(
canvas.toBuffer(),
'image.png',
);
message.channel.send(attachment);
},
};
Upvotes: 1