Problem: Cannot get this funtion to return, NOT BREAK, if the user does not have the defined role. Tried a few different formats but here it is clean. Every time i try an else if i can an unexpected symbol so i think my formatting is bad.
Goal: Implement a Role Permission.
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);
Update: The permission part is working alright when the using doesn't have the role, but it does NOTHING when a user has the role.
Are you sure you using discord v12 version ? for discord v12 you can use:
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')
for discord v11 you can use:
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;
}
});