Hershey Gamer
Hershey Gamer

Reputation: 147

Discord.js looking for nonexistent directory

I'm writing a Discord Bot with Discord.js and I'm making the bot send a random image from a set:

client.on('message', msg => {
   if (msg.content === 'I Hate It'){
       const randImg = ['https://i.imgur.com/C0gR27R.png', 'https://i.imgur.com/qErVhIm.jpg'];
       msg.channel.send("", { files: randImg[Math.floor(Math.random() * randImg.length)]});
   } 
});

However when I run the code and type "I Hate It", nodemon gives me this error:

(node:13) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, stat '/home/container/h'
(node:13) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

According to the error, It's trying to look for a directory that doesn't exist but nowhere in the code is it told to look for a directory. I know that it's that particular block because all other commands and part of the bot work without error. How do I fix this?

Upvotes: 2

Views: 111

Answers (2)

Gabriel Andrade
Gabriel Andrade

Reputation: 728

The 'files' propriety of the MessageOptions object is an array. Maybe, adding the arrays brackets should solve your issue.

client.on('message', msg => {
   if (msg.content === 'I Hate It'){
       const randImg = ['https://i.imgur.com/C0gR27R.png', 'https://i.imgur.com/qErVhIm.jpg'];
       msg.channel.send("", {
         files: [ // bracket
           randImg[Math.floor(Math.random() * randImg.length)]
         ] // bracket
       });
   } 
});

Helpful links:

MessageOptions | discord.js

TextChannel#send | discord.js

Upvotes: 0

Octagonal T
Octagonal T

Reputation: 404

You don't need to send a file, since it's not on your device, you can just send the link. Discord will turn the imgur link into a photo

msg.channel.send(randImg[Math.floor(Math.random() * randImg.length)])

Upvotes: 1

Related Questions