简体   繁体   中英

Is there anyway to clean this SQL/JS up?

I am working on a bot that sends users riddles when the join a discord server, the code below selects a random riddle from one table, saves it in another table with the user's (who the riddle has been sent to) id, before sending them a message with the riddle in.

Does anyone know if there is a better way to do this than what I have done, because it feels a bit ugly/inefficent having a query in a query.

client.on('guildMemberAdd', member => {
  connection.query('SELECT riddle FROM riddles AS riddle ORDER BY RAND() LIMIT 1', function (error, results, fields) {
    if (error) throw error;

    connection.query('INSERT INTO cache VALUES (?, ?)', [member.id, results[0].riddle], function (error, results, fields) {
      if (error) throw error;
    });

    member.send(results[0].riddle);
  });
});

Thanks.

To be honest, that's a pretty good way to go about it. Though you probably want to send the riddle only after you've ensured it's been saved to the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM