0
votes

I want to make a discord help bot, which take questions to users. If the users can answer all questions correct, the bot give them a role on the server.

How could I do that, the bot give role to user on the server?

I wrote in javascript, with discord.js module.

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

1 Answers

1
votes

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 {
                    [...]
                }
            }
        }
    }
});

First of all you need to find the guild which i made like let guild = client.guilds.get("Guild ID");, after you need to verify if the user is member of the guild let member = message.guild.member(datas.user); and on the end you just add it to the member member.addRole("Role Id").