0
votes

The bot randomly crashes and gives out this error. I've programmed it to auto restart whenever something goes wrong, and log the error, as its technically meant to not shut down.

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

The code used to auto restart is the following:

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

I also seem to be getting an "unhandled promise rejection"...

1

1 Answers

0
votes

This should fix your issue, your error lied in the 4 instances where you tried to locate the economy role by it's name inside the if statement, it didn't work because you never explicitly defined roles, you defined message, you can get to a role using message, but not using the role property itself. This code should work though:

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