我正在开发一个简单的Discord Trivia机器人(使用Discord.js)。我正在使用下面的代码,但是Glitch给我错误,提示userAnswer is not defined
和answered is not defined
等
该代码应选择一个随机问题,然后提出问题并等待答案,然后说出是对还是错。我不知道为什么会发生错误。
任何帮助将不胜感激。
const Discord = require("discord.js")
const { Client, MessageAttachment, MessageEmbed, MemberAdd, RichEmbed } = require('discord.js');
const client = new Client();
client.on("ready", () => {
client.user.setActivity("ihs help",{
type:"STREAMING",
url:"https://www.twitch.tv/ihsbot"
})
console.log("I am ready!");
client.on("guildMemberAdd", member => {
member.send(
"Welcome to the server! Please follow our server's rules. Have fun! ?"
)
})
client.on('message', message, msg => {
if (answered == false) {
userAnswer = msg;
if (userAnswer == cAnswer) {
message.reply ("got it right!")
}
else {
message.reply ("got it wrong!")
}
answered = true; cAnswer = ""; userAnswer = "";
}
if (msg.startsWith("ihs quiz")) {
answered = true;
cAnswer = "";
userAnswer = "";
number = 3;
random = Math.floor (Match.random() * (number - 1 + 1)) + 1;
switch (random) {
case 1: message.channel.send ("**Question** n\A) Answer1 n\B) Answer2 n\C) Answer3 n\D) Answer4"); cAnswer = "a"
case 2: message.channel.send ("**Question** n\A) Answer1 n\B) Answer2 n\C) Answer3 n\D) Answer4"); cAnswer = "a"
case 3: message.channel.send ("**Question** n\A) Answer1 n\B) Answer2 n\C) Answer3 n\D) Answer4"); cAnswer = "a"
}
answered = false;
}
});
client.login("TOKEN");
答案 0 :(得分:0)
Node.JS与PHP不一样,您可以在其中将值放入变量,但是必须首先声明它,它有3种主要类型:const =常量变量,您不更改,let和var =变量,可以更改,有关这方面的更多信息,这是一篇不错的文章What's the difference between using "let" and "var"?。要声明变量,请执行var varName
(与let和const相同)。