我已經在這個 bot 上作業了一段時間,然后我做了一個更新,其中包括“limitedquests”,它完美地作業。但是現在,當我更新它時,它里面有很多東西不起作用,而且由于沒有錯誤,我真的不確定為什么。我將在這里發布代碼并進一步解釋。
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”或“!christmas”就根本不起作用了。我試過重寫它,我也試過修復縮進。似乎沒有什么可以解決這個問題,我真的很困惑。
是的,我確實定義了所有需要的東西,我會在這里單獨展示
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();
uj5u.com熱心網友回復:
這不是關于縮進,而是關于大括號。例如,if (msg.content === '!limitedquest')在 下if (msg.content === '!quest'),也就是說,它們應該同時為真,這實際上沒有意義。
所以,為了解決這個問題,重新處理所有的塊,并確保它們形成一個邏輯和連貫的結構。一個體面的文本編輯器是一個很大的幫助。
請記住,雖然 JavaScript 作為一種語言不依賴于縮進,但它們對于閱讀代碼的任何人來說都非常重要。這就是為什么確保縮進和塊(由括號和方括號和大括號標記)一致很重要。
uj5u.com熱心網友回復:
你的目標:
- 檢查命令。
- 檢查作者是否已經嘗試過該命令。
- 如果他們有,請給他們發訊息。
- 如果沒有,則生成一個亂數并根據該值發送一條訊息。
您可以使用msg.content == "!command", 或來檢查命令msg.content.startsWith("!command")。
然后我們為每個命令呼叫處理函式。這使得管理我們的代碼更容易。
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 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);
}
}
對其他命令執行相同的操作,管理代碼應該會容易得多。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/381094.html
標籤:javascript 节点.js 不和谐 不和谐.js
上一篇:根據按下的按鈕填充不同顏色的畫布
下一篇:如果月份是12月,則執行*.js
