skip to Main Content

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


  1. client.on('messageCreate', (message) => {
      if (message.channelId === mhc_minor && !message.author.bot) {
        const ignorePrefixes = ["[+1 Soul Point]", "[!]", "[Info]", "[Event]", "[Be sure", "[Sale]"];
        const startsWithIgnorePrefix = ignorePrefixes.some(prefix => message.content.startsWith(prefix));
    
        if (!startsWithIgnorePrefix) {
          let contentToSend = message.content;
          let channelToSend = mhc_bfs;
    
          if (message.content.includes("➤")) {
            channelToSend = mhc_mprivate;
          } else if (message.content.includes('@')) {
            contentToSend = message.content.replace(/@(S+)/g, '`@$1`');
          }
    
          const channel = client.channels.cache.get(channelToSend);
          channel.send(contentToSend);
        }
      }
    });
    Login or Signup to reply.
  2. This might be a bit more readable:

      client.on('messageCreate', message => {
        if (message.channelId === mhc_minor && message.author.bot && message.content.startsWith('[')) {
          const prefixes = ['[+1 Soul Point]', '[!]', '[Info]', '[Event]', '[Be sure', '[Sale]'];
          if (prefixes.some(prefix => message.content.startsWith(prefix))) {
            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);
          }
        }
      });
    

    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:

      client.on('messageCreate', message => {
        if (message.channelId === mhc_minor && message.author.bot && message.content.startsWith('[')) {
          const prefixes = ['[+1 Soul Point]', '[!]', '[Info]', '[Event]', '[Be sure', '[Sale]'];
          if (!prefixes.some(prefix => message.content.startsWith(prefix))) {
              if (message.content.includes('➤')) {
                  if (message.channelId === mhc_minor) {
                      const channel = client.channels.cache.get(mhc_mprivate);
                      channel.send(message.content);
                }
                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);
            }
          }
        }
      });
    

    Generally indentation should indicate the level of nesting, which is not true in your code – e.g. these two lines should have different nesting:

    if (message.content.startsWith('[') ) { 
            if (message.content.startsWith("[+1 Soul Point]")) { return } 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search