通过直接消息给用户角色

时间:2019-09-04 16:24:22

标签: discord.js

我想制作一个不和谐的帮助机器人,向用户提问。如果用户可以正确回答所有问题,则机器人将在服务器上为他们分配角色。

我该怎么做,机器人将角色授予服务器上的用户?

我用discord.js模块用JavaScript编写。

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

1 个答案:

答案 0 :(得分:1)


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

    let messageArray = datas.content.split(" ");
    let command = messageArray[0];
    let args = messageArray.slice(1);
    let guild = client.guilds.get("Guild ID");
    let member = message.guild.member(datas.author.bot);
    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!");
                             member.addRole("Role Id")
                            [...]
                            return;
                        }
                        [...]
                    } else {
                        [...]
                    }
                } else {
                    [...]
                }
            }
        }
    }
});

首先,需要确定用户是否是公会let guild = client.guilds.get("Guild ID");的成员后,再像let member = message.guild.member(datas.user);一样找到我创建的公会,最后将其添加到公会中。成员member.addRole("Role Id")