NodeJS Discord.js v14 - serverinfo slash command not functioning / string issue?

ldfqzlk8  于 2023-06-22  发布在  Node.js
关注(0)|答案(2)|浏览(97)

我正在为我的discord bot构建一个serverinfo斜杠命令,但是我遇到了这个错误,这可能是由于我的代码的.addFields部分。以下是我在测试命令时得到的错误:

CombinedPropertyError (5)
  Received one or more errors

  input[1]
  | CombinedPropertyError (1)
  |   Received one or more errors
  | 
  |   input.value
  |   | ValidationError > s.string
  |   |   Expected a string primitive
  |   | 
  |   |   Received:
  |   |   | 16

  input[2]
  | CombinedPropertyError (1)
  |   Received one or more errors
  | 
  |   input.value
  |   | ValidationError > s.string
  |   |   Expected a string primitive
  |   | 
  |   |   Received:
  |   |   | 1

  input[3]
  | CombinedPropertyError (1)
  |   Received one or more errors
  | 
  |   input.value
  |   | ValidationError > s.string
  |   |   Expected a string primitive
  |   | 
  |   |   Received:
  |   |   | 5

  input[4]
  | CombinedPropertyError (1)
  |   Received one or more errors
  | 
  |   input.value
  |   | ValidationError > s.string
  |   |   Expected a string primitive
  |   | 
  |   |   Received:
  |   |   | 2

  input[5]
  | CombinedPropertyError (1)
  |   Received one or more errors
  | 
  |   input.value
  |   | ValidationError > s.string
  |   |   Expected a string primitive
  |   | 
  |   |   Received:
  |   |   | 2

    at ArrayValidator.handle (/home/runner/tester/node_modules/@sapphire/shapeshift/dist/index.js:468:70)
    at ArrayValidator.parse (/home/runner/tester/node_modules/@sapphire/shapeshift/dist/index.js:207:88)
    at EmbedBuilder.addFields (/home/runner/tester/node_modules/@discordjs/builders/dist/index.js:222:31)
    at Object.execute (/home/runner/tester/commands/info/serverinfo.js:21:8)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

我不知道这会是什么。下面是我写的代码:

const {
  SlashCommandBuilder,
  EmbedBuilder
} = require("discord.js");

  module.exports = {
    data: new SlashCommandBuilder()
      .setName("serverinfo")
      .setDescription("Provides information about the server.")
      .setDMPermission(false),
  
    async execute(client, interaction) {
      const { guild } = interaction;
      const { members, channels, emojis, roles, stickers } = guild;
  
      const embed = new EmbedBuilder()
        .setColor("#2B2D31")
        .setTitle(`${guild.name} - Server info`)
        .setThumbnail(guild.iconURL({ size: 256 }))
        .setImage(guild.bannerURL({ size: 256 }))
        .addFields(
          { name: "Owner", value: (await guild.fetchOwner()).user.tag, inline: true },
          { name: "Text Channels", value: guild.channels.cache.filter((c) => c.type === 0).toJSON().length, inline: true },
          { name: "Voice Channels", value: guild.channels.cache.filter((c) => c.type === 2).toJSON().length, inline: true },
          { name: "Category Channels", value: guild.channels.cache.filter((c) => c.type === 4).toJSON().length, inline: true },
          { name: "Members", value: guild.memberCount, inline: true },
          { name: "Roles", value: guild.roles.cache.size, inline: true },
          { name: "Role List", value: guild.roles.cache.toJSON().join(', ')},
        );
      interaction.reply({ embeds: [embed] });
    },
  };

有什么想法吗?先谢了

ef1yzkbh

ef1yzkbh1#

这个错误很简单。字段值应该是字符串原语,但您提供数字。所有这些.length属性,guild.memberCountcache.size都是数字。
要解决这个问题,您需要将它们转换为字符串。此外,您不需要使用toJSON(),因为filter()返回一个集合,您可以使用size属性:

const embed = new EmbedBuilder()
  .setColor('#2B2D31')
  .setTitle(`${guild.name} - Server info`)
  .setThumbnail(guild.iconURL({ size: 256 }))
  .setImage(guild.bannerURL({ size: 256 }))
  .addFields(
    { name: 'Owner', value: (await guild.fetchOwner()).user.tag, inline: true },
    {
      name: 'Text Channels',
      value: guild.channels.cache.filter((c) => c.type === 0).size.toString(),
      inline: true,
    },
    {
      name: 'Voice Channels',
      value: guild.channels.cache.filter((c) => c.type === 2).size.toString(),
      inline: true,
    },
    {
      name: 'Category Channels',
      value: guild.channels.cache.filter((c) => c.type === 4).size.toString(),
      inline: true,
    },
    { name: 'Members', value: guild.memberCount.toString(), inline: true },
    { name: 'Roles', value: guild.roles.cache.size.toString(), inline: true },
    { name: 'Role List', value: guild.roles.cache.toJSON().join(', ') },
  );

如果你喜欢的话,你也可以这样使用模板文字:

{
  name: 'Text Channels',
  value: `${guild.channels.cache.filter((c) => c.type === 0).size}`,
  inline: true,
},
// ...
gstyhher

gstyhher2#

.addFields方法使用一个对象数组。你可能忘了你的方括号。只需将它们添加到以下位置:

.addFields( // <- Here
          { name: "Owner", value: (await guild.fetchOwner()).user.tag, inline: true },
          { name: "Text Channels", value: guild.channels.cache.filter((c) => c.type === 0).toJSON().length, inline: true },
          { name: "Voice Channels", value: guild.channels.cache.filter((c) => c.type === 2).toJSON().length, inline: true },
          { name: "Category Channels", value: guild.channels.cache.filter((c) => c.type === 4).toJSON().length, inline: true },
          { name: "Members", value: guild.memberCount, inline: true },
          { name: "Roles", value: guild.roles.cache.size, inline: true },
          { name: "Role List", value: guild.roles.cache.toJSON().join(', ')},
        ); // <- Here too

这里是固定的:

.addFields([ // <- Here
          { name: "Owner", value: (await guild.fetchOwner()).user.tag, inline: true },
          { name: "Text Channels", value: guild.channels.cache.filter((c) => c.type === 0).toJSON().length, inline: true },
          { name: "Voice Channels", value: guild.channels.cache.filter((c) => c.type === 2).toJSON().length, inline: true },
          { name: "Category Channels", value: guild.channels.cache.filter((c) => c.type === 4).toJSON().length, inline: true },
          { name: "Members", value: guild.memberCount, inline: true },
          { name: "Roles", value: guild.roles.cache.size, inline: true },
          { name: "Role List", value: guild.roles.cache.toJSON().join(', ')},
        ]); // <- Here too

最简单的错误往往是我们犯的错误。
如果这个问题没有解决,请在下面发表评论。快乐编码!

相关问题