K
K
K1tekat2022-01-30 11:00:21
Node.js
K1tekat, 2022-01-30 11:00:21

How to make a command to issue a role in ds.js for a while?

I want to find how to make this command, I looked a lot, but did not find the right one. I ask you to help me with this problem, I'm just a beginner and I don't know much about it.
I found this code, but it doesn't work.

if (array[0] == '!role') {
    let member = msg.mentions.members.first() || msg.guild.members.cache.get(array[1])
    let role = msg.guild.roles.cache.get('937251550840111105');
    if (!member) {
        return msg.reply('Пользователь не найден!');
    }

    if (!role) {
        return msg.reply('Роль не найдена!');
    }

    member.roles
        .add(role)
        .then(() => {
            msg.reply('done');
        })
        .catch((err) => {
            msg.reply('failed');
        });
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2022-01-30
@K1tekat

if you are unable to read the code you copy-paste, I highly recommend reading at least a few chapters from learn.javascript.ru .
in this code there is nothing that would be connected with the removal of the role from the user - especially after some time has elapsed.
discord.js doesn't have a defined method that can temporarily set the role, so you can use the usual setTimeout() and logic.
assign a role to the user, after a certain time - delete it from him:

let member = message.mentions.members.first() || message.guild.members.cache.get(array[1])

let role = message.guild.roles.cache.find(r => r.name === "название_роли"); // или message.guild.roles.cache.get("ID_роли"), если вы хотите обнаружить роль по её ID.

member.roles.add(role).then((Member) => {
    setTimeout(() => {
        Member.roles.remove(role)
    }, 2000);
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question