You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js v14中channel.createWebhook触发ENOENT文件不存在错误求助

解决Discord.js v14中createWebhook触发的ENOENT文件不存在错误

问题根源

你传给avatar参数的${message.author.avatar}是用户头像的哈希值,Discord.js会误把这个哈希值当作本地文件路径去读取,自然找不到对应文件,触发ENOENT错误。

修复方案

把avatar参数替换为用户头像的完整CDN URL,两种可行方式:

方式一:使用displayAvatarURL()(推荐)

这个方法直接返回用户头像的完整URL,还能指定格式和尺寸:

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

方式二:手动拼接CDN链接

如果需要自定义CDN路径,可通过头像哈希值拼接:

// 处理用户无自定义头像的情况
const avatarURL = message.author.avatar 
  ? `https://cdn.discordapp.com/avatars/${message.author.id}/${message.author.avatar}.png`
  : null;

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

额外提示

  • 注意message.author.name在Discord.js v14中已被message.author.username替代,用旧属性可能获取不到正确用户名
  • 必须处理用户无自定义头像的场景,此时message.author.avatar为null,避免传入无效值

内容的提问来源于stack exchange,提问作者rileyhopkins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 02:36:01