首页 > 解决方案 > 我正在编写一个不和谐的机器人并制作一个验证系统,但发生了一些奇怪的事情

问题描述

    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)
                });
        });
        
    }

这是我需要帮助的代码,所以它的作用是通过让你回答问题来确保你是人类,例如:“天空是什么颜色”,但如果你回答蓝色,它会说“太好了!你现在被验证了!” 然后说“看起来你没有得到正确的答案,正确的答案是蓝色的”即使你写了正确的答案,它甚至没有给你验证的角色或者 代码

标签: discord.js

解决方案


您需要在代码中修复几件事:

首先,您声明了一个变量myRole,但您从未真正使用它来分配代码中的角色。

其次,是你如何添加角色。

user.member.addRole(myRole);

从v12开始,似乎没有addRolea 的方法。我建议这样做的是:GuildMemberDiscord.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)
        });
});

资源:

  1. 了解如何使用角色
  2. Discord.js - GuildMember

推荐阅读