Discord.js 无法理解 members.role(格式化)

Discord.js Trouble comprehending members.role (Formatting)

问题:如果用户没有定义的角色,则无法将此功能提供给 return,NOT BREAK。尝试了几种不同的格式,但这里很干净。每次我尝试 else 如果我能得到一个意想不到的符号,所以我认为我的格式很糟糕。

目标:实施角色权限。

     bot.on('message', function(message) {
    /* If message content not start with prefix stop function. Better do negative check, because it will avoid nesting. */
if(!message.content.startsWith(PREFIX)) return 
    /* if message startsWiht prefix declarate a command */
let command = message.content.substring(message.content.indexOf(" ") + 1, message.content.length);
    /* Now we can check user role */
if (!message.member.roles.cache.has('691441168000483409')) { 
        /* if Message.member has no target role stop function, send error message, and delete after 5s */ 
        message.reply('You has no permission for this').then(msg => {
            msg.delete({ timeout: 2000 }); 
        });
        return;
    }

bot.on('message', message => {
    let args = message.content.substring(PREFIX.length).split(" ");

    switch(args[0].toLowerCase()) {
        case "aa1":
            bot.commands.get('aa1').execute(message, args);
        break;

        case "aa2":
            bot.commands.get('aa2').execute(message, args);
        break;




    }

});
});


bot.login(token);

更新:当用户没有角色时,权限部分工作正常,但当用户有角色时它什么都不做。

您确定使用的是 discord v12 版本吗? 对于 discord v12,您可以使用:

if (!message.member.roles.cache.has('ROLE ID')) return message.reply('You has no permission for this')

if (!message.member.roles.cache.some(role => ['ID','ID2'].includes(role.id))) return message.reply('You has no permission for this')

对于 discord v11,您可以使用:

if (!message.member.roles.has('ROLE ID')) return message.reply('You has no permission for this')

if (!message.member.roles.some(role => ['ID','ID2'].includes(role.id))) return message.reply('You has no permission for this')

V2

bot.on('message', function(message) {
    /* If message content not start with prefix stop function. Better do negative check, because it will avoid nesting. */
if(!message.content.startsWith(PREFIX)) return 
    /* if message startsWiht prefix declarate a command */
let command = message.content.substring(message.content.indexOf(" ") + 1, message.content.length);
    /* Now we can check user role */
if (!message.member.roles.cache.has('691441168000483409')) { 
        /* if Message.member has no target role stop function, send error message, and delete after 5s */ 
        message.reply('You has no permission for this').then(msg => {
            msg.delete({ timeout: 2000 }); 
        });
        return;
    }
    /* Now you can enter code whats you need */


});

V3

bot.on('message', message => {
    if (message.channel.type === 'dm') return
    let args = message.content.substring(PREFIX.length).split(" ");
        /* If message content not start with prefix stop function. Better do negative check, because it will avoid nesting. */
    if(!message.content.startsWith(PREFIX)) return 
    /* if message startsWiht prefix declarate a command */
    let command = message.content.substring(message.content.indexOf(" ") + 1, message.content.length);
    /* Now we can check user role */
    if (!message.member.roles.cache.has('691441168000483409')) { 
        /* if Message.member has no target role stop function, send error message, and delete after 5s */ 
        message.reply('You has no permission for this').then(msg => {
            msg.delete({ timeout: 2000 }); 
        });
        return;
    }
    switch(args[0].toLowerCase()) {
        case "aa1":
            bot.commands.get('aa1').execute(message, args);
        break;
        case "aa2":
            bot.commands.get('aa2').execute(message, args);
        break;
    }
});