Discord v13 API调用.createInvite方法触发Null Error报错求助
问题原因
从你提供的Guild对象返回值可以看到,systemChannelId字段为null,说明你的Discord服务器未设置系统频道,因此Guild.systemChannel返回值为null,调用null的createInvite方法就会触发你遇到的类型错误。
解决方案
- 方案1:进入你的Discord服务器设置,在「社区」板块下设置系统频道,配置完成后原有代码即可正常运行
- 方案2:无需修改服务器配置,指定其他有权限的文字频道生成邀请,参考代码如下:
const Guild = await client.guilds.fetch(guildId); // 优先使用系统频道,无系统频道则自动查找第一个机器人可创建邀请的文字频道 const targetChannel = Guild.systemChannel ?? Guild.channels.cache.find(channel => channel.type === 0 // 0代表文字频道,如使用discord.js v14+可替换为ChannelType.GuildText && channel.permissionsFor(Guild.members.me).has('CreateInstantInvite') ); if (!targetChannel) { throw new Error('未找到可生成邀请的有效频道,请确认机器人拥有创建邀请权限'); } const invite = await targetChannel.createInvite({ maxAge: 1209600, // 14天对应的秒数,原参数14仅代表14秒有效期 maxUses: 10, unique: true });
额外提示:你原有代码中的maxAge参数单位为秒,若你确实需要14天的有效期,请将参数值修改为1209600
内容的提问来源于stack exchange,提问作者Rashid Osman
相关产品推荐
相关产品推荐

