当我在Javascript中输入特定词组时,如何制作一个只给我角色的Discord机器人?

时间:2020-06-24 05:52:32

标签: javascript bots discord discord.js roles

我已经完成了的搜索,但是它们都是超级复杂的代码,例如当我说“ !角色(角色)< / em>”,那么它将给我指定的角色。但是,我正在寻找的东西要简单得多,例如,如果我说“ Hello ”,那么该机器人将为我提供代码中的角色。

我也尝试了很多复杂的函数,但是大多数都使用了“ addRole”函数,但是输出结果却不喜欢

您认为您可以帮我吗?

1 个答案:

答案 0 :(得分:1)

Discord JS V12:

client.on("message", (message) => {
    // Checking if the message equals to "hello".
    // Since we use .toLowerCase() which converts any uppercase letter to lowercase, HeLLo will result in hello.
    if (message.content.toLowerCase() == "hello") {
        // Trying to find the role by ID.
        const Role = message.guild.roles.cache.get("RoleID");
        // Checking if the role exists.
        if (!Role) { // The role doesn't exist.
            message.channel.send(`I'm sorry, the role doesn't exist.`);
        } else { // The role exists.
            // Adding the role to the user.
            message.member.roles.add(Role).catch((error) => {console.error(error)});
            message.channel.send(`You received the role ${Role.name}.`);
        };
    }
});

Discord JS V11:

client.on("message", (message) => {
    if (message.content.toLowerCase() == "hello") {
        const Role = message.guild.roles.get("RoleID");
        if (!Role) {
            message.channel.send(`I'm sorry, the role doesn't exist.`);
        } else {
            message.member.addRole(Role).catch((error) => {console.error(error)})
            message.channel.send(`You received the role ${Role.name}.`);
        };
    }
});