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)
});
});
}
这是我需要帮助的代码,所以它的作用是通过让你回答这个问题来确保你是人,例如:;天空是什么颜色;但如果你的回答是蓝色的,它会说";太棒了您现在已验证"然后说";看起来你没有得到正确的答案,正确的答案是蓝色的;即使你写了正确的答案,它甚至也没有给你验证过的角色代码
您需要在代码中修复以下几点:
首先,您声明了一个变量myRole
,但从未实际使用它来分配代码中的角色。
第二,是如何添加角色。
user.member.addRole(myRole);
从Discord.js
v12开始,似乎没有GuildMember
的addRole
方法。我建议这样做:
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)
});
});
资源:
- 了解如何使用角色
- Discord.js-GuildMember