javascriptdiscord.jsbotswebhooks

channel.createWebhook causes 'no such file or directory' error


I am creating a bot in Discord.js v14, I am trying to make a simple modmail bot, but when attempting to create the webhook, I get a very strange error which doesn't look like it has any connection with creating a webhook, but when I remove the function, it works completely fine.

I have tried every combination of this function, as it is the only way to create webhooks

const webhook = await channel.createWebhook({
  name: `${message.author.name} (${message.author.id})`,
  avatar: `${message.author.avatar}`,
});

And the error I always recieve is:

Error: ENOENT: no such file or directory, stat 'C:\Users\riley\Documents\GitHub\advanced-modmail\c43c7fdd43c8315ddf7a816a1f25a85c'
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:397:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:84:21) {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'stat',
  path: 'C:\\Users\\riley\\Documents\\GitHub\\advanced-modmail\\c43c7fdd43c8315ddf7a816a1f25a85c'
}

the ending random gibberish changes each time


Solution

  • You are facing this error be because the bot attempting to use a file path for the webhook avatar which does not exist. The moment you passes message.author.avatar, it might be excepting a file path rather than an URL.

    In order to overcome with this you can pass the URL of the avatar. Follow the below code snippet:

    const webhook = await channel.createWebhook({
        name: `${message.author.username} (${message.author.id})`,
        avatar: message.author.displayAvatarURL({ format: 'png' }),
    });