[英]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 作为一种语言不依赖于缩进,但对于阅读代码的任何人来说,它们都非常重要。 这就是为什么确保缩进和块(由括号和括号和大括号标记)一致很重要的原因。
你的目标:
您可以使用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.