discord.js - 我正在编写一个不和谐的机器人并制作一个验证系统,但发生了一些奇怪的事情
问题描述
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);
从v12开始,似乎没有addRole
a 的方法。我建议这样做的是: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)
});
});
资源:
推荐阅读
- azure - Azure ARM 模板:如何使用模板创建 vnet 并将其与现有 vnet 对等?
- c++ - 寻找更紧凑的语法(简单代码) - C++
- cassandra - 如何在 DSE 6 中使用或启用密钥缓存(密钥缓存)
- .net - 在值不等于的情况下如何使用数据触发器
- r - 在r中循环使用日期时间的列表元素
- python - Python 3中的奇怪字符正方形+ 0101
- unity3d - Unity AI 需要将游戏对象向另一个对象移动给定距离
- html - 如何在 Flask 中国际化标题?
- haskell - 函数签名如何匹配请求的类型
- ajax - 经典 ASP 记录集插入和文件上传