Node.js捕获了未定义但已定义的错误

时间:2020-07-18 07:45:18

标签: javascript node.js discord.js

我已经在一个机器人上工作了12天,我需要升级到v12。一切都进行得很顺利,直到我尝试了一个将给所提到的用户指定角色的命令。

我有2个变量。一个是用户指定的角色变量和成员变量(等于message.guild.members.get(args [0]))。

代码如下:

let Member = message.mentions.members.first() || message.guild.members.get(arguments[0]);
    if (!Member) return message.channel.send("Error: Either the member does not exist or you haven't put the member to give the role to!");
    let role = message.guild.roles.cache.some(r => r.name === arguments[1]) || message.guild.roles.cache.some(r => r.id === arguments[1]) || message.mentions.roles.first();
    if (!role) return nessage.channel.send("Error: Please provide a valid role!");
    
    // if (role === "undefined") {
        // let role = message.guild.roles.cache.some(r => r.name === arguments[1]) || message.guild.roles.cache.some(r => r.id === arguments[1]) || message.mentions.roles.first();
    // }
    
    if (Member.roles.cache.has(role.id)) {
        return message.channel.send("Error: The mentioned user already has that role!");
    } else {
        let role = message.guild.roles.cache.some(r => r.name === arguments[1]) || message.guild.roles.cache.some(r => r.id === arguments[1]) || message.mentions.roles.first();
        console.log(message);
        console.log("role" + role);
        Member.roles.add(role.id).catch(e => console.log("Error: " + e.message));
    }

代码对我来说看起来不错,并希望它能正常工作,但是随后在控制台中出现错误。它没有说未定义什么变量,而只是说它未定义。

我尝试检查角色变量是否未定义,但由于它不起作用而将其注释掉。然后,我让它打印消息并检查角色是否未定义。角色变量很好。消息变量很好。即使是Member变量也可以。

我使用最新的Node.js。

这是控制台日志:

Message {
  channel: TextChannel {
    type: 'text',
    deleted: false,
    id: '729663938148565134',
    name: 'general',
    rawPosition: 0,
    parentID: '729663938148565132',
    permissionOverwrites: Collection [Map] {},
    topic: 'Beware, there will be a lot of mentions.',
    nsfw: undefined,
    lastMessageID: '733951355240316948',
    rateLimitPerUser: 0,
    lastPinTimestamp: 1594845448699,
    guild: Guild {
      members: [GuildMemberManager],
      channels: [GuildChannelManager],
      roles: [RoleManager],
      presences: [PresenceManager],
      voiceStates: [VoiceStateManager],
      deleted: false,
      available: true,
      id: '729663937557168181',
      shardID: 0,
      name: 'Bot Testing',
      icon: null,
      splash: null,
      region: 'europe',
      memberCount: 6,
      large: false,
      features: [],
      applicationID: null,
      afkTimeout: 300,
      afkChannelID: null,
      systemChannelID: '729663938148565134',
      embedEnabled: undefined,
      premiumTier: 0,
      premiumSubscriptionCount: 0,
      verificationLevel: 'MEDIUM',
      explicitContentFilter: 'DISABLED',
      mfaLevel: 0,
      joinedTimestamp: 1594035922599,
      defaultMessageNotifications: 'ALL',
      systemChannelFlags: [SystemChannelFlags],
      vanityURLCode: null,
      description: null,
      banner: null,
      rulesChannelID: null,
      publicUpdatesChannelID: null,
      ownerID: '402159567200583680',
      emojis: [GuildEmojiManager]
    },
    messages: MessageManager {
      cacheType: [Function: LimitedCollection],
      cache: [LimitedCollection [Map]],
      channel: [Circular]
    },
    _typing: Map { '402159567200583680' => [Object] }
  },
  deleted: false,
  id: '733951355240316948',
  type: 'DEFAULT',
  content: '>giveRole <@!655895332722442282> Mod',
  author: User {
    id: '402159567200583680',
    bot: false,
    username: '??KFM??',
    discriminator: '3147',
    avatar: '5f322e4ab3ec14e9fa83e1c77d8bb36d',
    lastMessageID: '733951355240316948',
    lastMessageChannelID: '729663938148565134',
    flags: UserFlags { bitfield: 0 }
  },
  pinned: false,
  tts: false,
  nonce: '733951341017169920',
  system: false,
  embeds: [],
  attachments: Collection [Map] {},
  createdTimestamp: 1595058039246,
  editedTimestamp: null,
  reactions: ReactionManager {
    cacheType: [Function: Collection],
    cache: Collection [Map] {},
    message: [Circular]
  },
  mentions: MessageMentions {
    everyone: false,
    users: Collection [Map] { '655895332722442282' => [User] },
    roles: Collection [Map] {},
    _members: Collection [Map] { '655895332722442282' => [GuildMember] },
    _channels: null,
    crosspostedChannels: Collection [Map] {}
  },
  webhookID: null,
  application: null,
  activity: null,
  _edits: [],
  flags: MessageFlags { bitfield: 0 },
  reference: null
}
roletrue
Error: undefined

谢谢!

编辑:好像role.id是未定义的。我将尝试看看如何解决该问题。

1 个答案:

答案 0 :(得分:0)

我认为这部分代码有问题

    console.log("role" + role); // true
    Member.roles.add(role.id).catch(e => console.log("Error: " + e.message));

您尝试在角色中添加undefined,因为:

const roleId = (true).id;
console.log(roleId); //undefined

您从some函数获得了布尔值,该函数始终返回truefalse,请尝试将其更改为find

尝试添加一些 id ,例如733951355240316948。

我不知道role.id的样子,但是角色必须是Object

例如:

{
  id:'asdasdawe3534536456',
  // other fields
}

您需要在.add中找到期望的Member.roles.add功能