提问者:小点点

(节点:5318)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“roles”


机器人随机崩溃并发出此错误。我已经将它编程为每当有问题时自动重新启动,并记录错误,因为它的技术上意味着不关机。

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);
});

我似乎也被“未经处理的承诺拒绝”...


共1个答案

匿名用户

这应该可以解决您的问题,您的错误发生在4个实例中,您试图通过if语句中的名称来定位economy角色,但没有工作,因为您从未显式定义角色,您定义了message,您可以使用message来获取角色,但不使用role属性本身。这段代码应该在以下情况下工作:

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);
})