【问题标题】:discord.js Unhandled Promise Rejection Warning : TypeError: Cannot read property 'includes' of nulldiscord.js未处理的承诺拒绝警告:TypeError:无法读取属性'includes' of null
【发布时间】:2021-05-21 11:58:32
【问题描述】:

我最近为我的机器人制作了一个自动角色代码,它会读取每个用户的状态,如果他们的状态中有我们的服务器虚荣邀请链接,就会给他们一个角色。代码完美运行,机器人添加了角色,然后删除了它,但这里的问题是它循环通过错误而不会使机器人崩溃:

(node:38) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'includes' of null
(node:38) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 345)

我很确定这是因为机器人无法读取此类离线且没有状态的用户的状态。我会感谢您的帮助,因为我无法解决这个问题。 这是我的代码:

client.on('presenceUpdate', async (oldPresence, newPresence) => {

    const role = newPresence.guild.roles.cache.get("832132526302035992");
    const member = newPresence.member
    const activities = member.user.presence.activities[0];
  
    if (activities && (activities.state.includes( ".gg/moonxile" ) || activities.state.includes("discord.gg/moonxile" ))) {
      return newPresence.member.roles.add(role)
      .catch(err => {
      console.log(err)
      return;
      })

    } else {
    if(member.roles.cache.get(role.id)) {
      newPresence.member.roles.remove(role)
      .catch(err => {
      console.log(err)
      return;

      })
      }
    }
});

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    对于某些人,当他们从存在状态更改为无时,它将没有数据,因此应该进行检查

    
        if (activities &&  activities.state && (activities.state.includes( ".gg/moonxile" ) || activities.state.includes("discord.gg/moonxile" ))) {
          return newPresence.member.roles.add(role)
          .catch(err => {
          console.log(err)
          return;
          })
    
        }
    

    【讨论】:

    • 当用户下线时,bot会移除角色,并在他/她重新上线时添加回来
    • here 是控制台显示的内容
    • 那是另一个问题错误。你能提供更多信息吗?你有没有得到那个错误
    • 机器人在用户离线时删除角色,并在他/她重新在线时将其添加回来 –> 您必须检查用户状态是否未离线,如果您愿意,可以更改他们的状态防止这种行为
    • 哦,好吧,让我这样做
    【解决方案2】:

    首先检查activities,然后检查activities.state,最后包括.include()

    【讨论】:

    • 哦好吧让我试试
    • welp,控制台充满了这个:(node:174) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'state' of undefined (node:174) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 5)
    • 哦,好吧。已更新。
    • welp 我试过了,我得到了this error
    猜你喜欢
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多