简体   繁体   中英

Discord.js bot appearing to not be working, no errors | Could it be my indentation?

I've been working on this bot for a while, before I made an update which included the 'limitedquests' it perfectly worked. But now, as I updated it, multiple things do not work inside of it and since there are no errors, I am really unsure why. I will post the code here and explain further.

client.on('messageCreate', (msg) => {
if (msg.content === '!quest'){
  if (talkedRecently.has(msg.author.id)) {
            msg.reply("The Quest can be used again in 6 hours (If you get a Very Hard or above quest and you are below Phase 3, you are allowed to not do that quest)");
    } else {
    if (msg.content === '!quest') {
    const randNumberChosen = Math.floor(Math.random() * 13 + 1);
        if (randNumberChosen == 1){
        msg.reply({embeds: [questEmbed] });
        }
        if (randNumberChosen == 2){
        msg.reply({embeds: [questEmbed2] });
        }
        if (randNumberChosen == 3){
        msg.reply({embeds: [questEmbed3] });
        }
        if (randNumberChosen == 4){
        msg.reply({embeds: [questEmbed4] });
        }
        if (randNumberChosen == 5){
        msg.reply({embeds: [questEmbed5] });
        }
        if (randNumberChosen == 6){
        msg.reply({embeds: [questEmbed6] });
        }
        if (randNumberChosen == 7){
        msg.reply({embeds: [questEmbed7] });
        }
        if (randNumberChosen == 8){
        msg.reply({embeds: [questEmbed8] });
        }
        if (randNumberChosen == 9){
        msg.reply({embeds: [questEmbed9] });
        }
        if (randNumberChosen == 10){
        msg.reply({embeds: [questEmbed10] });
        }
        if (randNumberChosen == 11){
        msg.reply({embeds: [questEmbed11] });
        }
        if (randNumberChosen == 12){
        msg.reply({embeds: [questEmbed12] });
        }
        if (randNumberChosen == 13){
        msg.reply({embeds: [questEmbed13] });
        }
        talkedRecently.add(msg.author.id);
        setTimeout(() => {
          talkedRecently.delete(msg.author.id);
        }, 21556900);
    }
}
if (msg.content === '!limitedquest'){
  if (talkedRecently4.has(msg.author.id)) {
            msg.reply("The Limited Quest can be used again in 8 hours");
    } else {
    if (msg.content === '!quest') {
    const randNumberChosen4 = Math.floor(Math.random() * 2 + 1);
        if (randNumberChosen4 == 1){
        msg.reply({embeds: [limitedQuestEmbed1] });
        }
        if (randNumberChosen4 == 2){
        msg.reply({embeds: [limitedQuestEmbed2] });
        }
        talkedRecently4.add(msg.author.id);
        setTimeout(() => {
          talkedRecently4.delete(msg.author.id);
        }, 28800000);
    }
}
}
}
if (msg.content === '!chest'){
  if (talkedRecently2.has(msg.author.id)) {
            msg.reply("The Chest can be used again in 12 hours");
    } else {
    if (msg.content === '!chest') {
    const randNumberChosen2 = Math.floor(Math.random() * 10 + 1);
        if (randNumberChosen2 == 1){
        msg.reply({embeds: [christmasChest1] });
        }
        if (randNumberChosen2 == 2){
        msg.reply({embeds: [christmasChest1] });
        }
        if (randNumberChosen2 == 3){
        msg.reply({embeds: [christmasChest3] });
        }
        if (randNumberChosen2 == 4){
        msg.reply({embeds: [christmasChest3] });
        }
        if (randNumberChosen2 == 5){
        msg.reply({embeds: [christmasChest2] });
        }
        if (randNumberChosen2 == 6){
        msg.reply({embeds: [christmasChest1] });
        }
        if (randNumberChosen2 == 7){
        msg.reply({embeds: [christmasChest4] });
        }
        if (randNumberChosen2 == 8){
        msg.reply({embeds: [christmasChest5] });
        }
        if (randNumberChosen2 == 9){
        msg.reply({embeds: [christmasChest6] });
        }
        if (randNumberChosen2 == 10){
        msg.reply({embeds: [christmasChest7] });
        }
        talkedRecently2.add(msg.author.id);
        setTimeout(() => {
          talkedRecently2.delete(msg.author.id);
        }, 43200000);
    }
}
if (msg.content === '!chestrarity') {
    msg.reply({embeds: [rarity] });
}
if (msg.content === '!christmas') {
    if (talkedRecently3.has(msg.author.id)) {
            msg.reply("3rd day of Chrismas reward available tomorrow :christmas_tree:");
    } else {
    const randNumberChosen3 = Math.floor(Math.random() * 25 + 1);
    if(randNumberChosen3 < 14){
    randNumberChosen3 = 15;
    }
    console.log(randNumberChosen3);
    msg.reply("Day 2 Christmas Gift.. you get: " + randNumberChosen3 + " EXP");
    talkedRecently3.add(msg.author.id);
        setTimeout(() => {
          talkedRecently3.delete(msg.author.id);
        }, 86400000);
      }
    }
}
});

It's not really that big, just a bunch of if statements because of the random number. I am doing all the commands, since there is only 4 or so commands, in one file. The parts that work are ".quest" and ",chest", But after that, ".chestrarity", ".limitedquest". or "!christmas" just do not work at all. I've tried rewriting it, I've also tried to fix indentation. Nothing seems to fix this and I'm really just confused.

And yes, I did define all the things required, I will show it here seperately

const Discord = require('discord.js');
const {MessageEmbed} = require('discord.js');

const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })

const talkedRecently = new Set();
const talkedRecently2 = new Set();
const talkedRecently3 = new Set();
const talkedRecently4 = new Set();

It's not about indentations but braces. You have, for example if (msg.content === '!limitedquest') under if (msg.content === '!quest') , that is, they should both be simultaneously true, which doesn't really make sense.

So, to fix this, rework through all the blocks and make sure they form a logical and coherent structure. A decent text editor is a great help.

Remember, while JavaScript doesn't as a language depend on indentations, they are very important for anybody who reads the code. That's why it's important to ensure that indentations and blocks (marked by parentheses and brackets and braces) agree.

Your goals:

  1. Check the command.
  2. Check if the author has already tried the command.
  3. If they have, send them a message.
  4. If they haven't, generate a random number and send a message depending on the value.

You can check the command using either msg.content == "!command" , or with msg.content.startsWith("!command") .

We then call the handler function for each command. This makes it easier to manage our code.

if(msg.command.startsWith("!quest")) questHandler(msg, questCache);
if(msg.command.startsWith("!chest")) chestHandler(msg, chestCache);
if(msg.command.startsWith("!christmas")) christmasHandler(msg, christmasCache);

Now in the handler we have the logic for the command. I added the cache as the second parameter, so this function won't break if something happens with the cache, such as you changing the variable name.

The rest is your previous code, but cleaned up.

function questHandler(msg, cache){
  // check if the author has already done the task.
  if(cache.has(msg.author.id)){
    msg.send("You have to wait 6 hours to do the quest again.")
  } else {
    const num = Math.ceil(Math.random() * 13);
    num === 1 
      ? msg.reply({embeds: [questEmbed] });
      : msg.reply({embeds: [[`questEmbed${num}`]] });

    cache.add(msg.author.id);
    setTimeout(() => {
      cache.delete(msg.author.id);
    }, 60 * 60 * 24 * 6);
  }
}

Do the same for the other commands, and it should be a lot easier to manage your code.

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