簡體   English   中英

Discord.js 機器人似乎無法正常工作,沒有錯誤 | 會不會是我的縮進?

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

在我進行更新之前,我一直在研究這個機器人,其中包括“有限任務”,它完美地工作。 但是現在,當我更新它時,它里面的很多東西都不起作用,而且由於沒有錯誤,我真的不確定為什么。 我將在這里發布代碼並進一步解釋。

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);
      }
    }
}
});

它並沒有那么大,只是因為隨機數而產生的一堆 if 語句。 我正在執行所有命令,因為一個文件中只有 4 個左右的命令。 起作用的部分是“.quest”和“,chest”,但之后是“.chestrarity”、“.limitedquest”。 或“!聖誕節”根本不起作用。 我嘗試過重寫它,我也嘗試過修復縮進。 似乎沒有什么可以解決這個問題,我真的很困惑。

是的,我確實定義了所有需要的東西,我將在這里單獨展示

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();

這不是關於縮進,而是關於大括號。 例如, if (msg.content === '!limitedquest')if (msg.content === '!quest')下有,也就是說,它們應該同時為真,這實際上沒有意義.

因此,要解決此問題,請重新處理所有塊,並確保它們形成一個合乎邏輯且連貫的結構。 一個體面的文本編輯器是一個很大的幫助。

請記住,雖然 JavaScript 作為一種語言不依賴於縮進,但對於閱讀代碼的任何人來說,它們都非常重要。 這就是為什么確保縮進和塊(由括號和括號和大括號標記)一致很重要的原因。

你的目標:

  1. 檢查命令。
  2. 檢查作者是否已經嘗試過該命令。
  3. 如果有,請給他們發消息。
  4. 如果沒有,則生成一個隨機數並根據該值發送一條消息。

您可以使用msg.content == "!command"msg.content.startsWith("!command")來檢查命令。

然后,我們為每個命令調用處理程序 function。 這使得管理我們的代碼變得更加容易。

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

現在在處理程序中,我們有了命令的邏輯。 我將緩存添加為第二個參數,因此如果緩存發生某些事情(例如更改變量名),此 function 不會中斷。

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);
  }
}

對其他命令執行相同的操作,管理代碼應該會容易得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM