当前位置: 首页 > 知识库问答 >
问题:

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

赫连琦
2023-03-14

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

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个答案

符国安
2023-03-14

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

js prettyprint-override">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);
})
 类似资料:
  • (节点:55028)UnhandledPromiserEjectionWarning:TypeError:无法读取位于/users/PatrickStanciu/WebStormProjects/AUTISMAssistant/Backend/API/Controllers/PaymentsController.js:1045:34 位于processTicksAndRejections(inte

  • 我正在尝试使用node express和Mongoose创建用户登录和注册...创建了用户模型....现在尝试在我的路由器auth.js中添加一个post方法,但由于某种原因,我一直得到错误不能读取属性'name'的未定义昨天我写了完全相同的代码,但使用了一些其他post方法,结果非常好.... 我只想让邮递员的工作现在就这样...多谢 auth.js文件 user.js文件 在此输入图像说明

  • 我是板条箱获取api从那里找到学生的分数主题手动获取和比较分数 我有两个集合和都与引用id连接 这是我的app.js 这是我的模型: 这是我的控制器: 我尝试了很多方法,但我的不能正常工作,这将产生错误。

  • tran.js 错误 (节点:12012)未处理的PromisejectionWarning:未处理的promise拒绝(拒绝id:1):TypeError:无法读取未定义(节点:12012)[DEP0018]弃用警告:未处理的promise拒绝被弃用。将来,未处理的promise拒绝将终止节点。具有非零退出代码的js进程。 我不知道该怎么办

  • 我试图添加盐到我的密码,但我有这个错误: (节点:12652)未处理的PromisejectionWarning:TypeError:无法读取未定义的属性“value” (节点: 12652)UnhandledPromiseRejse警告:未处理的promise拒绝。这个错误要么是由于抛出一个没有捕获块的异步函数,要么是由于拒绝了一个没有用. cat()处理的promise。(拒绝id: 1) (

  • 下午好,我是新反应和Node.js和我有一个问题。 我在一个网页上有一个表格,你将在那里收到旅行的数据。travel_order模型中存在这些列(Id locales、name、date、time),但是“lockations name”列旨在使用Id列出这些地方的名称。此位置的ID在“地点ID”列中列出。但是,在列出时,它给我一个错误:“TypeError:不能读取未定义的属性'Designac