Discord.js 嵌入中的预期约束错误 - ES6 模块

ExpectedConstraintError In Discord.js Embed - ES6 Module

提问人:RealCraze 提问时间:11/16/2023 更新时间:11/17/2023 访问量:17

问:

我正在开发一个 Discord 机器人,该机器人具有列出公会中任何用户的权限的功能。只要用户具有“跟踪”权限之一,代码就可以完美运行。

但是,当用户没有权限(正在“跟踪”)时,它应该说“没有特殊权限”,但相反,我收到多个错误。

请注意,我在尝试解决问题时确实添加了调试语句。

代码片段:

if (interaction.commandName === 'user') {

    let manageUser = interaction.options.getUser('target')

    const manageMember = interaction.guild.members.cache.get(manageUser.id);

    const accountDate = manageUser.createdAt;

    const joinDate = interaction.guild.members.cache.get(manageUser.id).joinedAt;

    const joinDateTimestamp = Math.floor(joinDate.getTime() / 1000);

    const accountAgeTimestamp = Math.floor(accountDate.getTime() / 1000);

    const roles = interaction.guild.members.cache.get(manageUser.id).roles.cache
      .filter(role => role.name !== '@everyone')
      .map(role => `<@&${role.id}>`).join('\n');

    const numberOfRoles = roles.split('\n').length;

    const trackedPermissions = [
      PermissionsBitField.Flags.Administrator,
      PermissionsBitField.Flags.ManageGuild,
      PermissionsBitField.Flags.ManageRoles,
      PermissionsBitField.Flags.ManageChannels,
      PermissionsBitField.Flags.ManageWebhooks,
      PermissionsBitField.Flags.ManageGuildExpressions,
      PermissionsBitField.Flags.ManageNicknames,
      PermissionsBitField.Flags.MentionEveryone,
      PermissionsBitField.Flags.BanMembers,
      PermissionsBitField.Flags.KickMembers,
      PermissionsBitField.Flags.ModerateMembers,
      PermissionsBitField.Flags.ManageMessages,
      PermissionsBitField.Flags.ManageThreads,
      PermissionsBitField.Flags.ManageEvents,
      PermissionsBitField.Flags.ViewAuditLog,
      PermissionsBitField.Flags.CreateInstantInvite,
    ]

    let filteredPermissions = '\`No Special Permissions\`'

    if (trackedPermissions.some((permission) => manageMember.permissions.has(permission))) {
      const permissionsFound = trackedPermissions.filter((permission) =>
        manageMember.permissions.has(permission)
      );

      filteredPermissions = permissionsFound
        .map((permission) => `\`${permissionNames[permission]}\``)
        .join('\n');

    }

      console.log(`Permissions:\n${filteredPermissions}`)

      const embed = new EmbedBuilder()
        .setColor('#aa99ef')
        .setTitle(`User Management | ${manageUser.username}`)
        .setThumbnail(`${manageUser.displayAvatarURL()}`)
        .setDescription(`### <:fairifyUser:1141513576608170004> **User ID:** \`${manageUser.id}\`
`)
        .addFields(
          { name: '<:fairifyDate:1145094016824778846> Account Created', value: `<t:${accountAgeTimestamp}:R>`, inline: true },
          { name: '<:fairifyDate:1145094016824778846> Member Since', value: `<t:${joinDateTimestamp}:R>`, inline: true },
          { name: `<:fairifyRoles:1173816294039105647> ${numberOfRoles === 1 ? '1 Role' : `${numberOfRoles} Roles`}`, value: `${roles}`, inline: false },
          { name: '<:fairifyPermissions:1173827440255172680> Permissions', value: `${filteredPermissions}`, inline: false }
        )
        .setTimestamp(Date.now())
        .setFooter({
          text: `${VERSION}`,
        });

      await interaction.reply({ embeds: [embed], ephemeral: true });

控制台输出:

Permissions:
`No Special Permissions`
node:events:504
      throw er; // Unhandled 'error' event
      ^

CombinedPropertyError: Received one or more errors
    at ArrayValidator.handle (D:\Documents\Discord\FairifyBot\node_modules\@sapphire\shapeshift\dist\index.js:473:70)
    at ArrayValidator.parse (D:\Documents\Discord\FairifyBot\node_modules\@sapphire\shapeshift\dist\index.js:212:88)
    at EmbedBuilder.addFields (D:\Documents\Discord\FairifyBot\node_modules\@discordjs\builders\dist\index.js:225:31)
    at Client.<anonymous> (file:///D:/Documents/Discord/FairifyBot/index.js:659:10)
    at Client.emit (node:events:526:28)
    at InteractionCreateAction.handle (D:\Documents\Discord\FairifyBot\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
    at Object.module.exports [as INTERACTION_CREATE] (D:\Documents\Discord\FairifyBot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (D:\Documents\Discord\FairifyBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31)
    at WebSocketManager.<anonymous> (D:\Documents\Discord\FairifyBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12)
    at WebSocketManager.emit (D:\Documents\Discord\FairifyBot\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:383:10)
    at processTicksAndRejections (node:internal/process/task_queues:85:21) {
  errors: [
    [
      2,
      CombinedPropertyError: Received one or more errors
          at ObjectValidator.handleIgnoreStrategy (D:\Documents\Discord\FairifyBot\node_modules\@sapphire\shapeshift\dist\index.js:1268:70)
          at ObjectValidator.handleStrategy (D:\Documents\Discord\FairifyBot\node_modules\@sapphire\shapeshift\dist\index.js:1121:47)
          at ObjectValidator.handle (D:\Documents\Discord\FairifyBot\node_modules\@sapphire\shapeshift\dist\index.js:1221:17)
          at ObjectValidator.run (D:\Documents\Discord\FairifyBot\node_modules\@sapphire\shapeshift\dist\index.js:198:23)
          at ArrayValidator.handle (D:\Documents\Discord\FairifyBot\node_modules\@sapphire\shapeshift\dist\index.js:467:37)
          at ArrayValidator.parse (D:\Documents\Discord\FairifyBot\node_modules\@sapphire\shapeshift\dist\index.js:212:88)
          at EmbedBuilder.addFields (D:\Documents\Discord\FairifyBot\node_modules\@discordjs\builders\dist\index.js:225:31)
          at Client.<anonymous> (file:///D:/Documents/Discord/FairifyBot/index.js:659:10)
          at Client.emit (node:events:526:28)
          at InteractionCreateAction.handle (D:\Documents\Discord\FairifyBot\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12) {
        errors: [ [ 'value', [ExpectedConstraintError] ] ]
      }
    ]
  ]
}

通常,我可以在一段时间后解决问题,但这个问题让我完全难住了。我花了几个小时试图解决这个问题。

我尝试将注册表注册为字符串、数组,我尝试在某一时刻映射“无特殊权限”,但我通常会收到相同的错误。filteredPermissions

javascript 节点 .js 不和谐 不和谐.js

评论


答:

0赞 RealCraze 11/16/2023 #1

在花了几个小时对究竟是什么导致了错误进行了更多测试后,我发现该问题是通过消除过程引起的:

{ name: `<:fairifyRoles:1173816294039105647> ${numberOfRoles === 1 ? '1 Role' : `${numberOfRoles} Roles`}`, value: `${roles}`, inline: false }

在检查服务器中没有任何角色的用户的信息时触发了该错误。

我修改了角色的处理方式,问题不再存在,一切都很顺利。

以前:

const roles = interaction.guild.members.cache.get(manageUser.id).roles.cache
      .filter(role => role.name !== '@everyone')
      .map(role => `<@&${role.id}>`).join('\n');

    const numberOfRoles = roles.split('\n').length;

后:

let roles = '`No Roles`';

    let numberOfRoles = 0

    const userRoles = interaction.guild.members.cache.get(manageUser.id).roles.cache;

    if (userRoles.size > 1) {
      roles = userRoles
        .filter(role => role.name !== '@everyone')
        .map(role => `<@&${role.id}>`)
        .join('\n');

        numberOfRoles = roles.split('\n').length;
    }