NodeJS 为什么是〈@&undefined>?

jutyujz0  于 2023-05-06  发布在  Node.js
关注(0)|答案(1)|浏览(233)

所以基本上,当我用我的机器人做/更新时,它的工作是添加角色,但在消息中它说〈@&undefined〉再次添加用户角色。
部分代码:

const addedRoles = [];
const removedRoles = [];

try {
  const inGroup = await noblox.getRankInGroup(groupid, await noblox.getIdFromUsername(robloxUsername));
  if (inGroup) {
    // Find the matching rankbind
    const rankNumber = await noblox.getRankInGroup(groupid, await noblox.getIdFromUsername(robloxUsername));
    const rankbind = groups.rankbinds.find(r => r.rank === rankNumber);

    
    if (rankbind) {
      const rolesArray = rankbind.roles;

  // Add all roles that are in the rolesArray
  const rolesToAdd = rolesArray.filter(roleId => !guildMember.roles.cache.has(roleId));
  if (rolesToAdd.length > 0) {
    await guildMember.roles.add(rolesToAdd);
    addedRoles.push(...rolesToAdd);
  }
    
  // Remove all roles that are not in the rolesArray, 
  const rolesToRemove = guildMember.roles.cache.filter(role => !rolesArray.includes(role.id) && role.id !== guild.id);
  if (rolesToRemove.size > 0) {
    await guildMember.roles.remove(rolesToRemove);
    removedRoles.push(...rolesToRemove);
  }

      // Set the user's nickname
      const template = rankbind.template;
      const nickname = `${template} ${robloxUsername}`;
      if (nickname !== guildMember.nickname) {
        await guildMember.setNickname(nickname);
      }
    }
  }

  // Send confirmation message
  const embed = new EmbedBuilder()
    .setColor('#000000')
    .setAuthor({
      name: guildMember.user.tag,
      iconURL: guildMember.user.displayAvatarURL(),
    })
    .setTitle('Roles Update')
    .setDescription('Successfully updated user roles')
    .addFields(
      { name: 'Nickname', value: guildMember.nickname || guildMember.user.username },
      {
        name: 'Roles Added',
        value: addedRoles.length ? addedRoles.map(role => `<@&${role.id}>`).join('\n') : 'None'
      },
      {
        name: 'Roles Removed',
        value: removedRoles.length ? removedRoles.map(role => `<@&${role.id}>`).join('\n') : 'None'
      },
    );

留言图片:[图片][1]
我用的是Discord JS。Node.js v18.15.0。不知道这些信息是否对你有帮助[1]:https://i.stack.imgur.com/i0CnE.png

abithluo

abithluo1#

显然,在存储角色时,您存储的不是角色对象,而是roleId,因此数组addedRoles是一个id数组,这就是为什么它返回undefined的原因。
尝试替换addedRoles.map(role =><@&${role.id}>)
关于addedRoles.map(roleId =><@&${roleId}>)

相关问题