简体   繁体   English

Discord.js 机器人似乎无法正常工作,没有错误 | 会不会是我的缩进?

[英]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.它并没有那么大,只是因为随机数而产生的一堆 if 语句。 I am doing all the commands, since there is only 4 or so commands, in one file.我正在执行所有命令,因为一个文件中只有 4 个左右的命令。 The parts that work are ".quest" and ",chest", But after that, ".chestrarity", ".limitedquest".起作用的部分是“.quest”和“,chest”,但之后是“.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.例如, if (msg.content === '!limitedquest')if (msg.content === '!quest')下有,也就是说,它们应该同时为真,这实际上没有意义.

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.请记住,虽然 JavaScript 作为一种语言不依赖于缩进,但对于阅读代码的任何人来说,它们都非常重要。 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") .您可以使用msg.content == "!command"msg.content.startsWith("!command")来检查命令。

We then call the handler function for each command.然后,我们为每个命令调用处理程序 function。 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.我将缓存添加为第二个参数,因此如果缓存发生某些事情(例如更改变量名),此 function 不会中断。

The rest is your previous code, but cleaned up. rest 是您以前的代码,但已清理。

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.对其他命令执行相同的操作,管理代码应该会容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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