Discord.js 通过直接消息将角色授予用户

Discord.js 通过直接消息将角色授予用户,discord.js,Discord.js,我想制作一个discord帮助机器人,它向用户提问。如果用户能够正确回答所有问题,bot将为他们提供服务器上的角色 我如何才能做到这一点,bot将角色授予服务器上的用户 我用javascript编写,带有discord.js模块 client.on("message", (datas) => { if (datas.author.bot) { return; } let messageArray = datas.content.split(" "); let co

我想制作一个discord帮助机器人,它向用户提问。如果用户能够正确回答所有问题,bot将为他们提供服务器上的角色

我如何才能做到这一点,bot将角色授予服务器上的用户

我用javascript编写,带有discord.js模块

client.on("message", (datas) => {
    if (datas.author.bot) { return; }

    let messageArray = datas.content.split(" ");
    let command = messageArray[0];
    let args = messageArray.slice(1);

    if (datas.channel.type === "dm") {
        [...]
        if (usersInTest[datas.user] !== undefined) {
            [...]
            } else if (usersInTest[datas.user][0] < testQuestions.length) {
                if (Number(datas.content) && Number(datas.content)>=1 && Number(datas.content)<=3) {
                    if (testQuestions[usersInTest[datas.user][0]][4] === Number(datas.content)) {
                        if (usersInTest[datas.user][0]+1 >= testQuestions.length) {
                            datas.channel.send("You successfully complete the test!");
//give role to user
                            [...]
                            return;
                        }
                        [...]
                    } else {
                        [...]
                    }
                } else {
                    [...]
                }
            }
        }
    }
});

function sendQuestion(channel, user) {
    if (usersInTest[user] !== undefined || channel !== undefined) {
        channel.send(testQuestions[usersInTest[user][0]][0]+"\n\n(1) " + testQuestions[usersInTest[user][0]][1] + "\n(2) " + testQuestions[usersInTest[user][0]][2] + "\n(3) " + testQuestions[usersInTest[user][0]][3] + "\n Type the answer to continue.");
    }
}
client.onmessage,client,datas=>{ 如果datas.author.bot{return;} 让messageArray=datas.content.split; let命令=messageArray[0]; 让args=messageArray.slice1; 让guild=client.guilds.getGuild ID; 让member=message.guild.memberdatas.author.bot; 如果datas.channel.type==dm{ [...] 如果用户测试[datas.user]!==未定义{ [...] }如果用户测试[datas.user][0]=1&&Numberdatas.content=testQuestions.length{ datas.channel.sendYou成功完成测试!; member.addRoleRole Id [...] 回来 } [...] }否则{ [...] } }否则{ [...] } } } } };
首先,你需要找到我创建的公会,比如让guild=client.guilds.getGuild ID;,需要验证用户是否为帮会成员后,请让member=message.guild.memberdatas.user;最后,您只需将其添加到member.addRoleRole Id。

您可能需要确保message.guild.member。。。是真实的,因为如果用户不在公会中,它将是未定义的。从任何被拒绝的承诺中发现错误也很好。