【问题标题】:How do i make a discord bot that simply gives me a role when i type a certain phrase in Javascript?我如何制作一个不和谐机器人,当我在 Javascript 中键入某个短语时,它只会给我一个角色?
【发布时间】:2020-10-14 07:51:10
【问题描述】:

我已经完成了 tonstons 的搜索,但它们都是超级复杂的代码,例如,当我说“!Role (role)" 然后它给了我我指定的角色。然而,我正在寻找更简单的东西,比如如果我说“Hello”,那么机器人会给我代码中的角色。

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

你觉得你能帮我解决这个问题吗?

【问题讨论】:

    标签: javascript bots discord discord.js roles


    【解决方案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}.`);
            };
        }
    });
    

    【讨论】:

    • 它似乎有一个好的开始,但我认为我这边有一个问题:DiscordAPIError: Missing Permissions at RequestHandler.execute (C:\Users\░░\Desktop\Discord Bot\node_modules \discord.js\src\rest\RequestHandler.js:170:25) 在 processTicksAndRejections (internal/process/task_queues.js:97:5) {方法:'put',路径:'/guilds/725126311168966749/members/625394153479471136 /roles/725139116001329182', code: 50013, httpStatus: 403 } 即使我给了我的机器人完全的管理员权限。我做错了什么?
    • 确保您的机器人角色高于您想要赋予的角色。这能解决您的问题吗?
    • 天哪!这很有意义!非常感谢!
    猜你喜欢
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 2020-11-18
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多