Quote:
Originally Posted by Kwarde
pawn Code:
DCC_GetGuildMemberRoleCount(guild, author, roleCount); if(channel == g_Discord_Admin_CMD) { if(!strcmp(command, "!mycommand", true)) { //Loop through all roles for this user for(new i = 0; i < roleCount; i++) { DCC_GetGuildRole(guild, i, role); //you can compare 'role' directly to 'g_Role_Level_X' if(role == g_Role_Level_1 || role == g_Role_Level_2 || role == g_Role_Level_3 || role == g_Role_Level_4) { //My actions //if you don't break here the command may execute many times. for example if user has both g_Role_Level_1 and g_Role_Level_3 break; } else return DCC_SendChannelMessage(g_Discord_Admin_CMD, "```ERROR: You are not a high enough level to use this command```"); } }
//other commands }
Let's say there are 5 roles. Level 0 up to 4. Someone with level 0 uses this.
pawn Code:
for (new i; i < roleCount; i++) { //Loop i=0: Get user role (role = 0) Role is not 1,2,3 or 4. Send message: ERROR: You are not a high enough level to use this command. Break entire function (not just the loop), return return value of DCC_SendChannelMessage(); }
Now let's say they have level 2.
pawn Code:
for (new i; i < roleCount; i++) { //Loop i=0: Get user role (role = 0) //Assuming this role is read before any other role. Role is not 1,2,3 or 4 Send message: ERROR: You are not a high enough level to use this command. Break entire function (not just the loop), return return value of DCC_SendChannelMessage(); }
You might wanna use DCC_HasGuildMemberRole(DCC_Guild:guild, DCC_User:user, DCC_Role:role, &bool:has_role); instead. This piece of code (as you can see) is
1- Inefficient because it could loop through all the roles every time someone uses that commands.
2- As seen in a above example, if their first read role is not level 1-4, it would send the error aswell.
As of the unreachable code; make sure you have no code after using return/break/continue in the same level.
|
I have it like this :
pawn Code:
if(!strcmp(command, "!mycommand", true)) {
for(new i = 0; i < roleCount; i++) {
DCC_GetGuildRole(g_Discord_Guild, i, rolename);
if(rolename == g_Role_Level_1 || rolename == g_Role_Level_2 || rolename == g_Role_Level_3 || rolename == g_Role_Level_4) {
//My actions
break;
} else return DCC_SendChannelMessage(g_Discord_Admin_CMD, "```ERROR: You are not a high enough level to use this command```");
}
}
or should i break it like :
pawn Code:
if(!strcmp(command, "!mycommand", true)) {
for(new i = 0; i < roleCount; i++) {
DCC_GetGuildRole(g_Discord_Guild, i, rolename);
if(rolename == g_Role_Level_1 || rolename == g_Role_Level_2 || rolename == g_Role_Level_3 || rolename == g_Role_Level_4) {
//My actions
} else return DCC_SendChannelMessage(g_Discord_Admin_CMD, "```ERROR: You are not a high enough level to use this command```");
break;
}
}