const quiz = [
{
"question": "What color is the sky?",
"answer": "blue"
},
{
"question": "How many letters are there in the alphabet?",
"answer": "26"
}
];
const item = quiz[Math.floor(Math.random() * quiz.length)];
const user = message.author
let myRole = message.guild.roles.cache.find(role => role.name === "Verified");
const embed = new Discord.MessageEmbed()
.setTitle('Mrrocketman10s Official Verification System')
.setDescription('Makes sure that you are a human')
.addField('Username: ', user.username)
.addField('Account created at: ', user.createdAt.toLocaleDateString())
.addField("To verify answer the following question.", item.question)
.setThumbnail()
if(!item){
console.log("Question and answer doesn't exist")
return false
}
const filter = response => {
return item.answer.toLowerCase() === response.content.toLowerCase();
};
message.channel.send(embed).then(() => {
message.channel.awaitMessages(filter, { max: 1, time: 30000, errors: ['time'] })
.then(collected => {
message.reply("Great! You are now verified!").then(user.member.addRole(role));
})
.catch(collected => {
message.reply("Looks like you didn't get the correct answer, the correct answer is " + item.answer)
});
});
}
这是我需要帮助的代码,因此它的作用是通过让您回答问题来确保您是人类,例如:“天空是什么颜色”但如果您回答蓝色它会说“太好了!您现在已通过验证!”然后说“看起来你没有得到正确的答案,正确的答案是蓝色的”即使你写了正确的答案,它甚至没有给你经过验证的角色 the code
答案 0 :(得分:0)
您需要在代码中修复一些问题:
首先,您声明了一个变量 myRole
,但实际上从未使用它来分配代码中的角色。
其次,是如何添加角色。
user.member.addRole(myRole);
从 addRole
v12 开始,似乎没有 GuildMember
的 Discord.js
方法。我建议这样做:
member.roles.add(myRole).catch(console.error);
第三,您必须检查 response
消息是否也来自 message.author
,否则其他人可能会回答并且机器人会认为它是正确的。
const filter = response => {
return item.answer.toLowerCase() === response.content.toLowerCase() && response.author == message.author;
};
最后,也是最简单的,您添加了一个会导致语法错误的大括号。你只需要删除一个大括号就可以了。
旁注:我认为 if (!item)
部分是无关紧要的,因此我将其删除。
代码:
const quiz = [
{
"question": "What color is the sky?",
"answer": "blue"
},
{
"question": "How many letters are there in the alphabet?",
"answer": "26"
}
];
const item = quiz[Math.floor(Math.random() * quiz.length)];
const user = message.author;
const member = message.guild.member(user);
let myRole = message.guild.roles.cache.find(role => role.name === "Verified");
const embed = new Discord.MessageEmbed()
.setTitle('Mrrocketman10s Official Verification System')
.setDescription('Makes sure that you are a human')
.addField('Username: ', user.username)
.addField('Account created at: ', user.createdAt.toLocaleDateString())
.addField("To verify answer the following question.", item.question)
.setThumbnail()
//removed if (!item) check
const filter = response => {
//added an extra parameter to check if the user responding is the original author
return item.answer.toLowerCase() === response.content.toLowerCase() && response.author == message.author;
};
message.channel.send(embed).then(() => {
message.channel.awaitMessages(filter, { max: 1, time: 30000, errors: ['time'] })
.then(collected => {
//modified section here
message.reply("Great! You are now verified!").then(member.roles.add(myRole).catch(console.error));
})
.catch(collected => {
message.reply("Looks like you didn't get the correct answer, the correct answer is " + item.answer)
});
});
资源: