0

I have come a long way from when I first started learning to code however, this is one thing that has eluded me. I need to add a couple of things into an event on my bot however it can become unreachable if logging is disabled as it returns, what is the general way to do something like this?

  const id = db.prepare(`SELECT channel FROM logging WHERE guildid = ${member.guild.id};`).get();
  if (!id) return;
  const logs = id.channel;
  const logembed = new MessageEmbed()
    .setAuthor('Member Joined', member.user.avatarURL())
    .setDescription(`<@${member.user.id}> - ${member.user.tag}`)
    .setColor(color)
    .setFooter(`ID: ${member.user.id}`)
    .setTimestamp();
  bot.channels.cache.get(logs).send(logembed);

  // Example
  // If I do code here, but logs are disabled, it's unreachable as the above returns.
};
Ragnar Lothbrok
  • 306
  • 1
  • 4
  • 22
  • Put it into a function instead, and call that function when it's needed, before returning? – CertainPerformance Apr 01 '20 at 04:41
  • This is called "early return", and is a subject of [ethernal debat](https://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement). But whether you're using early returns or not, you've to structure your code so, that everything needs to be executed will be executed. (My two cents to the debat is, that I always add a comment at the top of the function: "OBS! Early returns", when there is/are early return(s) in the function.) – Teemu Apr 01 '20 at 05:26
  • Where do you think unreachable code is in this function? The code where you comment is wouldn't be unreachable unless something before it `throw`s as there's no regular `return` other than the `if (!id) return` which is clearly a conditional that may or may not be `true` so it doesn't make code unreachable. – jfriend00 Apr 01 '20 at 05:33

1 Answers1

0

Would removing the return statement be a good way to do it?

  const id = db.prepare(`SELECT channel FROM logging WHERE guildid = ${member.guild.id};`).get();
  if (id) {
    const logs = id.channel;
    const logembed = new MessageEmbed()
      .setAuthor('Member Joined', member.user.avatarURL())
      .setDescription(`<@${member.user.id}> - ${member.user.tag}`)
      .setColor(color)
      .setFooter(`ID: ${member.user.id}`)
      .setTimestamp();
    bot.channels.cache.get(logs).send(logembed);
  }
Ragnar Lothbrok
  • 306
  • 1
  • 4
  • 22