I have wayy too many nested ifs in my code and ive looked up tutorials on how to remove them but they dont seem to work
client.on('messageCreate', (message) => {
if (message.channelId === mhc_minor ) {
if (message.author.bot) {
if (message.content.startsWith('[') ) {
if (message.content.startsWith("[+1 Soul Point]")) { return } ;
if (message.content.startsWith("[!]")) { return };
if (message.content.startsWith("[Info]")) { return };
if (message.content.startsWith("[Event]")) { return };
if (message.content.startsWith("[Be sure")) { return };
if (message.content.startsWith("[Sale]")) { return };
if (message.content.includes("➤")) {
if (message.channelId === mhc_minor ) {
const channel = client.channels.cache.get(mhc_mprivate)
channel.send(message.content); } return };
if (message.content.includes('@')) {
const contentWithBackticks = message.content.replace(/@(S+)/g, '`@$1`');
const channel = client.channels.cache.get(mhc_bfs)
channel.send(contentWithBackticks);
} else {
const channel = client.channels.cache.get(mhc_bfs);
channel.send(message.content);
}}}}})
works perfectly fine but the nested if look really bad, how do I could them while still getting the same result
2
Answers
This might be a bit more readable:
I suspect you might not need the return statements as well (you do not use them is some conditions). If that is true, this would work:
Generally indentation should indicate the level of nesting, which is not true in your code – e.g. these two lines should have different nesting: