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

Discord.js v13.6.0创建服务器邀请报错:Cannot read properties of undefined (reading 'createInvite')

Discord.js v13.6.0 guildCreate事件创建服务器邀请报错解决

错误信息

Cannot read properties of undefined (reading 'createInvite')

原错误代码

client.on("guildCreate", message => {

  let invite = message.channel.createInvite(
     {
       maxAge: 10 * 60 * 1000,
       maxUses: 0
     }
  )

  console.log(`${invite}`);
})

问题原因

guildCreate事件触发时,传入的参数是服务器对象(Guild),而非消息对象(Message)。原代码错误地使用message作为参数,导致message.channel为undefined,无法调用createInvite方法。

修正后的代码

需要将参数改为guild,并且由于createInvite是异步方法,要通过async/await或者.then()处理Promise,才能获取到实际的邀请链接:

client.on("guildCreate", async guild => {
  // 筛选服务器中的第一个文本频道
  const channel = guild.channels.cache.find(ch => ch.type === "GUILD_TEXT");
  if (!channel) return;

  try {
    const invite = await channel.createInvite({
      maxAge: 10 * 60 * 1000, // 10分钟后过期
      maxUses: 0 // 无使用次数限制
    });
    console.log(`服务器邀请链接:${invite.url}`);
  } catch (error) {
    console.error("创建邀请失败:", error);
  }
})

说明:额外增加了频道查找逻辑和错误捕获,避免因找不到可创建邀请的频道而报错,同时正确处理异步操作以获取真实的邀请链接。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 00:59:50