【问题标题】:(node:5318) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'roles' of undefined(节点:5318)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“角色”
【发布时间】:2020-01-20 08:01:22
【问题描述】:

机器人随机崩溃并给出此错误。我已经将它编程为在出现问题时自动重启,并记录错误,因为它在技术上意味着不会关闭。

client.on("message", message => {
  const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
  const command = args.shift().toLowerCase();
    if (command === "bal") {
        if (message.author.bot) return;
    const data = sql.prepare(`SELECT bal FROM ${args}`).get();
      message.channel.send(`You have ${data.bal}`)
  }
  if (command == "give") {
        if(message.member.roles.find(r => r.name === "Economy Manager") || message.member.roles.find(r => r.name === "Economy Manager ")){
      //Get their current balance
    const grab = sql.prepare(`SELECT bal FROM ${args[1]} WHERE rowid = 1;`).get();
    //Grab the value from the first input after the second. Ex: eco tgive 5 Juliana
    const pointsToAdd = parseInt(args[0]);
    //Add the two values from the database and the args[0] input
    const result = +grab.bal + +pointsToAdd;
    //Replace the curret value from column bal in table ${args[1]}, with the ${result}
    sql.prepare(`UPDATE ${args[1]} SET bal = ${result} WHERE rowid = 1;`).run();
    message.channel.send(`You now have ${result}`)
    //sql.prepare(`INSERT OR REPLACE INTO ${args[1]} (bal) VALUES (${result});)`).run();
  }
}
  if (command == "take") {
      if(message.member.roles.find(r => r.name === "Economy Manager") || message.member.roles.find(r => r.name === "Economy Manager ")){
      //Get their current balance
    const grab = sql.prepare(`SELECT bal FROM ${args[1]} WHERE rowid = 1;`).get();
    //Grab the value from the first input after the second. Ex: eco tgive 5 Juliana
    const pointsToAdd = parseInt(args[0]);
    //Add the two values from the database and the args[0] input
    const result = +grab.bal - +pointsToAdd;
    //Replace the curret value from column bal in table ${args[1]}, with the ${result}
    sql.prepare(`UPDATE ${args[1]} SET bal = ${result} WHERE rowid = 1;`).run();
    message.channel.send(`You now have ${result}`)
    //sql.prepare(`INSERT OR REPLACE INTO ${args[1]} (bal) VALUES (${result});)`).run();
}
}
if(message.member.roles.find(r => r.name === "Economy Manager") || message.member.roles.find(r => r.name === "Economy Manager ")){
  if (command == "delete") {
      sql.prepare(`DROP TABLE IF EXISTS ${args}`).run();
      message.channel.send(`Account ${args} has been deleted`);
  }
}
  });

用于自动重启的代码如下:

process.on('uncaughtException', (error, promise) => {
    client.destroy()
    console.log(error)
    client.login(config.token);

});

process.on('uncaughtRejection', (error, promise) => {
  console.log(error)
  client.destroy()
  client.login(config.token);
});

我似乎也收到了“未处理的承诺拒绝”...

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    这应该可以解决您的问题,您的错误在于您尝试通过 if 语句中的名称来定位经济角色的 4 个实例,它不起作用,因为您从未明确定义角色,您定义了消息,您可以使用消息获取角色,但不使用角色属性本身。这段代码应该可以工作:

    client.on('message', message => {
    
        const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
        const command = args.shift().toLowerCase();
        const economyRole = message.guild.roles.find(role => role.id === 'role id here');
    
        if (command === "bal") {
    
            if (message.author.bot) return;
    
            const data = sql.prepare(`SELECT bal FROM ${args}`).get();
    
            message.channel.send(`You have ${data.bal}`)
        }
        if (command === "give") {
    
            if(message.member.roles.has(economyRole.id)) {
    
            //Get their current balance
            const grab = sql.prepare(`SELECT bal FROM ${args[1]} WHERE rowid = 1;`).get();
    
            //Grab the value from the first input after the second. Ex: eco tgive 5 Juliana
            const pointsToAdd = parseInt(args[0]);
    
            //Add the two values from the database and the args[0] input
            const result = +grab.bal + +pointsToAdd;
    
            //Replace the curret value from column bal in table ${args[1]}, with the ${result}
            sql.prepare(`UPDATE ${args[1]} SET bal = ${result} WHERE rowid = 1;`).run();
    
            //sql.prepare(`INSERT OR REPLACE INTO ${args[1]} (bal) VALUES (${result});)`).run();
            message.channel.send(`You now have ${result}`)
            }
        }
        if (command === "take") {
    
            if(message.member.roles.has(economyRole.id)) {
    
            //Get their current balance
            const grab = sql.prepare(`SELECT bal FROM ${args[1]} WHERE rowid = 1;`).get();
    
            //Grab the value from the first input after the second. Ex: eco tgive 5 Juliana
            const pointsToAdd = parseInt(args[0]);
    
            //Add the two values from the database and the args[0] input
            const result = +grab.bal - +pointsToAdd;
    
            //Replace the curret value from column bal in table ${args[1]}, with the ${result}
            sql.prepare(`UPDATE ${args[1]} SET bal = ${result} WHERE rowid = 1;`).run();
    
            //sql.prepare(`INSERT OR REPLACE INTO ${args[1]} (bal) VALUES (${result});)`).run();
            message.channel.send(`You now have ${result}`)
            }          
        }
    
        if(message.member.roles.has(economyRole.id)) {
    
            if (command === "delete") {
    
            sql.prepare(`DROP TABLE IF EXISTS ${args}`).run();
    
            message.channel.send(`Account ${args} has been deleted`);
            }
        }
    })
    .catch(function(err) {
    
        console.error(err);
    })
    

    【讨论】:

    • 现在我得到以下信息:(node:5860) DeprecationWarning: Collection#find: pass a function instead
    • 已修复。只需将上面的答案重新插入您的代码即可。 :D
    • 现在我有这个错误:它只出现过一次,但它在执行命令时出现。 (node:6289) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'roles' of undefined。但是,您的代码仍然有效吗?
    • 所以它出错但代码仍然有效?它在什么命令上出错?
    • 命令是bal。我仍然对它的错误感到惊讶,但它工作得很好。错误只出现一次,看起来像
    猜你喜欢
    • 2021-07-05
    • 2021-10-10
    • 2020-08-18
    • 2020-12-11
    • 1970-01-01
    • 2019-12-15
    • 2021-08-31
    • 2021-09-01
    • 2020-12-27
    相关资源
    最近更新 更多