TypeError:无法读取 null 的属性(读取“公会”)

编程入门 行业动态 更新时间:2024-10-04 11:20:43

TypeError:无法读取 null 的属性(读取“<a href=https://www.elefans.com/category/jswz/34/1756487.html style=公会”)"/>

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 的属性(读取“公会”)

本文发布于:2024-05-30 18:53:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770810.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:公会   属性   TypeError   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!