Discord.js V14报错:TypeError: 无法读取null的'guild'属性
Discord.js V14 斜杠命令发送Embed报错求助
我用Discord.js V14编写斜杠命令,实现模态框交互后,将Embed消息发送到机器人已加入的特定服务器频道。但执行到const server = await interaction.client.guilds.fetch(serverId);时触发报错:
TypeError: Cannot read properties of null (reading 'guild')
相关代码
const { SlashCommandBuilder } = require('@discordjs/builders'); const { EmbedBuilder, TextInputStyle, ModalBuilder, TextInputBuilder, ActionRowBuilder } = require('discord.js'); module.exports = { data: new SlashCommandBuilder() .setName('experemental') .setDescription('Apply for Experemental Programme.'), async execute(interaction) { const modal = new ModalBuilder() .setCustomId('modal') .setTitle(`Apply for Experimental Programme`); const questionModal = new TextInputBuilder() .setCustomId('questionModal') .setLabel('Your Discord Username.') .setStyle(TextInputStyle.Short); const firstActionRow = new ActionRowBuilder().addComponents(questionModal); modal.addComponents(firstActionRow); await interaction.showModal(modal); const modalResponse = await interaction.awaitModalSubmit({ filter: (i) => i.customId === 'modal' && i.user.id === interaction.user.id, time: 0, }); const description = modalResponse.fields.getTextInputValue('questionModal'); // Get the server and channel IDs from the options const serverId = interaction.options.get('845347382206922782'); const channelId = interaction.options.get('1103660151040397394'); // Fetch the server and channel objects const server = await interaction.client.guilds.fetch(serverId); const channel = server.channels.cache.get(channelId); // Send the embed message to the channel const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Hello!') .setDescription(`Hello! ${description}`); await channel.send({ embeds: [embed] }); }, };
完整报错信息
C:\---\---\---\node_modules\discord.js\src\managers\GuildManager.js:264 const id = this.resolveId(options) ?? this.resolveId(options.guild); ^ TypeError: Cannot read properties of null (reading 'guild')
已参考相关文章但未解决问题,寻求帮助。
问题原因及解决方案
核心原因
interaction.options.get('845347382206922782')用法错误:
interaction.options.get()是获取斜杠命令定义中声明的选项名称对应的值,不是直接传入服务器/频道ID。- 你的斜杠命令
experemental未定义任何选项,所以这两行代码返回null,将null传入guilds.fetch()触发报错。
解决方案
根据需求选择以下方式之一:
方式1:固定服务器和频道ID(适合目标频道固定场景)
直接将ID以字符串常量赋值,无需通过interaction.options获取:
// 替换原获取ID的代码 const serverId = '845347382206922782'; const channelId = '1103660151040397394';
方式2:动态传入频道ID(适合用户选择目标频道场景)
先在斜杠命令中添加频道选项,再获取用户选择的频道:
- 修改命令定义,添加频道选择选项:
data: new SlashCommandBuilder() .setName('experemental') .setDescription('Apply for Experemental Programme.') .addChannelOption(option => option.setName('target-channel') .setDescription('选择要发送Embed的频道') .setRequired(true) ),
- 修改获取频道的代码:
// 替换原获取服务器和频道的代码 const channel = interaction.options.getChannel('target-channel'); // 直接发送Embed const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Hello!') .setDescription(`Hello! ${description}`); await channel.send({ embeds: [embed] });
额外注意事项
- 确保机器人已加入目标服务器,且拥有
SendMessages等必要权限。 - 建议添加错误捕获,避免程序崩溃:
try { const server = await interaction.client.guilds.fetch(serverId); const channel = server.channels.cache.get(channelId); if (!channel) throw new Error('目标频道不存在'); await channel.send({ embeds: [embed] }); } catch (error) { console.error('发送消息失败:', error); await modalResponse.reply({ content: '发送申请失败,请联系管理员', ephemeral: true }); }
内容的提问来源于stack exchange,提问作者Illanser
相关产品推荐
相关产品推荐

