公会”)"/>
TypeError:无法读取 null 的属性(读取“公会”)
这是我的代码:
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] });
},
};
我想向机器人已经加入的特定频道和服务器发送嵌入消息。问题是在这一行中
const server = await interaction.client.guilds.fetch(serverId);
它在控制台中返回了我的这个错误:
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')
接受任何答案。提前谢谢你。
注意:
这两篇文章 1) 和 2) 似乎都没有解决我的问题!
回答如下:您的问题将来自这些行。文件顶部的 SlashCommandBuilder 没有选项,只有名称和描述,因此没有可获取的选项。因此
serverId
未定义,当通过 fetch
时,它会抛出错误。
// Get the server and channel IDs from the options
const serverId = interaction.options.get('845347382206922782'); // not a thing
const channelId = interaction.options.get('1103660151040397394'); // not a thing
// Fetch the server and channel objects
const server = await interaction.client.guilds.fetch(serverId); // serverId undefined
const channel = server.channels.cache.get(channelId); // channelId undefined
删除中间地带,将硬编码数据直接输入
guilds.fetch
和cache.get
。例如:
const server = await interaction.client.guilds.fetch("845347382206922782");
const channel = server.channels.cache.get("1103660151040397394");
希望这有帮助!
更多推荐
TypeError:无法读取 null 的属性(读取“公会”)
发布评论