【问题标题】:Discord.js I am not able to create a role with specific permission which is a mute roleDiscord.js 我无法创建具有特定权限的角色,这是一个静音角色
【发布时间】:2021-10-10 03:09:20
【问题描述】:
module.exports = {
    name: "mute",
    description: "Use this to mute members",

    execute: async function (msg, arg) {
        const muterole = await msg.guild.roles.cache.find((r) => r.name == "Mute");
        let promise = new Promise(async function (resolve, reject) {
            if (muterole) {
                resolve(commandmute(msg, arg));
            } else {
                reject(createrole(msg, arg));
            }
        });

        async function createrole(msg, arg) {
            msg.guild.roles.create({
                data: {
                    name: "Mute",
                },
                reason: "This is a mute role created by the bot ",
            });
            const tole = msg.guild.roles.cache.find((r) => r.name == "Mute");
            const ch1 = msg.guild.channels.cache.forEach((ch, msg) => {
                if ((ch = msg.guild.channels.type("text"))) {
                    ch.updateOverwrite(tole, { VIEW_MESSAGES: true, SEND_MESSAGES: true });
                }

                msg.reply("It seems that the server doesnt have a Mute role so the bot has created one ");
                msg.reply("Successfully created the role");
            });
        }

        function commandmute(msg, arg) {}
    },
};

所以我使用 promise 来检查服务器是否具有静音角色,如果没有,它将传递给一个函数,该函数将创建一个名为“静音”的角色。

现在的问题是我无法弄清楚如何更改频道中角色的权限,以使具有上述角色的用户无法发送消息。

另外,我无法通过 role.id 获取角色的 id。 它显示未定义。

【问题讨论】:

  • 我没有看到你在任何地方定义role。它在哪里?

标签: javascript node.js discord discord.js


【解决方案1】:

如果 role.id 显示未定义,那一定是因为您的“tole”常量也未定义。我建议你试试这个:

async function createrole(msg, arg) {
            msg.guild.roles.create({
                data: {
                    name: "Mute",
                },
                reason: "This is a mute role created by the bot ",
            }).then( role => {
              msg.guild.channels.cache.forEach((ch, msg) => {
                if (ch = msg.guild.channels.type("text")) {
                  ch.updateOverwrite(role, { VIEW_MESSAGES: true, SEND_MESSAGES: false });
                }
              }
            });

            msg.reply("It seems that the server doesnt have a Mute role so the bot has created one ");
            msg.reply("Successfully created the role");
            
        }

使用then(),确保创建了角色,并且可以直接获取角色对象并使用它,而不是使用msg.guild.roles.cache.find((r) => r.name == "Mute")

【讨论】:

    猜你喜欢
    • 2021-08-19
    • 2019-06-26
    • 2018-10-11
    • 2021-07-27
    • 2020-09-09
    • 2021-05-30
    • 2020-04-27
    • 2021-03-30
    • 2020-10-23
    相关资源
    最近更新 更多